PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/gfs2/inode.c

https://bitbucket.org/digetx/picasso-kernel
C | 1861 lines | 1349 code | 274 blank | 238 comment | 304 complexity | 76b26c794c89b05f9eb314c91878d6be MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/security.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOBUFS);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  166. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  167. fail_iopen:
  168. if (io_gl)
  169. gfs2_glock_put(io_gl);
  170. fail_put:
  171. ip->i_gl->gl_object = NULL;
  172. gfs2_glock_put(ip->i_gl);
  173. fail:
  174. iget_failed(inode);
  175. return ERR_PTR(error);
  176. }
  177. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  178. u64 *no_formal_ino, unsigned int blktype)
  179. {
  180. struct super_block *sb = sdp->sd_vfs;
  181. struct gfs2_holder i_gh;
  182. struct inode *inode = NULL;
  183. int error;
  184. /* Must not read in block until block type is verified */
  185. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  186. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  187. if (error)
  188. return ERR_PTR(error);
  189. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  190. if (error)
  191. goto fail;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  193. if (IS_ERR(inode))
  194. goto fail;
  195. /* Two extra checks for NFS only */
  196. if (no_formal_ino) {
  197. error = -ESTALE;
  198. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  199. goto fail_iput;
  200. error = -EIO;
  201. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  202. goto fail_iput;
  203. error = 0;
  204. }
  205. fail:
  206. gfs2_glock_dq_uninit(&i_gh);
  207. return error ? ERR_PTR(error) : inode;
  208. fail_iput:
  209. iput(inode);
  210. goto fail;
  211. }
  212. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  213. {
  214. struct qstr qstr;
  215. struct inode *inode;
  216. gfs2_str2qstr(&qstr, name);
  217. inode = gfs2_lookupi(dip, &qstr, 1);
  218. /* gfs2_lookupi has inconsistent callers: vfs
  219. * related routines expect NULL for no entry found,
  220. * gfs2_lookup_simple callers expect ENOENT
  221. * and do not check for NULL.
  222. */
  223. if (inode == NULL)
  224. return ERR_PTR(-ENOENT);
  225. else
  226. return inode;
  227. }
  228. /**
  229. * gfs2_lookupi - Look up a filename in a directory and return its inode
  230. * @d_gh: An initialized holder for the directory glock
  231. * @name: The name of the inode to look for
  232. * @is_root: If 1, ignore the caller's permissions
  233. * @i_gh: An uninitialized holder for the new inode glock
  234. *
  235. * This can be called via the VFS filldir function when NFS is doing
  236. * a readdirplus and the inode which its intending to stat isn't
  237. * already in cache. In this case we must not take the directory glock
  238. * again, since the readdir call will have already taken that lock.
  239. *
  240. * Returns: errno
  241. */
  242. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  243. int is_root)
  244. {
  245. struct super_block *sb = dir->i_sb;
  246. struct gfs2_inode *dip = GFS2_I(dir);
  247. struct gfs2_holder d_gh;
  248. int error = 0;
  249. struct inode *inode = NULL;
  250. int unlock = 0;
  251. if (!name->len || name->len > GFS2_FNAMESIZE)
  252. return ERR_PTR(-ENAMETOOLONG);
  253. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  254. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  255. dir == sb->s_root->d_inode)) {
  256. igrab(dir);
  257. return dir;
  258. }
  259. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  260. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  261. if (error)
  262. return ERR_PTR(error);
  263. unlock = 1;
  264. }
  265. if (!is_root) {
  266. error = gfs2_permission(dir, MAY_EXEC);
  267. if (error)
  268. goto out;
  269. }
  270. inode = gfs2_dir_search(dir, name);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (unlock)
  275. gfs2_glock_dq_uninit(&d_gh);
  276. if (error == -ENOENT)
  277. return NULL;
  278. return inode ? inode : ERR_PTR(error);
  279. }
  280. /**
  281. * create_ok - OK to create a new on-disk inode here?
  282. * @dip: Directory in which dinode is to be created
  283. * @name: Name of new dinode
  284. * @mode:
  285. *
  286. * Returns: errno
  287. */
  288. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  289. umode_t mode)
  290. {
  291. int error;
  292. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  293. if (error)
  294. return error;
  295. /* Don't create entries in an unlinked directory */
  296. if (!dip->i_inode.i_nlink)
  297. return -ENOENT;
  298. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  299. switch (error) {
  300. case -ENOENT:
  301. error = 0;
  302. break;
  303. case 0:
  304. return -EEXIST;
  305. default:
  306. return error;
  307. }
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  315. struct inode *inode)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  319. if (S_ISDIR(inode->i_mode))
  320. inode->i_mode |= S_ISUID;
  321. else if (dip->i_inode.i_uid != current_fsuid())
  322. inode->i_mode &= ~07111;
  323. inode->i_uid = dip->i_inode.i_uid;
  324. } else
  325. inode->i_uid = current_fsuid();
  326. if (dip->i_inode.i_mode & S_ISGID) {
  327. if (S_ISDIR(inode->i_mode))
  328. inode->i_mode |= S_ISGID;
  329. inode->i_gid = dip->i_inode.i_gid;
  330. } else
  331. inode->i_gid = current_fsgid();
  332. }
  333. static int alloc_dinode(struct gfs2_inode *ip, u32 flags)
  334. {
  335. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  336. int error;
  337. int dblocks = 1;
  338. error = gfs2_inplace_reserve(ip, RES_DINODE, flags);
  339. if (error)
  340. goto out;
  341. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  342. if (error)
  343. goto out_ipreserv;
  344. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, &dblocks, 1, &ip->i_generation);
  345. ip->i_no_formal_ino = ip->i_generation;
  346. ip->i_inode.i_ino = ip->i_no_addr;
  347. ip->i_goal = ip->i_no_addr;
  348. gfs2_trans_end(sdp);
  349. out_ipreserv:
  350. gfs2_inplace_release(ip);
  351. out:
  352. return error;
  353. }
  354. static void gfs2_init_dir(struct buffer_head *dibh,
  355. const struct gfs2_inode *parent)
  356. {
  357. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  358. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  359. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  360. dent->de_inum = di->di_num; /* already GFS2 endian */
  361. dent->de_type = cpu_to_be16(DT_DIR);
  362. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  363. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  364. gfs2_inum_out(parent, dent);
  365. dent->de_type = cpu_to_be16(DT_DIR);
  366. }
  367. /**
  368. * init_dinode - Fill in a new dinode structure
  369. * @dip: The directory this inode is being created in
  370. * @ip: The inode
  371. * @symname: The symlink destination (if a symlink)
  372. * @bhp: The buffer head (returned to caller)
  373. *
  374. */
  375. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  376. const char *symname, struct buffer_head **bhp)
  377. {
  378. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  379. struct gfs2_dinode *di;
  380. struct buffer_head *dibh;
  381. struct timespec tv = CURRENT_TIME;
  382. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  383. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  384. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  385. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  386. di = (struct gfs2_dinode *)dibh->b_data;
  387. di->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  388. di->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  389. di->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  390. di->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  391. di->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  392. di->di_nlink = 0;
  393. di->di_size = cpu_to_be64(ip->i_inode.i_size);
  394. di->di_blocks = cpu_to_be64(1);
  395. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  396. di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
  397. di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
  398. di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_no_addr);
  399. di->di_generation = cpu_to_be64(ip->i_generation);
  400. di->di_flags = 0;
  401. di->__pad1 = 0;
  402. di->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) ? GFS2_FORMAT_DE : 0);
  403. di->di_height = 0;
  404. di->__pad2 = 0;
  405. di->__pad3 = 0;
  406. di->di_depth = 0;
  407. di->di_entries = 0;
  408. memset(&di->__pad4, 0, sizeof(di->__pad4));
  409. di->di_eattr = 0;
  410. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  411. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  412. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  413. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  414. switch(ip->i_inode.i_mode & S_IFMT) {
  415. case S_IFREG:
  416. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  417. gfs2_tune_get(sdp, gt_new_files_jdata))
  418. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  419. break;
  420. case S_IFDIR:
  421. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  422. GFS2_DIF_INHERIT_JDATA);
  423. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  424. di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  425. di->di_entries = cpu_to_be32(2);
  426. gfs2_init_dir(dibh, dip);
  427. break;
  428. case S_IFLNK:
  429. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  430. break;
  431. }
  432. set_buffer_uptodate(dibh);
  433. *bhp = dibh;
  434. }
  435. static int make_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  436. const char *symname, struct buffer_head **bhp)
  437. {
  438. struct inode *inode = &ip->i_inode;
  439. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  440. int error;
  441. error = gfs2_rindex_update(sdp);
  442. if (error)
  443. return error;
  444. error = gfs2_quota_lock(dip, inode->i_uid, inode->i_gid);
  445. if (error)
  446. return error;
  447. error = gfs2_quota_check(dip, inode->i_uid, inode->i_gid);
  448. if (error)
  449. goto out_quota;
  450. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  451. if (error)
  452. goto out_quota;
  453. init_dinode(dip, ip, symname, bhp);
  454. gfs2_quota_change(dip, +1, inode->i_uid, inode->i_gid);
  455. gfs2_trans_end(sdp);
  456. out_quota:
  457. gfs2_quota_unlock(dip);
  458. return error;
  459. }
  460. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  461. struct gfs2_inode *ip)
  462. {
  463. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  464. int alloc_required;
  465. struct buffer_head *dibh;
  466. int error;
  467. error = gfs2_rindex_update(sdp);
  468. if (error)
  469. return error;
  470. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  471. if (error)
  472. goto fail;
  473. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  474. if (alloc_required < 0)
  475. goto fail_quota_locks;
  476. if (alloc_required) {
  477. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  478. if (error)
  479. goto fail_quota_locks;
  480. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres, 0);
  481. if (error)
  482. goto fail_quota_locks;
  483. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  484. dip->i_rgd->rd_length +
  485. 2 * RES_DINODE +
  486. RES_STATFS + RES_QUOTA, 0);
  487. if (error)
  488. goto fail_ipreserv;
  489. } else {
  490. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  491. if (error)
  492. goto fail_quota_locks;
  493. }
  494. error = gfs2_dir_add(&dip->i_inode, name, ip);
  495. if (error)
  496. goto fail_end_trans;
  497. error = gfs2_meta_inode_buffer(ip, &dibh);
  498. if (error)
  499. goto fail_end_trans;
  500. set_nlink(&ip->i_inode, S_ISDIR(ip->i_inode.i_mode) ? 2 : 1);
  501. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  502. gfs2_dinode_out(ip, dibh->b_data);
  503. brelse(dibh);
  504. return 0;
  505. fail_end_trans:
  506. gfs2_trans_end(sdp);
  507. fail_ipreserv:
  508. if (alloc_required)
  509. gfs2_inplace_release(dip);
  510. fail_quota_locks:
  511. gfs2_quota_unlock(dip);
  512. fail:
  513. return error;
  514. }
  515. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  516. void *fs_info)
  517. {
  518. const struct xattr *xattr;
  519. int err = 0;
  520. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  521. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  522. xattr->value_len, 0,
  523. GFS2_EATYPE_SECURITY);
  524. if (err < 0)
  525. break;
  526. }
  527. return err;
  528. }
  529. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  530. const struct qstr *qstr)
  531. {
  532. return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  533. &gfs2_initxattrs, NULL);
  534. }
  535. /**
  536. * gfs2_create_inode - Create a new inode
  537. * @dir: The parent directory
  538. * @dentry: The new dentry
  539. * @mode: The permissions on the new inode
  540. * @dev: For device nodes, this is the device number
  541. * @symname: For symlinks, this is the link destination
  542. * @size: The initial size of the inode (ignored for directories)
  543. *
  544. * Returns: 0 on success, or error code
  545. */
  546. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  547. umode_t mode, dev_t dev, const char *symname,
  548. unsigned int size, int excl)
  549. {
  550. const struct qstr *name = &dentry->d_name;
  551. struct gfs2_holder ghs[2];
  552. struct inode *inode = NULL;
  553. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  554. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  555. struct gfs2_glock *io_gl;
  556. int error;
  557. struct buffer_head *bh = NULL;
  558. u32 aflags = 0;
  559. if (!name->len || name->len > GFS2_FNAMESIZE)
  560. return -ENAMETOOLONG;
  561. error = gfs2_rs_alloc(dip);
  562. if (error)
  563. return error;
  564. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  565. if (error)
  566. goto fail;
  567. error = create_ok(dip, name, mode);
  568. if ((error == -EEXIST) && S_ISREG(mode) && !excl) {
  569. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  570. gfs2_glock_dq_uninit(ghs);
  571. d_instantiate(dentry, inode);
  572. return IS_ERR(inode) ? PTR_ERR(inode) : 0;
  573. }
  574. if (error)
  575. goto fail_gunlock;
  576. inode = new_inode(sdp->sd_vfs);
  577. if (!inode) {
  578. gfs2_glock_dq_uninit(ghs);
  579. return -ENOMEM;
  580. }
  581. ip = GFS2_I(inode);
  582. error = gfs2_rs_alloc(ip);
  583. if (error)
  584. goto fail_free_inode;
  585. set_bit(GIF_INVALID, &ip->i_flags);
  586. inode->i_mode = mode;
  587. inode->i_rdev = dev;
  588. inode->i_size = size;
  589. munge_mode_uid_gid(dip, inode);
  590. ip->i_goal = dip->i_goal;
  591. if ((GFS2_I(sdp->sd_root_dir->d_inode) == dip) ||
  592. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  593. aflags |= GFS2_AF_ORLOV;
  594. error = alloc_dinode(ip, aflags);
  595. if (error)
  596. goto fail_free_inode;
  597. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  598. if (error)
  599. goto fail_free_inode;
  600. ip->i_gl->gl_object = ip;
  601. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  602. if (error)
  603. goto fail_free_inode;
  604. error = make_dinode(dip, ip, symname, &bh);
  605. if (error)
  606. goto fail_gunlock2;
  607. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  608. if (error)
  609. goto fail_gunlock2;
  610. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  611. if (error)
  612. goto fail_gunlock2;
  613. ip->i_iopen_gh.gh_gl->gl_object = ip;
  614. gfs2_glock_put(io_gl);
  615. gfs2_set_iop(inode);
  616. insert_inode_hash(inode);
  617. error = gfs2_inode_refresh(ip);
  618. if (error)
  619. goto fail_gunlock3;
  620. error = gfs2_acl_create(dip, inode);
  621. if (error)
  622. goto fail_gunlock3;
  623. error = gfs2_security_init(dip, ip, name);
  624. if (error)
  625. goto fail_gunlock3;
  626. error = link_dinode(dip, name, ip);
  627. if (error)
  628. goto fail_gunlock3;
  629. if (bh)
  630. brelse(bh);
  631. gfs2_trans_end(sdp);
  632. gfs2_inplace_release(dip);
  633. gfs2_quota_unlock(dip);
  634. mark_inode_dirty(inode);
  635. gfs2_glock_dq_uninit_m(2, ghs);
  636. d_instantiate(dentry, inode);
  637. return 0;
  638. fail_gunlock3:
  639. gfs2_glock_dq_uninit(ghs + 1);
  640. if (ip->i_gl)
  641. gfs2_glock_put(ip->i_gl);
  642. goto fail_gunlock;
  643. fail_gunlock2:
  644. gfs2_glock_dq_uninit(ghs + 1);
  645. fail_free_inode:
  646. if (ip->i_gl)
  647. gfs2_glock_put(ip->i_gl);
  648. gfs2_rs_delete(ip);
  649. free_inode_nonrcu(inode);
  650. inode = NULL;
  651. fail_gunlock:
  652. gfs2_glock_dq_uninit(ghs);
  653. if (inode && !IS_ERR(inode)) {
  654. set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
  655. iput(inode);
  656. }
  657. fail:
  658. if (bh)
  659. brelse(bh);
  660. return error;
  661. }
  662. /**
  663. * gfs2_create - Create a file
  664. * @dir: The directory in which to create the file
  665. * @dentry: The dentry of the new file
  666. * @mode: The mode of the new file
  667. *
  668. * Returns: errno
  669. */
  670. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  671. umode_t mode, bool excl)
  672. {
  673. return gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0, excl);
  674. }
  675. /**
  676. * gfs2_lookup - Look up a filename in a directory and return its inode
  677. * @dir: The directory inode
  678. * @dentry: The dentry of the new inode
  679. * @nd: passed from Linux VFS, ignored by us
  680. *
  681. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  682. *
  683. * Returns: errno
  684. */
  685. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  686. unsigned int flags)
  687. {
  688. struct inode *inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  689. if (inode && !IS_ERR(inode)) {
  690. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  691. struct gfs2_holder gh;
  692. int error;
  693. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  694. if (error) {
  695. iput(inode);
  696. return ERR_PTR(error);
  697. }
  698. gfs2_glock_dq_uninit(&gh);
  699. }
  700. return d_splice_alias(inode, dentry);
  701. }
  702. /**
  703. * gfs2_link - Link to a file
  704. * @old_dentry: The inode to link
  705. * @dir: Add link to this directory
  706. * @dentry: The name of the link
  707. *
  708. * Link the inode in "old_dentry" into the directory "dir" with the
  709. * name in "dentry".
  710. *
  711. * Returns: errno
  712. */
  713. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  714. struct dentry *dentry)
  715. {
  716. struct gfs2_inode *dip = GFS2_I(dir);
  717. struct gfs2_sbd *sdp = GFS2_SB(dir);
  718. struct inode *inode = old_dentry->d_inode;
  719. struct gfs2_inode *ip = GFS2_I(inode);
  720. struct gfs2_holder ghs[2];
  721. struct buffer_head *dibh;
  722. int alloc_required;
  723. int error;
  724. if (S_ISDIR(inode->i_mode))
  725. return -EPERM;
  726. error = gfs2_rs_alloc(dip);
  727. if (error)
  728. return error;
  729. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  730. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  731. error = gfs2_glock_nq(ghs); /* parent */
  732. if (error)
  733. goto out_parent;
  734. error = gfs2_glock_nq(ghs + 1); /* child */
  735. if (error)
  736. goto out_child;
  737. error = -ENOENT;
  738. if (inode->i_nlink == 0)
  739. goto out_gunlock;
  740. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  741. if (error)
  742. goto out_gunlock;
  743. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  744. switch (error) {
  745. case -ENOENT:
  746. break;
  747. case 0:
  748. error = -EEXIST;
  749. default:
  750. goto out_gunlock;
  751. }
  752. error = -EINVAL;
  753. if (!dip->i_inode.i_nlink)
  754. goto out_gunlock;
  755. error = -EFBIG;
  756. if (dip->i_entries == (u32)-1)
  757. goto out_gunlock;
  758. error = -EPERM;
  759. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  760. goto out_gunlock;
  761. error = -EINVAL;
  762. if (!ip->i_inode.i_nlink)
  763. goto out_gunlock;
  764. error = -EMLINK;
  765. if (ip->i_inode.i_nlink == (u32)-1)
  766. goto out_gunlock;
  767. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  768. if (error < 0)
  769. goto out_gunlock;
  770. error = 0;
  771. if (alloc_required) {
  772. error = gfs2_quota_lock_check(dip);
  773. if (error)
  774. goto out_gunlock;
  775. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres, 0);
  776. if (error)
  777. goto out_gunlock_q;
  778. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  779. gfs2_rg_blocks(dip, sdp->sd_max_dirres) +
  780. 2 * RES_DINODE + RES_STATFS +
  781. RES_QUOTA, 0);
  782. if (error)
  783. goto out_ipres;
  784. } else {
  785. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  786. if (error)
  787. goto out_ipres;
  788. }
  789. error = gfs2_meta_inode_buffer(ip, &dibh);
  790. if (error)
  791. goto out_end_trans;
  792. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  793. if (error)
  794. goto out_brelse;
  795. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  796. inc_nlink(&ip->i_inode);
  797. ip->i_inode.i_ctime = CURRENT_TIME;
  798. ihold(inode);
  799. d_instantiate(dentry, inode);
  800. mark_inode_dirty(inode);
  801. out_brelse:
  802. brelse(dibh);
  803. out_end_trans:
  804. gfs2_trans_end(sdp);
  805. out_ipres:
  806. if (alloc_required)
  807. gfs2_inplace_release(dip);
  808. out_gunlock_q:
  809. if (alloc_required)
  810. gfs2_quota_unlock(dip);
  811. out_gunlock:
  812. gfs2_glock_dq(ghs + 1);
  813. out_child:
  814. gfs2_glock_dq(ghs);
  815. out_parent:
  816. gfs2_holder_uninit(ghs);
  817. gfs2_holder_uninit(ghs + 1);
  818. return error;
  819. }
  820. /*
  821. * gfs2_unlink_ok - check to see that a inode is still in a directory
  822. * @dip: the directory
  823. * @name: the name of the file
  824. * @ip: the inode
  825. *
  826. * Assumes that the lock on (at least) @dip is held.
  827. *
  828. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  829. */
  830. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  831. const struct gfs2_inode *ip)
  832. {
  833. int error;
  834. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  835. return -EPERM;
  836. if ((dip->i_inode.i_mode & S_ISVTX) &&
  837. dip->i_inode.i_uid != current_fsuid() &&
  838. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  839. return -EPERM;
  840. if (IS_APPEND(&dip->i_inode))
  841. return -EPERM;
  842. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  843. if (error)
  844. return error;
  845. error = gfs2_dir_check(&dip->i_inode, name, ip);
  846. if (error)
  847. return error;
  848. return 0;
  849. }
  850. /**
  851. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  852. * @dip: The parent directory
  853. * @name: The name of the entry in the parent directory
  854. * @inode: The inode to be removed
  855. *
  856. * Called with all the locks and in a transaction. This will only be
  857. * called for a directory after it has been checked to ensure it is empty.
  858. *
  859. * Returns: 0 on success, or an error
  860. */
  861. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  862. const struct dentry *dentry)
  863. {
  864. struct inode *inode = dentry->d_inode;
  865. struct gfs2_inode *ip = GFS2_I(inode);
  866. int error;
  867. error = gfs2_dir_del(dip, dentry);
  868. if (error)
  869. return error;
  870. ip->i_entries = 0;
  871. inode->i_ctime = CURRENT_TIME;
  872. if (S_ISDIR(inode->i_mode))
  873. clear_nlink(inode);
  874. else
  875. drop_nlink(inode);
  876. mark_inode_dirty(inode);
  877. if (inode->i_nlink == 0)
  878. gfs2_unlink_di(inode);
  879. return 0;
  880. }
  881. /**
  882. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  883. * @dir: The inode of the directory containing the inode to unlink
  884. * @dentry: The file itself
  885. *
  886. * This routine uses the type of the inode as a flag to figure out
  887. * whether this is an unlink or an rmdir.
  888. *
  889. * Returns: errno
  890. */
  891. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  892. {
  893. struct gfs2_inode *dip = GFS2_I(dir);
  894. struct gfs2_sbd *sdp = GFS2_SB(dir);
  895. struct inode *inode = dentry->d_inode;
  896. struct gfs2_inode *ip = GFS2_I(inode);
  897. struct gfs2_holder ghs[3];
  898. struct gfs2_rgrpd *rgd;
  899. int error;
  900. error = gfs2_rindex_update(sdp);
  901. if (error)
  902. return error;
  903. error = -EROFS;
  904. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  905. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  906. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  907. if (!rgd)
  908. goto out_inodes;
  909. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  910. error = gfs2_glock_nq(ghs); /* parent */
  911. if (error)
  912. goto out_parent;
  913. error = gfs2_glock_nq(ghs + 1); /* child */
  914. if (error)
  915. goto out_child;
  916. error = -ENOENT;
  917. if (inode->i_nlink == 0)
  918. goto out_rgrp;
  919. if (S_ISDIR(inode->i_mode)) {
  920. error = -ENOTEMPTY;
  921. if (ip->i_entries > 2 || inode->i_nlink > 2)
  922. goto out_rgrp;
  923. }
  924. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  925. if (error)
  926. goto out_rgrp;
  927. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  928. if (error)
  929. goto out_gunlock;
  930. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  931. if (error)
  932. goto out_end_trans;
  933. error = gfs2_unlink_inode(dip, dentry);
  934. out_end_trans:
  935. gfs2_trans_end(sdp);
  936. out_gunlock:
  937. gfs2_glock_dq(ghs + 2);
  938. out_rgrp:
  939. gfs2_glock_dq(ghs + 1);
  940. out_child:
  941. gfs2_glock_dq(ghs);
  942. out_parent:
  943. gfs2_holder_uninit(ghs + 2);
  944. out_inodes:
  945. gfs2_holder_uninit(ghs + 1);
  946. gfs2_holder_uninit(ghs);
  947. return error;
  948. }
  949. /**
  950. * gfs2_symlink - Create a symlink
  951. * @dir: The directory to create the symlink in
  952. * @dentry: The dentry to put the symlink in
  953. * @symname: The thing which the link points to
  954. *
  955. * Returns: errno
  956. */
  957. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  958. const char *symname)
  959. {
  960. struct gfs2_sbd *sdp = GFS2_SB(dir);
  961. unsigned int size;
  962. size = strlen(symname);
  963. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  964. return -ENAMETOOLONG;
  965. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
  966. }
  967. /**
  968. * gfs2_mkdir - Make a directory
  969. * @dir: The parent directory of the new one
  970. * @dentry: The dentry of the new directory
  971. * @mode: The mode of the new directory
  972. *
  973. * Returns: errno
  974. */
  975. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  976. {
  977. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0, 0);
  978. }
  979. /**
  980. * gfs2_mknod - Make a special file
  981. * @dir: The directory in which the special file will reside
  982. * @dentry: The dentry of the special file
  983. * @mode: The mode of the special file
  984. * @dev: The device specification of the special file
  985. *
  986. */
  987. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  988. dev_t dev)
  989. {
  990. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0, 0);
  991. }
  992. /*
  993. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  994. * @this: move this
  995. * @to: to here
  996. *
  997. * Follow @to back to the root and make sure we don't encounter @this
  998. * Assumes we already hold the rename lock.
  999. *
  1000. * Returns: errno
  1001. */
  1002. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1003. {
  1004. struct inode *dir = &to->i_inode;
  1005. struct super_block *sb = dir->i_sb;
  1006. struct inode *tmp;
  1007. int error = 0;
  1008. igrab(dir);
  1009. for (;;) {
  1010. if (dir == &this->i_inode) {
  1011. error = -EINVAL;
  1012. break;
  1013. }
  1014. if (dir == sb->s_root->d_inode) {
  1015. error = 0;
  1016. break;
  1017. }
  1018. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1019. if (IS_ERR(tmp)) {
  1020. error = PTR_ERR(tmp);
  1021. break;
  1022. }
  1023. iput(dir);
  1024. dir = tmp;
  1025. }
  1026. iput(dir);
  1027. return error;
  1028. }
  1029. /**
  1030. * gfs2_rename - Rename a file
  1031. * @odir: Parent directory of old file name
  1032. * @odentry: The old dentry of the file
  1033. * @ndir: Parent directory of new file name
  1034. * @ndentry: The new dentry of the file
  1035. *
  1036. * Returns: errno
  1037. */
  1038. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1039. struct inode *ndir, struct dentry *ndentry)
  1040. {
  1041. struct gfs2_inode *odip = GFS2_I(odir);
  1042. struct gfs2_inode *ndip = GFS2_I(ndir);
  1043. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1044. struct gfs2_inode *nip = NULL;
  1045. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1046. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  1047. struct gfs2_rgrpd *nrgd;
  1048. unsigned int num_gh;
  1049. int dir_rename = 0;
  1050. int alloc_required = 0;
  1051. unsigned int x;
  1052. int error;
  1053. if (ndentry->d_inode) {
  1054. nip = GFS2_I(ndentry->d_inode);
  1055. if (ip == nip)
  1056. return 0;
  1057. }
  1058. error = gfs2_rindex_update(sdp);
  1059. if (error)
  1060. return error;
  1061. error = gfs2_rs_alloc(ndip);
  1062. if (error)
  1063. return error;
  1064. if (odip != ndip) {
  1065. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1066. 0, &r_gh);
  1067. if (error)
  1068. goto out;
  1069. if (S_ISDIR(ip->i_inode.i_mode)) {
  1070. dir_rename = 1;
  1071. /* don't move a dirctory into it's subdir */
  1072. error = gfs2_ok_to_move(ip, ndip);
  1073. if (error)
  1074. goto out_gunlock_r;
  1075. }
  1076. }
  1077. num_gh = 1;
  1078. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1079. if (odip != ndip) {
  1080. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1081. num_gh++;
  1082. }
  1083. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1084. num_gh++;
  1085. if (nip) {
  1086. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1087. num_gh++;
  1088. /* grab the resource lock for unlink flag twiddling
  1089. * this is the case of the target file already existing
  1090. * so we unlink before doing the rename
  1091. */
  1092. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1093. if (nrgd)
  1094. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1095. }
  1096. for (x = 0; x < num_gh; x++) {
  1097. error = gfs2_glock_nq(ghs + x);
  1098. if (error)
  1099. goto out_gunlock;
  1100. }
  1101. error = -ENOENT;
  1102. if (ip->i_inode.i_nlink == 0)
  1103. goto out_gunlock;
  1104. /* Check out the old directory */
  1105. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1106. if (error)
  1107. goto out_gunlock;
  1108. /* Check out the new directory */
  1109. if (nip) {
  1110. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1111. if (error)
  1112. goto out_gunlock;
  1113. if (nip->i_inode.i_nlink == 0) {
  1114. error = -EAGAIN;
  1115. goto out_gunlock;
  1116. }
  1117. if (S_ISDIR(nip->i_inode.i_mode)) {
  1118. if (nip->i_entries < 2) {
  1119. gfs2_consist_inode(nip);
  1120. error = -EIO;
  1121. goto out_gunlock;
  1122. }
  1123. if (nip->i_entries > 2) {
  1124. error = -ENOTEMPTY;
  1125. goto out_gunlock;
  1126. }
  1127. }
  1128. } else {
  1129. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1130. if (error)
  1131. goto out_gunlock;
  1132. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1133. switch (error) {
  1134. case -ENOENT:
  1135. error = 0;
  1136. break;
  1137. case 0:
  1138. error = -EEXIST;
  1139. default:
  1140. goto out_gunlock;
  1141. };
  1142. if (odip != ndip) {
  1143. if (!ndip->i_inode.i_nlink) {
  1144. error = -ENOENT;
  1145. goto out_gunlock;
  1146. }
  1147. if (ndip->i_entries == (u32)-1) {
  1148. error = -EFBIG;
  1149. goto out_gunlock;
  1150. }
  1151. if (S_ISDIR(ip->i_inode.i_mode) &&
  1152. ndip->i_inode.i_nlink == (u32)-1) {
  1153. error = -EMLINK;
  1154. goto out_gunlock;
  1155. }
  1156. }
  1157. }
  1158. /* Check out the dir to be renamed */
  1159. if (dir_rename) {
  1160. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1161. if (error)
  1162. goto out_gunlock;
  1163. }
  1164. if (nip == NULL)
  1165. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1166. error = alloc_required;
  1167. if (error < 0)
  1168. goto out_gunlock;
  1169. if (alloc_required) {
  1170. error = gfs2_quota_lock_check(ndip);
  1171. if (error)
  1172. goto out_gunlock;
  1173. error = gfs2_inplace_reserve(ndip, sdp->sd_max_dirres, 0);
  1174. if (error)
  1175. goto out_gunlock_q;
  1176. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1177. gfs2_rg_blocks(ndip, sdp->sd_max_dirres) +
  1178. 4 * RES_DINODE + 4 * RES_LEAF +
  1179. RES_STATFS + RES_QUOTA + 4, 0);
  1180. if (error)
  1181. goto out_ipreserv;
  1182. } else {
  1183. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1184. 5 * RES_LEAF + 4, 0);
  1185. if (error)
  1186. goto out_gunlock;
  1187. }
  1188. /* Remove the target file, if it exists */
  1189. if (nip)
  1190. error = gfs2_unlink_inode(ndip, ndentry);
  1191. if (dir_rename) {
  1192. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1193. if (error)
  1194. goto out_end_trans;
  1195. } else {
  1196. struct buffer_head *dibh;
  1197. error = gfs2_meta_inode_buffer(ip, &dibh);
  1198. if (error)
  1199. goto out_end_trans;
  1200. ip->i_inode.i_ctime = CURRENT_TIME;
  1201. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1202. gfs2_dinode_out(ip, dibh->b_data);
  1203. brelse(dibh);
  1204. }
  1205. error = gfs2_dir_del(odip, odentry);
  1206. if (error)
  1207. goto out_end_trans;
  1208. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1209. if (error)
  1210. goto out_end_trans;
  1211. out_end_trans:
  1212. gfs2_trans_end(sdp);
  1213. out_ipreserv:
  1214. if (alloc_required)
  1215. gfs2_inplace_release(ndip);
  1216. out_gunlock_q:
  1217. if (alloc_required)
  1218. gfs2_quota_unlock(ndip);
  1219. out_gunlock:
  1220. while (x--) {
  1221. gfs2_glock_dq(ghs + x);
  1222. gfs2_holder_uninit(ghs + x);
  1223. }
  1224. out_gunlock_r:
  1225. if (r_gh.gh_gl)
  1226. gfs2_glock_dq_uninit(&r_gh);
  1227. out:
  1228. return error;
  1229. }
  1230. /**
  1231. * gfs2_follow_link - Follow a symbolic link
  1232. * @dentry: The dentry of the link
  1233. * @nd: Data that we pass to vfs_follow_link()
  1234. *
  1235. * This can handle symlinks of any size.
  1236. *
  1237. * Returns: 0 on success or error code
  1238. */
  1239. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1240. {
  1241. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1242. struct gfs2_holder i_gh;
  1243. struct buffer_head *dibh;
  1244. unsigned int size;
  1245. char *buf;
  1246. int error;
  1247. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1248. error = gfs2_glock_nq(&i_gh);
  1249. if (error) {
  1250. gfs2_holder_uninit(&i_gh);
  1251. nd_set_link(nd, ERR_PTR(error));
  1252. return NULL;
  1253. }
  1254. size = (unsigned int)i_size_read(&ip->i_inode);
  1255. if (size == 0) {
  1256. gfs2_consist_inode(ip);
  1257. buf = ERR_PTR(-EIO);
  1258. goto out;
  1259. }
  1260. error = gfs2_meta_inode_buffer(ip, &dibh);
  1261. if (error) {
  1262. buf = ERR_PTR(error);
  1263. goto out;
  1264. }
  1265. buf = kzalloc(size + 1, GFP_NOFS);
  1266. if (!buf)
  1267. buf = ERR_PTR(-ENOMEM);
  1268. else
  1269. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1270. brelse(dibh);
  1271. out:
  1272. gfs2_glock_dq_uninit(&i_gh);
  1273. nd_set_link(nd, buf);
  1274. return NULL;
  1275. }
  1276. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1277. {
  1278. char *s = nd_get_link(nd);
  1279. if (!IS_ERR(s))
  1280. kfree(s);
  1281. }
  1282. /**
  1283. * gfs2_permission -
  1284. * @inode: The inode
  1285. * @mask: The mask to be tested
  1286. * @flags: Indicates whether this is an RCU path walk or not
  1287. *
  1288. * This may be called from the VFS directly, or from within GFS2 with the
  1289. * inode locked, so we look to see if the glock is already locked and only
  1290. * lock the glock if its not already been done.
  1291. *
  1292. * Returns: errno
  1293. */
  1294. int gfs2_permission(struct inode *inode, int mask)
  1295. {
  1296. struct gfs2_inode *ip;
  1297. struct gfs2_holder i_gh;
  1298. int error;
  1299. int unlock = 0;
  1300. ip = GFS2_I(inode);
  1301. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1302. if (mask & MAY_NOT_BLOCK)
  1303. return -ECHILD;
  1304. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1305. if (error)
  1306. return error;
  1307. unlock = 1;
  1308. }
  1309. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1310. error = -EACCES;
  1311. else
  1312. error = generic_permission(inode, mask);
  1313. if (unlock)
  1314. gfs2_glock_dq_uninit(&i_gh);
  1315. return error;
  1316. }
  1317. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1318. {
  1319. setattr_copy(inode, attr);
  1320. mark_inode_dirty(inode);
  1321. return 0;
  1322. }
  1323. /**
  1324. * gfs2_setattr_simple -
  1325. * @ip:
  1326. * @attr:
  1327. *
  1328. * Returns: errno
  1329. */
  1330. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1331. {
  1332. int error;
  1333. if (current->journal_info)
  1334. return __gfs2_setattr_simple(inode, attr);
  1335. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1336. if (error)
  1337. return error;
  1338. error = __gfs2_setattr_simple(inode, attr);
  1339. gfs2_trans_end(GFS2_SB(inode));
  1340. return error;
  1341. }
  1342. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1343. {
  1344. struct gfs2_inode *ip = GFS2_I(inode);
  1345. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1346. u32 ouid, ogid, nuid, ngid;
  1347. int error;
  1348. ouid = inode->i_uid;
  1349. ogid = inode->i_gid;
  1350. nuid = attr->ia_uid;
  1351. ngid = attr->ia_gid;
  1352. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  1353. ouid = nuid = NO_QUOTA_CHANGE;
  1354. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  1355. ogid = ngid = NO_QUOTA_CHANGE;
  1356. error = gfs2_quota_lock(ip, nuid, ngid);
  1357. if (error)
  1358. return error;
  1359. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1360. error = gfs2_quota_check(ip, nuid, ngid);
  1361. if (error)
  1362. goto out_gunlock_q;
  1363. }
  1364. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1365. if (error)
  1366. goto out_gunlock_q;
  1367. error = gfs2_setattr_simple(inode, attr);
  1368. if (error)
  1369. goto out_end_trans;
  1370. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1371. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1372. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1373. gfs2_quota_change(ip, blocks, nuid, ngid);
  1374. }
  1375. out_end_trans:
  1376. gfs2_trans_end(sdp);
  1377. out_gunlock_q:
  1378. gfs2_quota_unlock(ip);
  1379. return error;
  1380. }
  1381. /**
  1382. * gfs2_setattr - Change attributes on an inode
  1383. * @dentry: The dentry which is changing
  1384. * @attr: The structure describing the change
  1385. *
  1386. * The VFS layer wants to change one or more of an inodes attributes. Write
  1387. * that change out to disk.
  1388. *
  1389. * Returns: errno
  1390. */
  1391. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1392. {
  1393. struct inode *inode = dentry->d_inode;
  1394. struct gfs2_inode *ip = GFS2_I(inode);
  1395. struct gfs2_holder i_gh;
  1396. int error;
  1397. error = gfs2_rs_alloc(ip);
  1398. if (error)
  1399. return error;
  1400. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1401. if (error)
  1402. return error;
  1403. error = -EPERM;
  1404. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1405. goto out;
  1406. error = inode_change_ok(inode, attr);
  1407. if (error)
  1408. goto out;
  1409. if (attr->ia_valid & ATTR_SIZE)
  1410. error = gfs2_setattr_size(inode, attr->ia_size);
  1411. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1412. error = setattr_chown(inode, attr);
  1413. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1414. error = gfs2_acl_chmod(ip, attr);
  1415. else
  1416. error = gfs2_setattr_simple(inode, attr);
  1417. out:
  1418. if (!error)
  1419. mark_inode_dirty(inode);
  1420. gfs2_glock_dq_uninit(&i_gh);
  1421. return error;
  1422. }
  1423. /**
  1424. * gfs2_getattr - Read out an inode's attributes
  1425. * @mnt: The vfsmount the inode is being accessed from
  1426. * @dentry: The dentry to stat
  1427. * @stat: The inode's stats
  1428. *
  1429. * This may be called from the VFS directly, or from within GFS2 with the
  1430. * inode locked, so we look to see if the glock is already locked and only
  1431. * lock the glock if its not already been done. Note that its the NFS
  1432. * readdirplus operation which causes this to be called (from filldir)
  1433. * with the glock already held.
  1434. *
  1435. * Returns: errno
  1436. */
  1437. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1438. struct kstat *stat)
  1439. {
  1440. struct inode *inode = dentry->d_inode;
  1441. struct gfs2_inode *ip = GFS2_I(inode);
  1442. struct gfs2_holder gh;
  1443. int error;
  1444. int unlock = 0;
  1445. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1446. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1447. if (error)
  1448. return error;
  1449. unlock = 1;
  1450. }
  1451. generic_fillattr(inode, stat);
  1452. if (unlock)
  1453. gfs2_glock_dq_uninit(&gh);
  1454. return 0;
  1455. }
  1456. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1457. const void *data, size_t size, int flags)
  1458. {
  1459. struct inode *inode = dentry->d_inode;
  1460. struct gfs2_inode *ip = GFS2_I(inode);
  1461. struct gfs2_holder gh;
  1462. int ret;
  1463. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1464. ret = gfs2_glock_nq(&gh);
  1465. if (ret == 0) {
  1466. ret = gfs2_rs_alloc(ip);
  1467. if (ret == 0)
  1468. ret = generic_setxattr(dentry, name, data, size, flags);
  1469. gfs2_glock_dq(&gh);
  1470. }
  1471. gfs2_holder_uninit(&gh);
  1472. return ret;
  1473. }
  1474. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1475. void *data, size_t size)
  1476. {
  1477. struct inode *inode = dentry->d_inode;
  1478. struct gfs2_inode *ip = GFS2_I(inode);
  1479. struct gfs2_holder gh;
  1480. int ret;
  1481. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1482. ret = gfs2_glock_nq(&gh);
  1483. if (ret == 0) {
  1484. ret = generic_getxattr(dentry, name, data, size);
  1485. gfs2_glock_dq(&gh);
  1486. }
  1487. gfs2_holder_uninit(&gh);
  1488. return ret;
  1489. }
  1490. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1491. {
  1492. struct inode *inode = dentry->d_inode;
  1493. struct gfs2_inode *ip = GFS2_I(inode);
  1494. struct gfs2_holder gh;
  1495. int ret;
  1496. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1497. ret = gfs2_glock_nq(&gh);
  1498. if (ret == 0) {
  1499. ret = gfs2_rs_alloc(ip);
  1500. if (ret == 0)
  1501. ret = generic_removexattr(dentry, name);
  1502. gfs2_glock_dq(&gh);
  1503. }
  1504. gfs2_holder_uninit(&gh);
  1505. return ret;
  1506. }
  1507. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1508. u64 start, u64 len)
  1509. {
  1510. struct gfs2_inode *ip = GFS2_I(inode);
  1511. struct gfs2_holder gh;
  1512. int ret;
  1513. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1514. if (ret)
  1515. return ret;
  1516. mutex_lock(&inode->i_mutex);
  1517. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1518. if (ret)
  1519. goto out;
  1520. if (gfs2_is_stuffed(ip)) {
  1521. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1522. u64 size = i_size_read(inode);
  1523. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1524. FIEMAP_EXTENT_DATA_INLINE;
  1525. phys += sizeof(struct gfs2_dinode);
  1526. phys += start;
  1527. if (start + len > size)
  1528. len = size - start;
  1529. if (start < size)
  1530. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1531. len, flags);
  1532. if (ret == 1)
  1533. ret = 0;
  1534. } else {
  1535. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1536. gfs2_block_map);
  1537. }
  1538. gfs2_glock_dq_uninit(&gh);
  1539. out:
  1540. mutex_unlock(&inode->i_mutex);
  1541. return ret;
  1542. }
  1543. const struct inode_operations gfs2_file_iops = {
  1544. .permission = gfs2_permission,
  1545. .setattr = gfs2_setattr,
  1546. .getattr = gfs2_getattr,
  1547. .setxattr = gfs2_setxattr,
  1548. .getxattr = gfs2_getxattr,
  1549. .listxattr = gfs2_listxattr,
  1550. .removexattr = gfs2_removexattr,
  1551. .fiemap = gfs2_fiemap,
  1552. .get_acl = gfs2_get_acl,
  1553. };
  1554. const struct inode_operations gfs2_dir_iops = {
  1555. .create = gfs2_create,
  1556. .lookup = gfs2_lookup,
  1557. .link = gfs2_link,
  1558. .unlink = gfs2_unlink,
  1559. .symlink = gfs2_symlink,
  1560. .mkdir = gfs2_mkdir,
  1561. .rmdir = gfs2_unlink,
  1562. .mknod = gfs2_mknod,
  1563. .rename = gfs2_rename,
  1564. .permission = gfs2_permission,
  1565. .setattr = gfs2_setattr,
  1566. .getattr = gfs2_getattr,
  1567. .setxattr = gfs2_setxattr,
  1568. .getxattr = gfs2_getxattr,
  1569. .listxattr = gfs2_listxattr,
  1570. .removexattr = gfs2_removexattr,
  1571. .fiemap = gfs2_fiemap,
  1572. .get_acl = gfs2_get_acl,
  1573. };
  1574. const struct inode_operations gfs2_symlink_iops = {
  1575. .readlink = generic_readlink,
  1576. .follow_link = gfs2_follow_link,
  1577. .put_link = gfs2_put_link,
  1578. .permission = gfs2_permission,
  1579. .setattr = gfs2_setattr,
  1580. .getattr = gfs2_getattr,
  1581. .setxattr = gfs2_setxattr,
  1582. .getxattr = gfs2_getxattr,
  1583. .listxattr = gfs2_listxattr,
  1584. .removexattr = gfs2_removexattr,
  1585. .fiemap = gfs2_fiemap,
  1586. .get_acl = gfs2_get_acl,
  1587. };