PageRenderTime 81ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/gfs2/inode.c

https://bitbucket.org/emiliolopez/linux
C | 2122 lines | 1497 code | 317 blank | 308 comment | 349 complexity | a9c18c191abf3475edd375b11237bf98 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  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/cred.h>
  16. #include <linux/xattr.h>
  17. #include <linux/posix_acl.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/crc32.h>
  20. #include <linux/iomap.h>
  21. #include <linux/security.h>
  22. #include <linux/uaccess.h>
  23. #include "gfs2.h"
  24. #include "incore.h"
  25. #include "acl.h"
  26. #include "bmap.h"
  27. #include "dir.h"
  28. #include "xattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "quota.h"
  33. #include "rgrp.h"
  34. #include "trans.h"
  35. #include "util.h"
  36. #include "super.h"
  37. #include "glops.h"
  38. static int iget_test(struct inode *inode, void *opaque)
  39. {
  40. u64 no_addr = *(u64 *)opaque;
  41. return GFS2_I(inode)->i_no_addr == no_addr;
  42. }
  43. static int iget_set(struct inode *inode, void *opaque)
  44. {
  45. u64 no_addr = *(u64 *)opaque;
  46. GFS2_I(inode)->i_no_addr = no_addr;
  47. inode->i_ino = no_addr;
  48. return 0;
  49. }
  50. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  51. {
  52. struct inode *inode;
  53. repeat:
  54. inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
  55. if (!inode)
  56. return inode;
  57. if (is_bad_inode(inode)) {
  58. iput(inode);
  59. goto repeat;
  60. }
  61. return inode;
  62. }
  63. /**
  64. * gfs2_set_iop - Sets inode operations
  65. * @inode: The inode with correct i_mode filled in
  66. *
  67. * GFS2 lookup code fills in vfs inode contents based on info obtained
  68. * from directory entry inside gfs2_inode_lookup().
  69. */
  70. static void gfs2_set_iop(struct inode *inode)
  71. {
  72. struct gfs2_sbd *sdp = GFS2_SB(inode);
  73. umode_t mode = inode->i_mode;
  74. if (S_ISREG(mode)) {
  75. inode->i_op = &gfs2_file_iops;
  76. if (gfs2_localflocks(sdp))
  77. inode->i_fop = &gfs2_file_fops_nolock;
  78. else
  79. inode->i_fop = &gfs2_file_fops;
  80. } else if (S_ISDIR(mode)) {
  81. inode->i_op = &gfs2_dir_iops;
  82. if (gfs2_localflocks(sdp))
  83. inode->i_fop = &gfs2_dir_fops_nolock;
  84. else
  85. inode->i_fop = &gfs2_dir_fops;
  86. } else if (S_ISLNK(mode)) {
  87. inode->i_op = &gfs2_symlink_iops;
  88. } else {
  89. inode->i_op = &gfs2_file_iops;
  90. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  91. }
  92. }
  93. /**
  94. * gfs2_inode_lookup - Lookup an inode
  95. * @sb: The super block
  96. * @type: The type of the inode
  97. * @no_addr: The inode number
  98. * @no_formal_ino: The inode generation number
  99. * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
  100. * GFS2_BLKST_FREE to indicate not to verify)
  101. *
  102. * If @type is DT_UNKNOWN, the inode type is fetched from disk.
  103. *
  104. * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
  105. * placeholder because it doesn't otherwise make sense), the on-disk block type
  106. * is verified to be @blktype.
  107. *
  108. * Returns: A VFS inode, or an error
  109. */
  110. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  111. u64 no_addr, u64 no_formal_ino,
  112. unsigned int blktype)
  113. {
  114. struct inode *inode;
  115. struct gfs2_inode *ip;
  116. struct gfs2_glock *io_gl = NULL;
  117. struct gfs2_holder i_gh;
  118. int error;
  119. gfs2_holder_mark_uninitialized(&i_gh);
  120. inode = gfs2_iget(sb, no_addr);
  121. if (!inode)
  122. return ERR_PTR(-ENOMEM);
  123. ip = GFS2_I(inode);
  124. if (inode->i_state & I_NEW) {
  125. struct gfs2_sbd *sdp = GFS2_SB(inode);
  126. ip->i_no_formal_ino = no_formal_ino;
  127. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  128. if (unlikely(error))
  129. goto fail;
  130. flush_delayed_work(&ip->i_gl->gl_work);
  131. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  132. if (unlikely(error))
  133. goto fail_put;
  134. if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
  135. /*
  136. * The GL_SKIP flag indicates to skip reading the inode
  137. * block. We read the inode with gfs2_inode_refresh
  138. * after possibly checking the block type.
  139. */
  140. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
  141. GL_SKIP, &i_gh);
  142. if (error)
  143. goto fail_put;
  144. if (blktype != GFS2_BLKST_FREE) {
  145. error = gfs2_check_blk_type(sdp, no_addr,
  146. blktype);
  147. if (error)
  148. goto fail_put;
  149. }
  150. }
  151. glock_set_object(ip->i_gl, ip);
  152. set_bit(GIF_INVALID, &ip->i_flags);
  153. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  154. if (unlikely(error))
  155. goto fail_put;
  156. glock_set_object(ip->i_iopen_gh.gh_gl, ip);
  157. gfs2_glock_put(io_gl);
  158. io_gl = NULL;
  159. if (type == DT_UNKNOWN) {
  160. /* Inode glock must be locked already */
  161. error = gfs2_inode_refresh(GFS2_I(inode));
  162. if (error)
  163. goto fail_refresh;
  164. } else {
  165. inode->i_mode = DT2IF(type);
  166. }
  167. gfs2_set_iop(inode);
  168. /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
  169. inode->i_atime.tv_sec = 1LL << (8 * sizeof(inode->i_atime.tv_sec) - 1);
  170. inode->i_atime.tv_nsec = 0;
  171. unlock_new_inode(inode);
  172. }
  173. if (gfs2_holder_initialized(&i_gh))
  174. gfs2_glock_dq_uninit(&i_gh);
  175. return inode;
  176. fail_refresh:
  177. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  178. glock_clear_object(ip->i_iopen_gh.gh_gl, ip);
  179. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  180. fail_put:
  181. if (io_gl)
  182. gfs2_glock_put(io_gl);
  183. glock_clear_object(ip->i_gl, ip);
  184. if (gfs2_holder_initialized(&i_gh))
  185. gfs2_glock_dq_uninit(&i_gh);
  186. fail:
  187. iget_failed(inode);
  188. return ERR_PTR(error);
  189. }
  190. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  191. u64 *no_formal_ino, unsigned int blktype)
  192. {
  193. struct super_block *sb = sdp->sd_vfs;
  194. struct inode *inode;
  195. int error;
  196. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, blktype);
  197. if (IS_ERR(inode))
  198. return inode;
  199. /* Two extra checks for NFS only */
  200. if (no_formal_ino) {
  201. error = -ESTALE;
  202. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  203. goto fail_iput;
  204. error = -EIO;
  205. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  206. goto fail_iput;
  207. }
  208. return inode;
  209. fail_iput:
  210. iput(inode);
  211. return ERR_PTR(error);
  212. }
  213. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  214. {
  215. struct qstr qstr;
  216. struct inode *inode;
  217. gfs2_str2qstr(&qstr, name);
  218. inode = gfs2_lookupi(dip, &qstr, 1);
  219. /* gfs2_lookupi has inconsistent callers: vfs
  220. * related routines expect NULL for no entry found,
  221. * gfs2_lookup_simple callers expect ENOENT
  222. * and do not check for NULL.
  223. */
  224. if (inode == NULL)
  225. return ERR_PTR(-ENOENT);
  226. else
  227. return inode;
  228. }
  229. /**
  230. * gfs2_lookupi - Look up a filename in a directory and return its inode
  231. * @d_gh: An initialized holder for the directory glock
  232. * @name: The name of the inode to look for
  233. * @is_root: If 1, ignore the caller's permissions
  234. * @i_gh: An uninitialized holder for the new inode glock
  235. *
  236. * This can be called via the VFS filldir function when NFS is doing
  237. * a readdirplus and the inode which its intending to stat isn't
  238. * already in cache. In this case we must not take the directory glock
  239. * again, since the readdir call will have already taken that lock.
  240. *
  241. * Returns: errno
  242. */
  243. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  244. int is_root)
  245. {
  246. struct super_block *sb = dir->i_sb;
  247. struct gfs2_inode *dip = GFS2_I(dir);
  248. struct gfs2_holder d_gh;
  249. int error = 0;
  250. struct inode *inode = NULL;
  251. gfs2_holder_mark_uninitialized(&d_gh);
  252. if (!name->len || name->len > GFS2_FNAMESIZE)
  253. return ERR_PTR(-ENAMETOOLONG);
  254. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  255. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  256. dir == d_inode(sb->s_root))) {
  257. igrab(dir);
  258. return dir;
  259. }
  260. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  261. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  262. if (error)
  263. return ERR_PTR(error);
  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, false);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (gfs2_holder_initialized(&d_gh))
  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. if (dip->i_entries == (u32)-1)
  299. return -EFBIG;
  300. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  301. return -EMLINK;
  302. return 0;
  303. }
  304. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  305. struct inode *inode)
  306. {
  307. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  308. (dip->i_inode.i_mode & S_ISUID) &&
  309. !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
  310. if (S_ISDIR(inode->i_mode))
  311. inode->i_mode |= S_ISUID;
  312. else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
  313. inode->i_mode &= ~07111;
  314. inode->i_uid = dip->i_inode.i_uid;
  315. } else
  316. inode->i_uid = current_fsuid();
  317. if (dip->i_inode.i_mode & S_ISGID) {
  318. if (S_ISDIR(inode->i_mode))
  319. inode->i_mode |= S_ISGID;
  320. inode->i_gid = dip->i_inode.i_gid;
  321. } else
  322. inode->i_gid = current_fsgid();
  323. }
  324. static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
  325. {
  326. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  327. struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
  328. int error;
  329. error = gfs2_quota_lock_check(ip, &ap);
  330. if (error)
  331. goto out;
  332. error = gfs2_inplace_reserve(ip, &ap);
  333. if (error)
  334. goto out_quota;
  335. error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
  336. if (error)
  337. goto out_ipreserv;
  338. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
  339. ip->i_no_formal_ino = ip->i_generation;
  340. ip->i_inode.i_ino = ip->i_no_addr;
  341. ip->i_goal = ip->i_no_addr;
  342. gfs2_trans_end(sdp);
  343. out_ipreserv:
  344. gfs2_inplace_release(ip);
  345. out_quota:
  346. gfs2_quota_unlock(ip);
  347. out:
  348. return error;
  349. }
  350. static void gfs2_init_dir(struct buffer_head *dibh,
  351. const struct gfs2_inode *parent)
  352. {
  353. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  354. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  355. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  356. dent->de_inum = di->di_num; /* already GFS2 endian */
  357. dent->de_type = cpu_to_be16(DT_DIR);
  358. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  359. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  360. gfs2_inum_out(parent, dent);
  361. dent->de_type = cpu_to_be16(DT_DIR);
  362. }
  363. /**
  364. * gfs2_init_xattr - Initialise an xattr block for a new inode
  365. * @ip: The inode in question
  366. *
  367. * This sets up an empty xattr block for a new inode, ready to
  368. * take any ACLs, LSM xattrs, etc.
  369. */
  370. static void gfs2_init_xattr(struct gfs2_inode *ip)
  371. {
  372. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  373. struct buffer_head *bh;
  374. struct gfs2_ea_header *ea;
  375. bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
  376. gfs2_trans_add_meta(ip->i_gl, bh);
  377. gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  378. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  379. ea = GFS2_EA_BH2FIRST(bh);
  380. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  381. ea->ea_type = GFS2_EATYPE_UNUSED;
  382. ea->ea_flags = GFS2_EAFLAG_LAST;
  383. brelse(bh);
  384. }
  385. /**
  386. * init_dinode - Fill in a new dinode structure
  387. * @dip: The directory this inode is being created in
  388. * @ip: The inode
  389. * @symname: The symlink destination (if a symlink)
  390. * @bhp: The buffer head (returned to caller)
  391. *
  392. */
  393. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  394. const char *symname)
  395. {
  396. struct gfs2_dinode *di;
  397. struct buffer_head *dibh;
  398. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  399. gfs2_trans_add_meta(ip->i_gl, dibh);
  400. di = (struct gfs2_dinode *)dibh->b_data;
  401. gfs2_dinode_out(ip, di);
  402. di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
  403. di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
  404. di->__pad1 = 0;
  405. di->__pad2 = 0;
  406. di->__pad3 = 0;
  407. memset(&di->__pad4, 0, sizeof(di->__pad4));
  408. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  409. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  410. switch(ip->i_inode.i_mode & S_IFMT) {
  411. case S_IFDIR:
  412. gfs2_init_dir(dibh, dip);
  413. break;
  414. case S_IFLNK:
  415. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  416. break;
  417. }
  418. set_buffer_uptodate(dibh);
  419. brelse(dibh);
  420. }
  421. /**
  422. * gfs2_trans_da_blocks - Calculate number of blocks to link inode
  423. * @dip: The directory we are linking into
  424. * @da: The dir add information
  425. * @nr_inodes: The number of inodes involved
  426. *
  427. * This calculate the number of blocks we need to reserve in a
  428. * transaction to link @nr_inodes into a directory. In most cases
  429. * @nr_inodes will be 2 (the directory plus the inode being linked in)
  430. * but in case of rename, 4 may be required.
  431. *
  432. * Returns: Number of blocks
  433. */
  434. static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
  435. const struct gfs2_diradd *da,
  436. unsigned nr_inodes)
  437. {
  438. return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
  439. (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
  440. }
  441. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  442. struct gfs2_inode *ip, struct gfs2_diradd *da)
  443. {
  444. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  445. struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
  446. int error;
  447. if (da->nr_blocks) {
  448. error = gfs2_quota_lock_check(dip, &ap);
  449. if (error)
  450. goto fail_quota_locks;
  451. error = gfs2_inplace_reserve(dip, &ap);
  452. if (error)
  453. goto fail_quota_locks;
  454. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
  455. if (error)
  456. goto fail_ipreserv;
  457. } else {
  458. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  459. if (error)
  460. goto fail_quota_locks;
  461. }
  462. error = gfs2_dir_add(&dip->i_inode, name, ip, da);
  463. gfs2_trans_end(sdp);
  464. fail_ipreserv:
  465. gfs2_inplace_release(dip);
  466. fail_quota_locks:
  467. gfs2_quota_unlock(dip);
  468. return error;
  469. }
  470. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  471. void *fs_info)
  472. {
  473. const struct xattr *xattr;
  474. int err = 0;
  475. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  476. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  477. xattr->value_len, 0,
  478. GFS2_EATYPE_SECURITY);
  479. if (err < 0)
  480. break;
  481. }
  482. return err;
  483. }
  484. /**
  485. * gfs2_create_inode - Create a new inode
  486. * @dir: The parent directory
  487. * @dentry: The new dentry
  488. * @file: If non-NULL, the file which is being opened
  489. * @mode: The permissions on the new inode
  490. * @dev: For device nodes, this is the device number
  491. * @symname: For symlinks, this is the link destination
  492. * @size: The initial size of the inode (ignored for directories)
  493. *
  494. * Returns: 0 on success, or error code
  495. */
  496. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  497. struct file *file,
  498. umode_t mode, dev_t dev, const char *symname,
  499. unsigned int size, int excl, int *opened)
  500. {
  501. const struct qstr *name = &dentry->d_name;
  502. struct posix_acl *default_acl, *acl;
  503. struct gfs2_holder ghs[2];
  504. struct inode *inode = NULL;
  505. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  506. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  507. struct gfs2_glock *io_gl = NULL;
  508. int error, free_vfs_inode = 1;
  509. u32 aflags = 0;
  510. unsigned blocks = 1;
  511. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  512. if (!name->len || name->len > GFS2_FNAMESIZE)
  513. return -ENAMETOOLONG;
  514. error = gfs2_rsqa_alloc(dip);
  515. if (error)
  516. return error;
  517. error = gfs2_rindex_update(sdp);
  518. if (error)
  519. return error;
  520. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  521. if (error)
  522. goto fail;
  523. gfs2_holder_mark_uninitialized(ghs + 1);
  524. error = create_ok(dip, name, mode);
  525. if (error)
  526. goto fail_gunlock;
  527. inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
  528. error = PTR_ERR(inode);
  529. if (!IS_ERR(inode)) {
  530. if (S_ISDIR(inode->i_mode)) {
  531. iput(inode);
  532. inode = ERR_PTR(-EISDIR);
  533. goto fail_gunlock;
  534. }
  535. d_instantiate(dentry, inode);
  536. error = 0;
  537. if (file) {
  538. if (S_ISREG(inode->i_mode))
  539. error = finish_open(file, dentry, gfs2_open_common, opened);
  540. else
  541. error = finish_no_open(file, NULL);
  542. }
  543. gfs2_glock_dq_uninit(ghs);
  544. return error;
  545. } else if (error != -ENOENT) {
  546. goto fail_gunlock;
  547. }
  548. error = gfs2_diradd_alloc_required(dir, name, &da);
  549. if (error < 0)
  550. goto fail_gunlock;
  551. inode = new_inode(sdp->sd_vfs);
  552. error = -ENOMEM;
  553. if (!inode)
  554. goto fail_gunlock;
  555. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  556. if (error)
  557. goto fail_gunlock;
  558. ip = GFS2_I(inode);
  559. error = gfs2_rsqa_alloc(ip);
  560. if (error)
  561. goto fail_free_acls;
  562. inode->i_mode = mode;
  563. set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
  564. inode->i_rdev = dev;
  565. inode->i_size = size;
  566. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  567. gfs2_set_inode_blocks(inode, 1);
  568. munge_mode_uid_gid(dip, inode);
  569. check_and_update_goal(dip);
  570. ip->i_goal = dip->i_goal;
  571. ip->i_diskflags = 0;
  572. ip->i_eattr = 0;
  573. ip->i_height = 0;
  574. ip->i_depth = 0;
  575. ip->i_entries = 0;
  576. ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
  577. switch(mode & S_IFMT) {
  578. case S_IFREG:
  579. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  580. gfs2_tune_get(sdp, gt_new_files_jdata))
  581. ip->i_diskflags |= GFS2_DIF_JDATA;
  582. gfs2_set_aops(inode);
  583. break;
  584. case S_IFDIR:
  585. ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
  586. ip->i_diskflags |= GFS2_DIF_JDATA;
  587. ip->i_entries = 2;
  588. break;
  589. }
  590. /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
  591. if (dip->i_diskflags & GFS2_DIF_SYSTEM)
  592. ip->i_diskflags |= GFS2_DIF_SYSTEM;
  593. gfs2_set_inode_flags(inode);
  594. if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
  595. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  596. aflags |= GFS2_AF_ORLOV;
  597. if (default_acl || acl)
  598. blocks++;
  599. error = alloc_dinode(ip, aflags, &blocks);
  600. if (error)
  601. goto fail_free_inode;
  602. gfs2_set_inode_blocks(inode, blocks);
  603. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  604. if (error)
  605. goto fail_free_inode;
  606. flush_delayed_work(&ip->i_gl->gl_work);
  607. glock_set_object(ip->i_gl, ip);
  608. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  609. if (error)
  610. goto fail_free_inode;
  611. error = gfs2_trans_begin(sdp, blocks, 0);
  612. if (error)
  613. goto fail_gunlock2;
  614. if (blocks > 1) {
  615. ip->i_eattr = ip->i_no_addr + 1;
  616. gfs2_init_xattr(ip);
  617. }
  618. init_dinode(dip, ip, symname);
  619. gfs2_trans_end(sdp);
  620. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  621. if (error)
  622. goto fail_gunlock2;
  623. BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
  624. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  625. if (error)
  626. goto fail_gunlock2;
  627. glock_set_object(ip->i_iopen_gh.gh_gl, ip);
  628. gfs2_glock_put(io_gl);
  629. gfs2_set_iop(inode);
  630. insert_inode_hash(inode);
  631. free_vfs_inode = 0; /* After this point, the inode is no longer
  632. considered free. Any failures need to undo
  633. the gfs2 structures. */
  634. if (default_acl) {
  635. error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  636. posix_acl_release(default_acl);
  637. }
  638. if (acl) {
  639. if (!error)
  640. error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
  641. posix_acl_release(acl);
  642. }
  643. if (error)
  644. goto fail_gunlock3;
  645. error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
  646. &gfs2_initxattrs, NULL);
  647. if (error)
  648. goto fail_gunlock3;
  649. error = link_dinode(dip, name, ip, &da);
  650. if (error)
  651. goto fail_gunlock3;
  652. mark_inode_dirty(inode);
  653. d_instantiate(dentry, inode);
  654. if (file) {
  655. *opened |= FILE_CREATED;
  656. error = finish_open(file, dentry, gfs2_open_common, opened);
  657. }
  658. gfs2_glock_dq_uninit(ghs);
  659. gfs2_glock_dq_uninit(ghs + 1);
  660. clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
  661. return error;
  662. fail_gunlock3:
  663. glock_clear_object(io_gl, ip);
  664. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  665. gfs2_glock_put(io_gl);
  666. fail_gunlock2:
  667. if (io_gl)
  668. clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
  669. fail_free_inode:
  670. if (ip->i_gl) {
  671. glock_clear_object(ip->i_gl, ip);
  672. gfs2_glock_put(ip->i_gl);
  673. }
  674. gfs2_rsqa_delete(ip, NULL);
  675. fail_free_acls:
  676. if (default_acl)
  677. posix_acl_release(default_acl);
  678. if (acl)
  679. posix_acl_release(acl);
  680. fail_gunlock:
  681. gfs2_dir_no_add(&da);
  682. gfs2_glock_dq_uninit(ghs);
  683. if (inode && !IS_ERR(inode)) {
  684. clear_nlink(inode);
  685. if (!free_vfs_inode)
  686. mark_inode_dirty(inode);
  687. set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
  688. &GFS2_I(inode)->i_flags);
  689. iput(inode);
  690. }
  691. if (gfs2_holder_initialized(ghs + 1))
  692. gfs2_glock_dq_uninit(ghs + 1);
  693. fail:
  694. return error;
  695. }
  696. /**
  697. * gfs2_create - Create a file
  698. * @dir: The directory in which to create the file
  699. * @dentry: The dentry of the new file
  700. * @mode: The mode of the new file
  701. *
  702. * Returns: errno
  703. */
  704. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  705. umode_t mode, bool excl)
  706. {
  707. return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl, NULL);
  708. }
  709. /**
  710. * __gfs2_lookup - Look up a filename in a directory and return its inode
  711. * @dir: The directory inode
  712. * @dentry: The dentry of the new inode
  713. * @file: File to be opened
  714. * @opened: atomic_open flags
  715. *
  716. *
  717. * Returns: errno
  718. */
  719. static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
  720. struct file *file, int *opened)
  721. {
  722. struct inode *inode;
  723. struct dentry *d;
  724. struct gfs2_holder gh;
  725. struct gfs2_glock *gl;
  726. int error;
  727. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  728. if (inode == NULL) {
  729. d_add(dentry, NULL);
  730. return NULL;
  731. }
  732. if (IS_ERR(inode))
  733. return ERR_CAST(inode);
  734. gl = GFS2_I(inode)->i_gl;
  735. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  736. if (error) {
  737. iput(inode);
  738. return ERR_PTR(error);
  739. }
  740. d = d_splice_alias(inode, dentry);
  741. if (IS_ERR(d)) {
  742. gfs2_glock_dq_uninit(&gh);
  743. return d;
  744. }
  745. if (file && S_ISREG(inode->i_mode))
  746. error = finish_open(file, dentry, gfs2_open_common, opened);
  747. gfs2_glock_dq_uninit(&gh);
  748. if (error) {
  749. dput(d);
  750. return ERR_PTR(error);
  751. }
  752. return d;
  753. }
  754. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  755. unsigned flags)
  756. {
  757. return __gfs2_lookup(dir, dentry, NULL, NULL);
  758. }
  759. /**
  760. * gfs2_link - Link to a file
  761. * @old_dentry: The inode to link
  762. * @dir: Add link to this directory
  763. * @dentry: The name of the link
  764. *
  765. * Link the inode in "old_dentry" into the directory "dir" with the
  766. * name in "dentry".
  767. *
  768. * Returns: errno
  769. */
  770. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  771. struct dentry *dentry)
  772. {
  773. struct gfs2_inode *dip = GFS2_I(dir);
  774. struct gfs2_sbd *sdp = GFS2_SB(dir);
  775. struct inode *inode = d_inode(old_dentry);
  776. struct gfs2_inode *ip = GFS2_I(inode);
  777. struct gfs2_holder ghs[2];
  778. struct buffer_head *dibh;
  779. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  780. int error;
  781. if (S_ISDIR(inode->i_mode))
  782. return -EPERM;
  783. error = gfs2_rsqa_alloc(dip);
  784. if (error)
  785. return error;
  786. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  787. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  788. error = gfs2_glock_nq(ghs); /* parent */
  789. if (error)
  790. goto out_parent;
  791. error = gfs2_glock_nq(ghs + 1); /* child */
  792. if (error)
  793. goto out_child;
  794. error = -ENOENT;
  795. if (inode->i_nlink == 0)
  796. goto out_gunlock;
  797. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  798. if (error)
  799. goto out_gunlock;
  800. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  801. switch (error) {
  802. case -ENOENT:
  803. break;
  804. case 0:
  805. error = -EEXIST;
  806. default:
  807. goto out_gunlock;
  808. }
  809. error = -EINVAL;
  810. if (!dip->i_inode.i_nlink)
  811. goto out_gunlock;
  812. error = -EFBIG;
  813. if (dip->i_entries == (u32)-1)
  814. goto out_gunlock;
  815. error = -EPERM;
  816. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  817. goto out_gunlock;
  818. error = -EINVAL;
  819. if (!ip->i_inode.i_nlink)
  820. goto out_gunlock;
  821. error = -EMLINK;
  822. if (ip->i_inode.i_nlink == (u32)-1)
  823. goto out_gunlock;
  824. error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
  825. if (error < 0)
  826. goto out_gunlock;
  827. if (da.nr_blocks) {
  828. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  829. error = gfs2_quota_lock_check(dip, &ap);
  830. if (error)
  831. goto out_gunlock;
  832. error = gfs2_inplace_reserve(dip, &ap);
  833. if (error)
  834. goto out_gunlock_q;
  835. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
  836. if (error)
  837. goto out_ipres;
  838. } else {
  839. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  840. if (error)
  841. goto out_ipres;
  842. }
  843. error = gfs2_meta_inode_buffer(ip, &dibh);
  844. if (error)
  845. goto out_end_trans;
  846. error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
  847. if (error)
  848. goto out_brelse;
  849. gfs2_trans_add_meta(ip->i_gl, dibh);
  850. inc_nlink(&ip->i_inode);
  851. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  852. ihold(inode);
  853. d_instantiate(dentry, inode);
  854. mark_inode_dirty(inode);
  855. out_brelse:
  856. brelse(dibh);
  857. out_end_trans:
  858. gfs2_trans_end(sdp);
  859. out_ipres:
  860. if (da.nr_blocks)
  861. gfs2_inplace_release(dip);
  862. out_gunlock_q:
  863. if (da.nr_blocks)
  864. gfs2_quota_unlock(dip);
  865. out_gunlock:
  866. gfs2_dir_no_add(&da);
  867. gfs2_glock_dq(ghs + 1);
  868. out_child:
  869. gfs2_glock_dq(ghs);
  870. out_parent:
  871. gfs2_holder_uninit(ghs);
  872. gfs2_holder_uninit(ghs + 1);
  873. return error;
  874. }
  875. /*
  876. * gfs2_unlink_ok - check to see that a inode is still in a directory
  877. * @dip: the directory
  878. * @name: the name of the file
  879. * @ip: the inode
  880. *
  881. * Assumes that the lock on (at least) @dip is held.
  882. *
  883. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  884. */
  885. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  886. const struct gfs2_inode *ip)
  887. {
  888. int error;
  889. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  890. return -EPERM;
  891. if ((dip->i_inode.i_mode & S_ISVTX) &&
  892. !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
  893. !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
  894. return -EPERM;
  895. if (IS_APPEND(&dip->i_inode))
  896. return -EPERM;
  897. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  898. if (error)
  899. return error;
  900. return gfs2_dir_check(&dip->i_inode, name, ip);
  901. }
  902. /**
  903. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  904. * @dip: The parent directory
  905. * @name: The name of the entry in the parent directory
  906. * @inode: The inode to be removed
  907. *
  908. * Called with all the locks and in a transaction. This will only be
  909. * called for a directory after it has been checked to ensure it is empty.
  910. *
  911. * Returns: 0 on success, or an error
  912. */
  913. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  914. const struct dentry *dentry)
  915. {
  916. struct inode *inode = d_inode(dentry);
  917. struct gfs2_inode *ip = GFS2_I(inode);
  918. int error;
  919. error = gfs2_dir_del(dip, dentry);
  920. if (error)
  921. return error;
  922. ip->i_entries = 0;
  923. inode->i_ctime = current_time(inode);
  924. if (S_ISDIR(inode->i_mode))
  925. clear_nlink(inode);
  926. else
  927. drop_nlink(inode);
  928. mark_inode_dirty(inode);
  929. if (inode->i_nlink == 0)
  930. gfs2_unlink_di(inode);
  931. return 0;
  932. }
  933. /**
  934. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  935. * @dir: The inode of the directory containing the inode to unlink
  936. * @dentry: The file itself
  937. *
  938. * This routine uses the type of the inode as a flag to figure out
  939. * whether this is an unlink or an rmdir.
  940. *
  941. * Returns: errno
  942. */
  943. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  944. {
  945. struct gfs2_inode *dip = GFS2_I(dir);
  946. struct gfs2_sbd *sdp = GFS2_SB(dir);
  947. struct inode *inode = d_inode(dentry);
  948. struct gfs2_inode *ip = GFS2_I(inode);
  949. struct gfs2_holder ghs[3];
  950. struct gfs2_rgrpd *rgd;
  951. int error;
  952. error = gfs2_rindex_update(sdp);
  953. if (error)
  954. return error;
  955. error = -EROFS;
  956. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  957. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  958. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  959. if (!rgd)
  960. goto out_inodes;
  961. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  962. error = gfs2_glock_nq(ghs); /* parent */
  963. if (error)
  964. goto out_parent;
  965. error = gfs2_glock_nq(ghs + 1); /* child */
  966. if (error)
  967. goto out_child;
  968. error = -ENOENT;
  969. if (inode->i_nlink == 0)
  970. goto out_rgrp;
  971. if (S_ISDIR(inode->i_mode)) {
  972. error = -ENOTEMPTY;
  973. if (ip->i_entries > 2 || inode->i_nlink > 2)
  974. goto out_rgrp;
  975. }
  976. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  977. if (error)
  978. goto out_rgrp;
  979. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  980. if (error)
  981. goto out_gunlock;
  982. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  983. if (error)
  984. goto out_end_trans;
  985. error = gfs2_unlink_inode(dip, dentry);
  986. out_end_trans:
  987. gfs2_trans_end(sdp);
  988. out_gunlock:
  989. gfs2_glock_dq(ghs + 2);
  990. out_rgrp:
  991. gfs2_glock_dq(ghs + 1);
  992. out_child:
  993. gfs2_glock_dq(ghs);
  994. out_parent:
  995. gfs2_holder_uninit(ghs + 2);
  996. out_inodes:
  997. gfs2_holder_uninit(ghs + 1);
  998. gfs2_holder_uninit(ghs);
  999. return error;
  1000. }
  1001. /**
  1002. * gfs2_symlink - Create a symlink
  1003. * @dir: The directory to create the symlink in
  1004. * @dentry: The dentry to put the symlink in
  1005. * @symname: The thing which the link points to
  1006. *
  1007. * Returns: errno
  1008. */
  1009. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  1010. const char *symname)
  1011. {
  1012. struct gfs2_sbd *sdp = GFS2_SB(dir);
  1013. unsigned int size;
  1014. size = strlen(symname);
  1015. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  1016. return -ENAMETOOLONG;
  1017. return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0, NULL);
  1018. }
  1019. /**
  1020. * gfs2_mkdir - Make a directory
  1021. * @dir: The parent directory of the new one
  1022. * @dentry: The dentry of the new directory
  1023. * @mode: The mode of the new directory
  1024. *
  1025. * Returns: errno
  1026. */
  1027. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1028. {
  1029. struct gfs2_sbd *sdp = GFS2_SB(dir);
  1030. unsigned dsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  1031. return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0, NULL);
  1032. }
  1033. /**
  1034. * gfs2_mknod - Make a special file
  1035. * @dir: The directory in which the special file will reside
  1036. * @dentry: The dentry of the special file
  1037. * @mode: The mode of the special file
  1038. * @dev: The device specification of the special file
  1039. *
  1040. */
  1041. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  1042. dev_t dev)
  1043. {
  1044. return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0, NULL);
  1045. }
  1046. /**
  1047. * gfs2_atomic_open - Atomically open a file
  1048. * @dir: The directory
  1049. * @dentry: The proposed new entry
  1050. * @file: The proposed new struct file
  1051. * @flags: open flags
  1052. * @mode: File mode
  1053. * @opened: Flag to say whether the file has been opened or not
  1054. *
  1055. * Returns: error code or 0 for success
  1056. */
  1057. static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
  1058. struct file *file, unsigned flags,
  1059. umode_t mode, int *opened)
  1060. {
  1061. struct dentry *d;
  1062. bool excl = !!(flags & O_EXCL);
  1063. if (!d_in_lookup(dentry))
  1064. goto skip_lookup;
  1065. d = __gfs2_lookup(dir, dentry, file, opened);
  1066. if (IS_ERR(d))
  1067. return PTR_ERR(d);
  1068. if (d != NULL)
  1069. dentry = d;
  1070. if (d_really_is_positive(dentry)) {
  1071. if (!(*opened & FILE_OPENED))
  1072. return finish_no_open(file, d);
  1073. dput(d);
  1074. return 0;
  1075. }
  1076. BUG_ON(d != NULL);
  1077. skip_lookup:
  1078. if (!(flags & O_CREAT))
  1079. return -ENOENT;
  1080. return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl, opened);
  1081. }
  1082. /*
  1083. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  1084. * @this: move this
  1085. * @to: to here
  1086. *
  1087. * Follow @to back to the root and make sure we don't encounter @this
  1088. * Assumes we already hold the rename lock.
  1089. *
  1090. * Returns: errno
  1091. */
  1092. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1093. {
  1094. struct inode *dir = &to->i_inode;
  1095. struct super_block *sb = dir->i_sb;
  1096. struct inode *tmp;
  1097. int error = 0;
  1098. igrab(dir);
  1099. for (;;) {
  1100. if (dir == &this->i_inode) {
  1101. error = -EINVAL;
  1102. break;
  1103. }
  1104. if (dir == d_inode(sb->s_root)) {
  1105. error = 0;
  1106. break;
  1107. }
  1108. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1109. if (!tmp) {
  1110. error = -ENOENT;
  1111. break;
  1112. }
  1113. if (IS_ERR(tmp)) {
  1114. error = PTR_ERR(tmp);
  1115. break;
  1116. }
  1117. iput(dir);
  1118. dir = tmp;
  1119. }
  1120. iput(dir);
  1121. return error;
  1122. }
  1123. /**
  1124. * update_moved_ino - Update an inode that's being moved
  1125. * @ip: The inode being moved
  1126. * @ndip: The parent directory of the new filename
  1127. * @dir_rename: True of ip is a directory
  1128. *
  1129. * Returns: errno
  1130. */
  1131. static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
  1132. int dir_rename)
  1133. {
  1134. int error;
  1135. struct buffer_head *dibh;
  1136. if (dir_rename)
  1137. return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1138. error = gfs2_meta_inode_buffer(ip, &dibh);
  1139. if (error)
  1140. return error;
  1141. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1142. gfs2_trans_add_meta(ip->i_gl, dibh);
  1143. gfs2_dinode_out(ip, dibh->b_data);
  1144. brelse(dibh);
  1145. return 0;
  1146. }
  1147. /**
  1148. * gfs2_rename - Rename a file
  1149. * @odir: Parent directory of old file name
  1150. * @odentry: The old dentry of the file
  1151. * @ndir: Parent directory of new file name
  1152. * @ndentry: The new dentry of the file
  1153. *
  1154. * Returns: errno
  1155. */
  1156. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1157. struct inode *ndir, struct dentry *ndentry)
  1158. {
  1159. struct gfs2_inode *odip = GFS2_I(odir);
  1160. struct gfs2_inode *ndip = GFS2_I(ndir);
  1161. struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
  1162. struct gfs2_inode *nip = NULL;
  1163. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1164. struct gfs2_holder ghs[5], r_gh;
  1165. struct gfs2_rgrpd *nrgd;
  1166. unsigned int num_gh;
  1167. int dir_rename = 0;
  1168. struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
  1169. unsigned int x;
  1170. int error;
  1171. gfs2_holder_mark_uninitialized(&r_gh);
  1172. if (d_really_is_positive(ndentry)) {
  1173. nip = GFS2_I(d_inode(ndentry));
  1174. if (ip == nip)
  1175. return 0;
  1176. }
  1177. error = gfs2_rindex_update(sdp);
  1178. if (error)
  1179. return error;
  1180. error = gfs2_rsqa_alloc(ndip);
  1181. if (error)
  1182. return error;
  1183. if (odip != ndip) {
  1184. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1185. 0, &r_gh);
  1186. if (error)
  1187. goto out;
  1188. if (S_ISDIR(ip->i_inode.i_mode)) {
  1189. dir_rename = 1;
  1190. /* don't move a directory into its subdir */
  1191. error = gfs2_ok_to_move(ip, ndip);
  1192. if (error)
  1193. goto out_gunlock_r;
  1194. }
  1195. }
  1196. num_gh = 1;
  1197. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1198. if (odip != ndip) {
  1199. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1200. num_gh++;
  1201. }
  1202. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1203. num_gh++;
  1204. if (nip) {
  1205. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1206. num_gh++;
  1207. /* grab the resource lock for unlink flag twiddling
  1208. * this is the case of the target file already existing
  1209. * so we unlink before doing the rename
  1210. */
  1211. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1212. if (nrgd)
  1213. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1214. }
  1215. for (x = 0; x < num_gh; x++) {
  1216. error = gfs2_glock_nq(ghs + x);
  1217. if (error)
  1218. goto out_gunlock;
  1219. }
  1220. error = -ENOENT;
  1221. if (ip->i_inode.i_nlink == 0)
  1222. goto out_gunlock;
  1223. /* Check out the old directory */
  1224. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1225. if (error)
  1226. goto out_gunlock;
  1227. /* Check out the new directory */
  1228. if (nip) {
  1229. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1230. if (error)
  1231. goto out_gunlock;
  1232. if (nip->i_inode.i_nlink == 0) {
  1233. error = -EAGAIN;
  1234. goto out_gunlock;
  1235. }
  1236. if (S_ISDIR(nip->i_inode.i_mode)) {
  1237. if (nip->i_entries < 2) {
  1238. gfs2_consist_inode(nip);
  1239. error = -EIO;
  1240. goto out_gunlock;
  1241. }
  1242. if (nip->i_entries > 2) {
  1243. error = -ENOTEMPTY;
  1244. goto out_gunlock;
  1245. }
  1246. }
  1247. } else {
  1248. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1249. if (error)
  1250. goto out_gunlock;
  1251. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1252. switch (error) {
  1253. case -ENOENT:
  1254. error = 0;
  1255. break;
  1256. case 0:
  1257. error = -EEXIST;
  1258. default:
  1259. goto out_gunlock;
  1260. };
  1261. if (odip != ndip) {
  1262. if (!ndip->i_inode.i_nlink) {
  1263. error = -ENOENT;
  1264. goto out_gunlock;
  1265. }
  1266. if (ndip->i_entries == (u32)-1) {
  1267. error = -EFBIG;
  1268. goto out_gunlock;
  1269. }
  1270. if (S_ISDIR(ip->i_inode.i_mode) &&
  1271. ndip->i_inode.i_nlink == (u32)-1) {
  1272. error = -EMLINK;
  1273. goto out_gunlock;
  1274. }
  1275. }
  1276. }
  1277. /* Check out the dir to be renamed */
  1278. if (dir_rename) {
  1279. error = gfs2_permission(d_inode(odentry), MAY_WRITE);
  1280. if (error)
  1281. goto out_gunlock;
  1282. }
  1283. if (nip == NULL) {
  1284. error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
  1285. if (error)
  1286. goto out_gunlock;
  1287. }
  1288. if (da.nr_blocks) {
  1289. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  1290. error = gfs2_quota_lock_check(ndip, &ap);
  1291. if (error)
  1292. goto out_gunlock;
  1293. error = gfs2_inplace_reserve(ndip, &ap);
  1294. if (error)
  1295. goto out_gunlock_q;
  1296. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
  1297. 4 * RES_LEAF + 4, 0);
  1298. if (error)
  1299. goto out_ipreserv;
  1300. } else {
  1301. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1302. 5 * RES_LEAF + 4, 0);
  1303. if (error)
  1304. goto out_gunlock;
  1305. }
  1306. /* Remove the target file, if it exists */
  1307. if (nip)
  1308. error = gfs2_unlink_inode(ndip, ndentry);
  1309. error = update_moved_ino(ip, ndip, dir_rename);
  1310. if (error)
  1311. goto out_end_trans;
  1312. error = gfs2_dir_del(odip, odentry);
  1313. if (error)
  1314. goto out_end_trans;
  1315. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
  1316. if (error)
  1317. goto out_end_trans;
  1318. out_end_trans:
  1319. gfs2_trans_end(sdp);
  1320. out_ipreserv:
  1321. if (da.nr_blocks)
  1322. gfs2_inplace_release(ndip);
  1323. out_gunlock_q:
  1324. if (da.nr_blocks)
  1325. gfs2_quota_unlock(ndip);
  1326. out_gunlock:
  1327. gfs2_dir_no_add(&da);
  1328. while (x--) {
  1329. gfs2_glock_dq(ghs + x);
  1330. gfs2_holder_uninit(ghs + x);
  1331. }
  1332. out_gunlock_r:
  1333. if (gfs2_holder_initialized(&r_gh))
  1334. gfs2_glock_dq_uninit(&r_gh);
  1335. out:
  1336. return error;
  1337. }
  1338. /**
  1339. * gfs2_exchange - exchange two files
  1340. * @odir: Parent directory of old file name
  1341. * @odentry: The old dentry of the file
  1342. * @ndir: Parent directory of new file name
  1343. * @ndentry: The new dentry of the file
  1344. * @flags: The rename flags
  1345. *
  1346. * Returns: errno
  1347. */
  1348. static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
  1349. struct inode *ndir, struct dentry *ndentry,
  1350. unsigned int flags)
  1351. {
  1352. struct gfs2_inode *odip = GFS2_I(odir);
  1353. struct gfs2_inode *ndip = GFS2_I(ndir);
  1354. struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
  1355. struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
  1356. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1357. struct gfs2_holder ghs[5], r_gh;
  1358. unsigned int num_gh;
  1359. unsigned int x;
  1360. umode_t old_mode = oip->i_inode.i_mode;
  1361. umode_t new_mode = nip->i_inode.i_mode;
  1362. int error;
  1363. gfs2_holder_mark_uninitialized(&r_gh);
  1364. error = gfs2_rindex_update(sdp);
  1365. if (error)
  1366. return error;
  1367. if (odip != ndip) {
  1368. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1369. 0, &r_gh);
  1370. if (error)
  1371. goto out;
  1372. if (S_ISDIR(old_mode)) {
  1373. /* don't move a directory into its subdir */
  1374. error = gfs2_ok_to_move(oip, ndip);
  1375. if (error)
  1376. goto out_gunlock_r;
  1377. }
  1378. if (S_ISDIR(new_mode)) {
  1379. /* don't move a directory into its subdir */
  1380. error = gfs2_ok_to_move(nip, odip);
  1381. if (error)
  1382. goto out_gunlock_r;
  1383. }
  1384. }
  1385. num_gh = 1;
  1386. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1387. if (odip != ndip) {
  1388. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1389. num_gh++;
  1390. }
  1391. gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1392. num_gh++;
  1393. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1394. num_gh++;
  1395. for (x = 0; x < num_gh; x++) {
  1396. error = gfs2_glock_nq(ghs + x);
  1397. if (error)
  1398. goto out_gunlock;
  1399. }
  1400. error = -ENOENT;
  1401. if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
  1402. goto out_gunlock;
  1403. error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
  1404. if (error)
  1405. goto out_gunlock;
  1406. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1407. if (error)
  1408. goto out_gunlock;
  1409. if (S_ISDIR(old_mode)) {
  1410. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1411. if (error)
  1412. goto out_gunlock;
  1413. }
  1414. if (S_ISDIR(new_mode)) {
  1415. error = gfs2_permission(ndentry->d_inode, MAY_WRITE);
  1416. if (error)
  1417. goto out_gunlock;
  1418. }
  1419. error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
  1420. if (error)
  1421. goto out_gunlock;
  1422. error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
  1423. if (error)
  1424. goto out_end_trans;
  1425. error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
  1426. if (error)
  1427. goto out_end_trans;
  1428. error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
  1429. IF2DT(old_mode));
  1430. if (error)
  1431. goto out_end_trans;
  1432. error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
  1433. IF2DT(new_mode));
  1434. if (error)
  1435. goto out_end_trans;
  1436. if (odip != ndip) {
  1437. if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
  1438. inc_nlink(&odip->i_inode);
  1439. drop_nlink(&ndip->i_inode);
  1440. } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
  1441. inc_nlink(&ndip->i_inode);
  1442. drop_nlink(&odip->i_inode);
  1443. }
  1444. }
  1445. mark_inode_dirty(&ndip->i_inode);
  1446. if (odip != ndip)
  1447. mark_inode_dirty(&odip->i_inode);
  1448. out_end_trans:
  1449. gfs2_trans_end(sdp);
  1450. out_gunlock:
  1451. while (x--) {
  1452. gfs2_glock_dq(ghs + x);
  1453. gfs2_holder_uninit(ghs + x);
  1454. }
  1455. out_gunlock_r:
  1456. if (gfs2_holder_initialized(&r_gh))
  1457. gfs2_glock_dq_uninit(&r_gh);
  1458. out:
  1459. return error;
  1460. }
  1461. static int gfs2_rename2(struct inode *odir, struct dentry *odentry,
  1462. struct inode *ndir, struct dentry *ndentry,
  1463. unsigned int flags)
  1464. {
  1465. flags &= ~RENAME_NOREPLACE;
  1466. if (flags & ~RENAME_EXCHANGE)
  1467. return -EINVAL;
  1468. if (flags & RENAME_EXCHANGE)
  1469. return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
  1470. return gfs2_rename(odir, odentry, ndir, ndentry);
  1471. }
  1472. /**
  1473. * gfs2_get_link - Follow a symbolic link
  1474. * @dentry: The dentry of the link
  1475. * @inode: The inode of the link
  1476. * @done: destructor for return value
  1477. *
  1478. * This can handle symlinks of any size.
  1479. *
  1480. * Returns: 0 on success or error code
  1481. */
  1482. static const char *gfs2_get_link(struct dentry *dentry,
  1483. struct inode *inode,
  1484. struct delayed_call *done)
  1485. {
  1486. struct gfs2_inode *ip = GFS2_I(inode);
  1487. struct gfs2_holder i_gh;
  1488. struct buffer_head *dibh;
  1489. unsigned int size;
  1490. char *buf;
  1491. int error;
  1492. if (!dentry)
  1493. return ERR_PTR(-ECHILD);
  1494. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1495. error = gfs2_glock_nq(&i_gh);
  1496. if (error) {
  1497. gfs2_holder_uninit(&i_gh);
  1498. return ERR_PTR(error);
  1499. }
  1500. size = (unsigned int)i_size_read(&ip->i_inode);
  1501. if (size == 0) {
  1502. gfs2_consist_inode(ip);
  1503. buf = ERR_PTR(-EIO);
  1504. goto out;
  1505. }
  1506. error = gfs2_meta_inode_buffer(ip, &dibh);
  1507. if (error) {
  1508. buf = ERR_PTR(error);
  1509. goto out;
  1510. }
  1511. buf = kzalloc(size + 1, GFP_NOFS);
  1512. if (!buf)
  1513. buf = ERR_PTR(-ENOMEM);
  1514. else
  1515. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1516. brelse(dibh);
  1517. out:
  1518. gfs2_glock_dq_uninit(&i_gh);
  1519. if (!IS_ERR(buf))
  1520. set_delayed_call(done, kfree_link, buf);
  1521. return buf;
  1522. }
  1523. /**
  1524. * gfs2_permission -
  1525. * @inode: The inode
  1526. * @mask: The mask to be tested
  1527. * @flags: Indicates whether this is an RCU path walk or not
  1528. *
  1529. * This may be called from the VFS directly, or from within GFS2 with the
  1530. * inode locked, so we look to see if the glock is already locked and only
  1531. * lock the glock if its not already been done.
  1532. *
  1533. * Returns: errno
  1534. */
  1535. int gfs2_permission(struct inode *inode, int mask)
  1536. {
  1537. struct gfs2_inode *ip;
  1538. struct gfs2_holder i_gh;
  1539. int error;
  1540. gfs2_holder_mark_uninitialized(&i_gh);
  1541. ip = GFS2_I(inode);
  1542. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1543. if (mask & MAY_NOT_BLOCK)
  1544. return -ECHILD;
  1545. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1546. if (error)
  1547. return error;
  1548. }
  1549. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1550. error = -EPERM;
  1551. else
  1552. error = generic_permission(inode, mask);
  1553. if (gfs2_holder_initialized(&i_gh))
  1554. gfs2_glock_dq_uninit(&i_gh);
  1555. return error;
  1556. }
  1557. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1558. {
  1559. setattr_copy(inode, attr);
  1560. mark_inode_dirty(inode);
  1561. return 0;
  1562. }
  1563. /**
  1564. * gfs2_setattr_simple -
  1565. * @ip:
  1566. * @attr:
  1567. *
  1568. * Returns: errno
  1569. */
  1570. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1571. {
  1572. int error;
  1573. if (current->journal_info)
  1574. return __gfs2_setattr_simple(inode, attr);
  1575. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1576. if (error)
  1577. return error;
  1578. error = __gfs2_setattr_simple(inode, attr);
  1579. gfs2_trans_end(GFS2_SB(inode));
  1580. return error;
  1581. }
  1582. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1583. {
  1584. struct gfs2_inode *ip = GFS2_I(inode);
  1585. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1586. kuid_t ouid, nuid;
  1587. kgid_t ogid, ngid;
  1588. int error;
  1589. struct gfs2_alloc_parms ap;
  1590. ouid = inode->i_uid;
  1591. ogid = inode->i_gid;
  1592. nuid = attr->ia_uid;
  1593. ngid = attr->ia_gid;
  1594. if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
  1595. ouid = nuid = NO_UID_QUOTA_CHANGE;
  1596. if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
  1597. ogid = ngid = NO_GID_QUOTA_CHANGE;
  1598. error = gfs2_rsqa_alloc(ip);
  1599. if (error)
  1600. goto out;
  1601. error = gfs2_rindex_update(sdp);
  1602. if (error)
  1603. goto out;
  1604. error = gfs2_quota_lock(ip, nuid, ngid);
  1605. if (error)
  1606. goto out;
  1607. ap.target = gfs2_get_inode_blocks(&ip->i_inode);
  1608. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1609. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1610. error = gfs2_quota_check(ip, nuid, ngid, &ap);
  1611. if (error)
  1612. goto out_gunlock_q;
  1613. }
  1614. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1615. if (error)
  1616. goto out_gunlock_q;
  1617. error = gfs2_setattr_simple(inode, attr);
  1618. if (error)
  1619. goto out_end_trans;
  1620. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1621. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1622. gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
  1623. gfs2_quota_change(ip, ap.target, nuid, ngid);
  1624. }
  1625. out_end_trans:
  1626. gfs2_trans_end(sdp);
  1627. out_gunlock_q:
  1628. gfs2_quota_unlock(ip);
  1629. out:
  1630. return error;
  1631. }
  1632. /**
  1633. * gfs2_setattr - Change attributes on an inode
  1634. * @dentry: The dentry which is changing
  1635. * @attr: The structure describing the change
  1636. *
  1637. * The VFS layer wants to change one or more of an inodes attributes. Write
  1638. * that change out to disk.
  1639. *
  1640. * Returns: errno
  1641. */
  1642. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1643. {
  1644. struct inode *inode = d_inode(dentry);
  1645. struct gfs2_inode *ip = GFS2_I(inode);
  1646. struct gfs2_holder i_gh;
  1647. int error;
  1648. error = gfs2_rsqa_alloc(ip);
  1649. if (error)
  1650. return error;
  1651. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1652. if (error)
  1653. return error;
  1654. error = -EPERM;
  1655. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1656. goto out;
  1657. error = setattr_prepare(dentry, attr);
  1658. if (error)
  1659. goto out;
  1660. if (attr->ia_valid & ATTR_SIZE)
  1661. error = gfs2_setattr_size(inode, attr->ia_size);
  1662. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1663. error = setattr_chown(inode, attr);
  1664. else {
  1665. error = gfs2_setattr_simple(inode, attr);
  1666. if (!error && attr->ia_valid & ATTR_MODE)
  1667. error = posix_acl_chmod(inode, inode->i_mode);
  1668. }
  1669. out:
  1670. if (!error)
  1671. mark_inode_dirty(inode);
  1672. gfs2_glock_dq_uninit(&i_gh);
  1673. return error;
  1674. }
  1675. /**
  1676. * gfs2_getattr - Read out an inode's attributes
  1677. * @path: Object to query
  1678. * @stat: The inode's stats
  1679. * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
  1680. * @flags: AT_STATX_xxx setting
  1681. *
  1682. * This may be called from the VFS directly, or from within GFS2 with the
  1683. * inode locked, so we look to see if the glock is already locked and only
  1684. * lock the glock if its not already been done. Note that its the NFS
  1685. * readdirplus operation which causes this to be called (from filldir)
  1686. * with the glock already held.
  1687. *
  1688. * Returns: errno
  1689. */
  1690. static int gfs2_getattr(const struct path *path, struct kstat *stat,
  1691. u32 request_mask, unsigned int flags)
  1692. {
  1693. struct inode *inode = d_inode(path->dentry);
  1694. struct gfs2_inode *ip = GFS2_I(inode);
  1695. struct gfs2_holder gh;
  1696. u32 gfsflags;
  1697. int error;
  1698. gfs2_holder_mark_uninitialized(&gh);
  1699. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1700. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1701. if (error)
  1702. return error;
  1703. }
  1704. gfsflags = ip->i_diskflags;
  1705. if (gfsflags & GFS2_DIF_APPENDONLY)
  1706. stat->attributes |= STATX_ATTR_APPEND;
  1707. if (gfsflags & GFS2_DIF_IMMUTABLE)
  1708. stat->attributes |= STATX_ATTR_IMMUTABLE;
  1709. stat->attributes_mask |= (STATX_ATTR_APPEND |
  1710. STATX_ATTR_COMPRESSED |
  1711. STATX_ATTR_ENCRYPTED |
  1712. STATX_ATTR_IMMUTABLE |
  1713. STATX_ATTR_NODUMP);
  1714. generic_fillattr(inode, stat);
  1715. if (gfs2_holder_initialized(&gh))
  1716. gfs2_glock_dq_uninit(&gh);
  1717. return 0;
  1718. }
  1719. const struct iomap_ops gfs2_iomap_ops = {
  1720. .iomap_begin = gfs2_iomap_begin,
  1721. };
  1722. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1723. u64 start, u64 len)
  1724. {
  1725. struct gfs2_inode *ip = GFS2_I(inode);
  1726. struct gfs2_holder gh;
  1727. int ret;
  1728. inode_lock_shared(inode);
  1729. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1730. if (ret)
  1731. goto out;
  1732. ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
  1733. gfs2_glock_dq_uninit(&gh);
  1734. out:
  1735. inode_unlock_shared(inode);
  1736. return ret;
  1737. }
  1738. loff_t gfs2_seek_data(struct file *file, loff_t offset)
  1739. {
  1740. struct inode *inode = file->f_mapping->host;
  1741. struct gfs2_inode *ip = GFS2_I(inode);
  1742. struct gfs2_holder gh;
  1743. loff_t ret;
  1744. inode_lock_shared(inode);
  1745. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1746. if (!ret)
  1747. ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
  1748. gfs2_glock_dq_uninit(&gh);
  1749. inode_unlock_shared(inode);
  1750. if (ret < 0)
  1751. return ret;
  1752. return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
  1753. }
  1754. loff_t gfs2_seek_hole(struct file *file, loff_t offset)
  1755. {
  1756. struct inode *inode = file->f_mapping->host;
  1757. struct gfs2_inode *ip = GFS2_I(inode);
  1758. struct gfs2_holder gh;
  1759. loff_t ret;
  1760. inode_lock_shared(inode);
  1761. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1762. if (!ret)
  1763. ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
  1764. gfs2_glock_dq_uninit(&gh);
  1765. inode_unlock_shared(inode);
  1766. if (ret < 0)
  1767. return ret;
  1768. return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
  1769. }
  1770. const struct inode_operations gfs2_file_iops = {
  1771. .permission = gfs2_permission,
  1772. .setattr = gfs2_setattr,
  1773. .getattr = gfs2_getattr,
  1774. .listxattr = gfs2_listxattr,
  1775. .fiemap = gfs2_fiemap,
  1776. .get_acl = gfs2_get_acl,
  1777. .set_acl = gfs2_set_acl,
  1778. };
  1779. const struct inode_operations gfs2_dir_iops = {
  1780. .create = gfs2_create,
  1781. .lookup = gfs2_lookup,
  1782. .link = gfs2_link,
  1783. .unlink = gfs2_unlink,
  1784. .symlink = gf

Large files files are truncated, but you can click here to view the full file