xfs_iops.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_da_format.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_bmap.h"
  28. #include "xfs_bmap_util.h"
  29. #include "xfs_acl.h"
  30. #include "xfs_quota.h"
  31. #include "xfs_error.h"
  32. #include "xfs_attr.h"
  33. #include "xfs_trans.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_icache.h"
  36. #include "xfs_symlink.h"
  37. #include "xfs_da_btree.h"
  38. #include "xfs_dir2.h"
  39. #include "xfs_trans_space.h"
  40. #include "xfs_pnfs.h"
  41. #include "xfs_iomap.h"
  42. #include <linux/capability.h>
  43. #include <linux/xattr.h>
  44. #include <linux/posix_acl.h>
  45. #include <linux/security.h>
  46. #include <linux/iomap.h>
  47. #include <linux/slab.h>
  48. /*
  49. * Directories have different lock order w.r.t. mmap_sem compared to regular
  50. * files. This is due to readdir potentially triggering page faults on a user
  51. * buffer inside filldir(), and this happens with the ilock on the directory
  52. * held. For regular files, the lock order is the other way around - the
  53. * mmap_sem is taken during the page fault, and then we lock the ilock to do
  54. * block mapping. Hence we need a different class for the directory ilock so
  55. * that lockdep can tell them apart.
  56. */
  57. static struct lock_class_key xfs_nondir_ilock_class;
  58. static struct lock_class_key xfs_dir_ilock_class;
  59. static int
  60. xfs_initxattrs(
  61. struct inode *inode,
  62. const struct xattr *xattr_array,
  63. void *fs_info)
  64. {
  65. const struct xattr *xattr;
  66. struct xfs_inode *ip = XFS_I(inode);
  67. int error = 0;
  68. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  69. error = xfs_attr_set(ip, xattr->name, xattr->value,
  70. xattr->value_len, ATTR_SECURE);
  71. if (error < 0)
  72. break;
  73. }
  74. return error;
  75. }
  76. /*
  77. * Hook in SELinux. This is not quite correct yet, what we really need
  78. * here (as we do for default ACLs) is a mechanism by which creation of
  79. * these attrs can be journalled at inode creation time (along with the
  80. * inode, of course, such that log replay can't cause these to be lost).
  81. */
  82. STATIC int
  83. xfs_init_security(
  84. struct inode *inode,
  85. struct inode *dir,
  86. const struct qstr *qstr)
  87. {
  88. return security_inode_init_security(inode, dir, qstr,
  89. &xfs_initxattrs, NULL);
  90. }
  91. static void
  92. xfs_dentry_to_name(
  93. struct xfs_name *namep,
  94. struct dentry *dentry,
  95. int mode)
  96. {
  97. namep->name = dentry->d_name.name;
  98. namep->len = dentry->d_name.len;
  99. namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
  100. }
  101. STATIC void
  102. xfs_cleanup_inode(
  103. struct inode *dir,
  104. struct inode *inode,
  105. struct dentry *dentry)
  106. {
  107. struct xfs_name teardown;
  108. /* Oh, the horror.
  109. * If we can't add the ACL or we fail in
  110. * xfs_init_security we must back out.
  111. * ENOSPC can hit here, among other things.
  112. */
  113. xfs_dentry_to_name(&teardown, dentry, 0);
  114. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  115. }
  116. STATIC int
  117. xfs_generic_create(
  118. struct inode *dir,
  119. struct dentry *dentry,
  120. umode_t mode,
  121. dev_t rdev,
  122. bool tmpfile) /* unnamed file */
  123. {
  124. struct inode *inode;
  125. struct xfs_inode *ip = NULL;
  126. struct posix_acl *default_acl, *acl;
  127. struct xfs_name name;
  128. int error;
  129. /*
  130. * Irix uses Missed'em'V split, but doesn't want to see
  131. * the upper 5 bits of (14bit) major.
  132. */
  133. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  134. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  135. return -EINVAL;
  136. rdev = sysv_encode_dev(rdev);
  137. } else {
  138. rdev = 0;
  139. }
  140. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  141. if (error)
  142. return error;
  143. if (!tmpfile) {
  144. xfs_dentry_to_name(&name, dentry, mode);
  145. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
  146. } else {
  147. error = xfs_create_tmpfile(XFS_I(dir), dentry, mode, &ip);
  148. }
  149. if (unlikely(error))
  150. goto out_free_acl;
  151. inode = VFS_I(ip);
  152. error = xfs_init_security(inode, dir, &dentry->d_name);
  153. if (unlikely(error))
  154. goto out_cleanup_inode;
  155. #ifdef CONFIG_XFS_POSIX_ACL
  156. if (default_acl) {
  157. error = xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  158. if (error)
  159. goto out_cleanup_inode;
  160. }
  161. if (acl) {
  162. error = xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  163. if (error)
  164. goto out_cleanup_inode;
  165. }
  166. #endif
  167. xfs_setup_iops(ip);
  168. if (tmpfile)
  169. d_tmpfile(dentry, inode);
  170. else
  171. d_instantiate(dentry, inode);
  172. xfs_finish_inode_setup(ip);
  173. out_free_acl:
  174. if (default_acl)
  175. posix_acl_release(default_acl);
  176. if (acl)
  177. posix_acl_release(acl);
  178. return error;
  179. out_cleanup_inode:
  180. xfs_finish_inode_setup(ip);
  181. if (!tmpfile)
  182. xfs_cleanup_inode(dir, inode, dentry);
  183. iput(inode);
  184. goto out_free_acl;
  185. }
  186. STATIC int
  187. xfs_vn_mknod(
  188. struct inode *dir,
  189. struct dentry *dentry,
  190. umode_t mode,
  191. dev_t rdev)
  192. {
  193. return xfs_generic_create(dir, dentry, mode, rdev, false);
  194. }
  195. STATIC int
  196. xfs_vn_create(
  197. struct inode *dir,
  198. struct dentry *dentry,
  199. umode_t mode,
  200. bool flags)
  201. {
  202. return xfs_vn_mknod(dir, dentry, mode, 0);
  203. }
  204. STATIC int
  205. xfs_vn_mkdir(
  206. struct inode *dir,
  207. struct dentry *dentry,
  208. umode_t mode)
  209. {
  210. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  211. }
  212. STATIC struct dentry *
  213. xfs_vn_lookup(
  214. struct inode *dir,
  215. struct dentry *dentry,
  216. unsigned int flags)
  217. {
  218. struct xfs_inode *cip;
  219. struct xfs_name name;
  220. int error;
  221. if (dentry->d_name.len >= MAXNAMELEN)
  222. return ERR_PTR(-ENAMETOOLONG);
  223. xfs_dentry_to_name(&name, dentry, 0);
  224. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  225. if (unlikely(error)) {
  226. if (unlikely(error != -ENOENT))
  227. return ERR_PTR(error);
  228. d_add(dentry, NULL);
  229. return NULL;
  230. }
  231. return d_splice_alias(VFS_I(cip), dentry);
  232. }
  233. STATIC struct dentry *
  234. xfs_vn_ci_lookup(
  235. struct inode *dir,
  236. struct dentry *dentry,
  237. unsigned int flags)
  238. {
  239. struct xfs_inode *ip;
  240. struct xfs_name xname;
  241. struct xfs_name ci_name;
  242. struct qstr dname;
  243. int error;
  244. if (dentry->d_name.len >= MAXNAMELEN)
  245. return ERR_PTR(-ENAMETOOLONG);
  246. xfs_dentry_to_name(&xname, dentry, 0);
  247. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  248. if (unlikely(error)) {
  249. if (unlikely(error != -ENOENT))
  250. return ERR_PTR(error);
  251. /*
  252. * call d_add(dentry, NULL) here when d_drop_negative_children
  253. * is called in xfs_vn_mknod (ie. allow negative dentries
  254. * with CI filesystems).
  255. */
  256. return NULL;
  257. }
  258. /* if exact match, just splice and exit */
  259. if (!ci_name.name)
  260. return d_splice_alias(VFS_I(ip), dentry);
  261. /* else case-insensitive match... */
  262. dname.name = ci_name.name;
  263. dname.len = ci_name.len;
  264. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  265. kmem_free(ci_name.name);
  266. return dentry;
  267. }
  268. STATIC int
  269. xfs_vn_link(
  270. struct dentry *old_dentry,
  271. struct inode *dir,
  272. struct dentry *dentry)
  273. {
  274. struct inode *inode = d_inode(old_dentry);
  275. struct xfs_name name;
  276. int error;
  277. xfs_dentry_to_name(&name, dentry, inode->i_mode);
  278. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  279. if (unlikely(error))
  280. return error;
  281. ihold(inode);
  282. d_instantiate(dentry, inode);
  283. return 0;
  284. }
  285. STATIC int
  286. xfs_vn_unlink(
  287. struct inode *dir,
  288. struct dentry *dentry)
  289. {
  290. struct xfs_name name;
  291. int error;
  292. xfs_dentry_to_name(&name, dentry, 0);
  293. error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
  294. if (error)
  295. return error;
  296. /*
  297. * With unlink, the VFS makes the dentry "negative": no inode,
  298. * but still hashed. This is incompatible with case-insensitive
  299. * mode, so invalidate (unhash) the dentry in CI-mode.
  300. */
  301. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  302. d_invalidate(dentry);
  303. return 0;
  304. }
  305. STATIC int
  306. xfs_vn_symlink(
  307. struct inode *dir,
  308. struct dentry *dentry,
  309. const char *symname)
  310. {
  311. struct inode *inode;
  312. struct xfs_inode *cip = NULL;
  313. struct xfs_name name;
  314. int error;
  315. umode_t mode;
  316. mode = S_IFLNK |
  317. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  318. xfs_dentry_to_name(&name, dentry, mode);
  319. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
  320. if (unlikely(error))
  321. goto out;
  322. inode = VFS_I(cip);
  323. error = xfs_init_security(inode, dir, &dentry->d_name);
  324. if (unlikely(error))
  325. goto out_cleanup_inode;
  326. xfs_setup_iops(cip);
  327. d_instantiate(dentry, inode);
  328. xfs_finish_inode_setup(cip);
  329. return 0;
  330. out_cleanup_inode:
  331. xfs_finish_inode_setup(cip);
  332. xfs_cleanup_inode(dir, inode, dentry);
  333. iput(inode);
  334. out:
  335. return error;
  336. }
  337. STATIC int
  338. xfs_vn_rename(
  339. struct inode *odir,
  340. struct dentry *odentry,
  341. struct inode *ndir,
  342. struct dentry *ndentry,
  343. unsigned int flags)
  344. {
  345. struct inode *new_inode = d_inode(ndentry);
  346. int omode = 0;
  347. struct xfs_name oname;
  348. struct xfs_name nname;
  349. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  350. return -EINVAL;
  351. /* if we are exchanging files, we need to set i_mode of both files */
  352. if (flags & RENAME_EXCHANGE)
  353. omode = d_inode(ndentry)->i_mode;
  354. xfs_dentry_to_name(&oname, odentry, omode);
  355. xfs_dentry_to_name(&nname, ndentry, d_inode(odentry)->i_mode);
  356. return xfs_rename(XFS_I(odir), &oname, XFS_I(d_inode(odentry)),
  357. XFS_I(ndir), &nname,
  358. new_inode ? XFS_I(new_inode) : NULL, flags);
  359. }
  360. /*
  361. * careful here - this function can get called recursively, so
  362. * we need to be very careful about how much stack we use.
  363. * uio is kmalloced for this reason...
  364. */
  365. STATIC const char *
  366. xfs_vn_get_link(
  367. struct dentry *dentry,
  368. struct inode *inode,
  369. struct delayed_call *done)
  370. {
  371. char *link;
  372. int error = -ENOMEM;
  373. if (!dentry)
  374. return ERR_PTR(-ECHILD);
  375. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  376. if (!link)
  377. goto out_err;
  378. error = xfs_readlink(XFS_I(d_inode(dentry)), link);
  379. if (unlikely(error))
  380. goto out_kfree;
  381. set_delayed_call(done, kfree_link, link);
  382. return link;
  383. out_kfree:
  384. kfree(link);
  385. out_err:
  386. return ERR_PTR(error);
  387. }
  388. STATIC const char *
  389. xfs_vn_get_link_inline(
  390. struct dentry *dentry,
  391. struct inode *inode,
  392. struct delayed_call *done)
  393. {
  394. ASSERT(XFS_I(inode)->i_df.if_flags & XFS_IFINLINE);
  395. return XFS_I(inode)->i_df.if_u1.if_data;
  396. }
  397. STATIC int
  398. xfs_vn_getattr(
  399. struct vfsmount *mnt,
  400. struct dentry *dentry,
  401. struct kstat *stat)
  402. {
  403. struct inode *inode = d_inode(dentry);
  404. struct xfs_inode *ip = XFS_I(inode);
  405. struct xfs_mount *mp = ip->i_mount;
  406. trace_xfs_getattr(ip);
  407. if (XFS_FORCED_SHUTDOWN(mp))
  408. return -EIO;
  409. stat->size = XFS_ISIZE(ip);
  410. stat->dev = inode->i_sb->s_dev;
  411. stat->mode = inode->i_mode;
  412. stat->nlink = inode->i_nlink;
  413. stat->uid = inode->i_uid;
  414. stat->gid = inode->i_gid;
  415. stat->ino = ip->i_ino;
  416. stat->atime = inode->i_atime;
  417. stat->mtime = inode->i_mtime;
  418. stat->ctime = inode->i_ctime;
  419. stat->blocks =
  420. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  421. switch (inode->i_mode & S_IFMT) {
  422. case S_IFBLK:
  423. case S_IFCHR:
  424. stat->blksize = BLKDEV_IOSIZE;
  425. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  426. sysv_minor(ip->i_df.if_u2.if_rdev));
  427. break;
  428. default:
  429. if (XFS_IS_REALTIME_INODE(ip)) {
  430. /*
  431. * If the file blocks are being allocated from a
  432. * realtime volume, then return the inode's realtime
  433. * extent size or the realtime volume's extent size.
  434. */
  435. stat->blksize =
  436. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  437. } else
  438. stat->blksize = xfs_preferred_iosize(mp);
  439. stat->rdev = 0;
  440. break;
  441. }
  442. return 0;
  443. }
  444. static void
  445. xfs_setattr_mode(
  446. struct xfs_inode *ip,
  447. struct iattr *iattr)
  448. {
  449. struct inode *inode = VFS_I(ip);
  450. umode_t mode = iattr->ia_mode;
  451. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  452. inode->i_mode &= S_IFMT;
  453. inode->i_mode |= mode & ~S_IFMT;
  454. }
  455. void
  456. xfs_setattr_time(
  457. struct xfs_inode *ip,
  458. struct iattr *iattr)
  459. {
  460. struct inode *inode = VFS_I(ip);
  461. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  462. if (iattr->ia_valid & ATTR_ATIME)
  463. inode->i_atime = iattr->ia_atime;
  464. if (iattr->ia_valid & ATTR_CTIME)
  465. inode->i_ctime = iattr->ia_ctime;
  466. if (iattr->ia_valid & ATTR_MTIME)
  467. inode->i_mtime = iattr->ia_mtime;
  468. }
  469. int
  470. xfs_setattr_nonsize(
  471. struct xfs_inode *ip,
  472. struct iattr *iattr,
  473. int flags)
  474. {
  475. xfs_mount_t *mp = ip->i_mount;
  476. struct inode *inode = VFS_I(ip);
  477. int mask = iattr->ia_valid;
  478. xfs_trans_t *tp;
  479. int error;
  480. kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
  481. kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
  482. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  483. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  484. trace_xfs_setattr(ip);
  485. /* If acls are being inherited, we already have this checked */
  486. if (!(flags & XFS_ATTR_NOACL)) {
  487. if (mp->m_flags & XFS_MOUNT_RDONLY)
  488. return -EROFS;
  489. if (XFS_FORCED_SHUTDOWN(mp))
  490. return -EIO;
  491. error = inode_change_ok(inode, iattr);
  492. if (error)
  493. return error;
  494. }
  495. ASSERT((mask & ATTR_SIZE) == 0);
  496. /*
  497. * If disk quotas is on, we make sure that the dquots do exist on disk,
  498. * before we start any other transactions. Trying to do this later
  499. * is messy. We don't care to take a readlock to look at the ids
  500. * in inode here, because we can't hold it across the trans_reserve.
  501. * If the IDs do change before we take the ilock, we're covered
  502. * because the i_*dquot fields will get updated anyway.
  503. */
  504. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  505. uint qflags = 0;
  506. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  507. uid = iattr->ia_uid;
  508. qflags |= XFS_QMOPT_UQUOTA;
  509. } else {
  510. uid = inode->i_uid;
  511. }
  512. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  513. gid = iattr->ia_gid;
  514. qflags |= XFS_QMOPT_GQUOTA;
  515. } else {
  516. gid = inode->i_gid;
  517. }
  518. /*
  519. * We take a reference when we initialize udqp and gdqp,
  520. * so it is important that we never blindly double trip on
  521. * the same variable. See xfs_create() for an example.
  522. */
  523. ASSERT(udqp == NULL);
  524. ASSERT(gdqp == NULL);
  525. error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
  526. xfs_kgid_to_gid(gid),
  527. xfs_get_projid(ip),
  528. qflags, &udqp, &gdqp, NULL);
  529. if (error)
  530. return error;
  531. }
  532. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  533. if (error)
  534. goto out_dqrele;
  535. xfs_ilock(ip, XFS_ILOCK_EXCL);
  536. xfs_trans_ijoin(tp, ip, 0);
  537. /*
  538. * Change file ownership. Must be the owner or privileged.
  539. */
  540. if (mask & (ATTR_UID|ATTR_GID)) {
  541. /*
  542. * These IDs could have changed since we last looked at them.
  543. * But, we're assured that if the ownership did change
  544. * while we didn't have the inode locked, inode's dquot(s)
  545. * would have changed also.
  546. */
  547. iuid = inode->i_uid;
  548. igid = inode->i_gid;
  549. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  550. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  551. /*
  552. * Do a quota reservation only if uid/gid is actually
  553. * going to change.
  554. */
  555. if (XFS_IS_QUOTA_RUNNING(mp) &&
  556. ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
  557. (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
  558. ASSERT(tp);
  559. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  560. NULL, capable(CAP_FOWNER) ?
  561. XFS_QMOPT_FORCE_RES : 0);
  562. if (error) /* out of quota */
  563. goto out_cancel;
  564. }
  565. }
  566. /*
  567. * Change file ownership. Must be the owner or privileged.
  568. */
  569. if (mask & (ATTR_UID|ATTR_GID)) {
  570. /*
  571. * CAP_FSETID overrides the following restrictions:
  572. *
  573. * The set-user-ID and set-group-ID bits of a file will be
  574. * cleared upon successful return from chown()
  575. */
  576. if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
  577. !capable(CAP_FSETID))
  578. inode->i_mode &= ~(S_ISUID|S_ISGID);
  579. /*
  580. * Change the ownerships and register quota modifications
  581. * in the transaction.
  582. */
  583. if (!uid_eq(iuid, uid)) {
  584. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  585. ASSERT(mask & ATTR_UID);
  586. ASSERT(udqp);
  587. olddquot1 = xfs_qm_vop_chown(tp, ip,
  588. &ip->i_udquot, udqp);
  589. }
  590. ip->i_d.di_uid = xfs_kuid_to_uid(uid);
  591. inode->i_uid = uid;
  592. }
  593. if (!gid_eq(igid, gid)) {
  594. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  595. ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
  596. !XFS_IS_PQUOTA_ON(mp));
  597. ASSERT(mask & ATTR_GID);
  598. ASSERT(gdqp);
  599. olddquot2 = xfs_qm_vop_chown(tp, ip,
  600. &ip->i_gdquot, gdqp);
  601. }
  602. ip->i_d.di_gid = xfs_kgid_to_gid(gid);
  603. inode->i_gid = gid;
  604. }
  605. }
  606. if (mask & ATTR_MODE)
  607. xfs_setattr_mode(ip, iattr);
  608. if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  609. xfs_setattr_time(ip, iattr);
  610. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  611. XFS_STATS_INC(mp, xs_ig_attrchg);
  612. if (mp->m_flags & XFS_MOUNT_WSYNC)
  613. xfs_trans_set_sync(tp);
  614. error = xfs_trans_commit(tp);
  615. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  616. /*
  617. * Release any dquot(s) the inode had kept before chown.
  618. */
  619. xfs_qm_dqrele(olddquot1);
  620. xfs_qm_dqrele(olddquot2);
  621. xfs_qm_dqrele(udqp);
  622. xfs_qm_dqrele(gdqp);
  623. if (error)
  624. return error;
  625. /*
  626. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  627. * update. We could avoid this with linked transactions
  628. * and passing down the transaction pointer all the way
  629. * to attr_set. No previous user of the generic
  630. * Posix ACL code seems to care about this issue either.
  631. */
  632. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  633. error = posix_acl_chmod(inode, inode->i_mode);
  634. if (error)
  635. return error;
  636. }
  637. return 0;
  638. out_cancel:
  639. xfs_trans_cancel(tp);
  640. out_dqrele:
  641. xfs_qm_dqrele(udqp);
  642. xfs_qm_dqrele(gdqp);
  643. return error;
  644. }
  645. /*
  646. * Truncate file. Must have write permission and not be a directory.
  647. */
  648. int
  649. xfs_setattr_size(
  650. struct xfs_inode *ip,
  651. struct iattr *iattr)
  652. {
  653. struct xfs_mount *mp = ip->i_mount;
  654. struct inode *inode = VFS_I(ip);
  655. xfs_off_t oldsize, newsize;
  656. struct xfs_trans *tp;
  657. int error;
  658. uint lock_flags = 0;
  659. bool did_zeroing = false;
  660. trace_xfs_setattr(ip);
  661. if (mp->m_flags & XFS_MOUNT_RDONLY)
  662. return -EROFS;
  663. if (XFS_FORCED_SHUTDOWN(mp))
  664. return -EIO;
  665. error = inode_change_ok(inode, iattr);
  666. if (error)
  667. return error;
  668. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  669. ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
  670. ASSERT(S_ISREG(inode->i_mode));
  671. ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  672. ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  673. oldsize = inode->i_size;
  674. newsize = iattr->ia_size;
  675. /*
  676. * Short circuit the truncate case for zero length files.
  677. */
  678. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  679. if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
  680. return 0;
  681. /*
  682. * Use the regular setattr path to update the timestamps.
  683. */
  684. iattr->ia_valid &= ~ATTR_SIZE;
  685. return xfs_setattr_nonsize(ip, iattr, 0);
  686. }
  687. /*
  688. * Make sure that the dquots are attached to the inode.
  689. */
  690. error = xfs_qm_dqattach(ip, 0);
  691. if (error)
  692. return error;
  693. /*
  694. * Wait for all direct I/O to complete.
  695. */
  696. inode_dio_wait(inode);
  697. /*
  698. * File data changes must be complete before we start the transaction to
  699. * modify the inode. This needs to be done before joining the inode to
  700. * the transaction because the inode cannot be unlocked once it is a
  701. * part of the transaction.
  702. *
  703. * Start with zeroing any data beyond EOF that we may expose on file
  704. * extension, or zeroing out the rest of the block on a downward
  705. * truncate.
  706. */
  707. if (newsize > oldsize) {
  708. error = xfs_zero_eof(ip, newsize, oldsize, &did_zeroing);
  709. } else {
  710. error = iomap_truncate_page(inode, newsize, &did_zeroing,
  711. &xfs_iomap_ops);
  712. }
  713. if (error)
  714. return error;
  715. /*
  716. * We are going to log the inode size change in this transaction so
  717. * any previous writes that are beyond the on disk EOF and the new
  718. * EOF that have not been written out need to be written here. If we
  719. * do not write the data out, we expose ourselves to the null files
  720. * problem. Note that this includes any block zeroing we did above;
  721. * otherwise those blocks may not be zeroed after a crash.
  722. */
  723. if (did_zeroing ||
  724. (newsize > ip->i_d.di_size && oldsize != ip->i_d.di_size)) {
  725. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  726. ip->i_d.di_size, newsize);
  727. if (error)
  728. return error;
  729. }
  730. /*
  731. * We've already locked out new page faults, so now we can safely remove
  732. * pages from the page cache knowing they won't get refaulted until we
  733. * drop the XFS_MMAP_EXCL lock after the extent manipulations are
  734. * complete. The truncate_setsize() call also cleans partial EOF page
  735. * PTEs on extending truncates and hence ensures sub-page block size
  736. * filesystems are correctly handled, too.
  737. *
  738. * We have to do all the page cache truncate work outside the
  739. * transaction context as the "lock" order is page lock->log space
  740. * reservation as defined by extent allocation in the writeback path.
  741. * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
  742. * having already truncated the in-memory version of the file (i.e. made
  743. * user visible changes). There's not much we can do about this, except
  744. * to hope that the caller sees ENOMEM and retries the truncate
  745. * operation.
  746. */
  747. truncate_setsize(inode, newsize);
  748. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  749. if (error)
  750. return error;
  751. lock_flags |= XFS_ILOCK_EXCL;
  752. xfs_ilock(ip, XFS_ILOCK_EXCL);
  753. xfs_trans_ijoin(tp, ip, 0);
  754. /*
  755. * Only change the c/mtime if we are changing the size or we are
  756. * explicitly asked to change it. This handles the semantic difference
  757. * between truncate() and ftruncate() as implemented in the VFS.
  758. *
  759. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  760. * special case where we need to update the times despite not having
  761. * these flags set. For all other operations the VFS set these flags
  762. * explicitly if it wants a timestamp update.
  763. */
  764. if (newsize != oldsize &&
  765. !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
  766. iattr->ia_ctime = iattr->ia_mtime =
  767. current_fs_time(inode->i_sb);
  768. iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  769. }
  770. /*
  771. * The first thing we do is set the size to new_size permanently on
  772. * disk. This way we don't have to worry about anyone ever being able
  773. * to look at the data being freed even in the face of a crash.
  774. * What we're getting around here is the case where we free a block, it
  775. * is allocated to another file, it is written to, and then we crash.
  776. * If the new data gets written to the file but the log buffers
  777. * containing the free and reallocation don't, then we'd end up with
  778. * garbage in the blocks being freed. As long as we make the new size
  779. * permanent before actually freeing any blocks it doesn't matter if
  780. * they get written to.
  781. */
  782. ip->i_d.di_size = newsize;
  783. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  784. if (newsize <= oldsize) {
  785. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  786. if (error)
  787. goto out_trans_cancel;
  788. /*
  789. * Truncated "down", so we're removing references to old data
  790. * here - if we delay flushing for a long time, we expose
  791. * ourselves unduly to the notorious NULL files problem. So,
  792. * we mark this inode and flush it when the file is closed,
  793. * and do not wait the usual (long) time for writeout.
  794. */
  795. xfs_iflags_set(ip, XFS_ITRUNCATED);
  796. /* A truncate down always removes post-EOF blocks. */
  797. xfs_inode_clear_eofblocks_tag(ip);
  798. }
  799. if (iattr->ia_valid & ATTR_MODE)
  800. xfs_setattr_mode(ip, iattr);
  801. if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  802. xfs_setattr_time(ip, iattr);
  803. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  804. XFS_STATS_INC(mp, xs_ig_attrchg);
  805. if (mp->m_flags & XFS_MOUNT_WSYNC)
  806. xfs_trans_set_sync(tp);
  807. error = xfs_trans_commit(tp);
  808. out_unlock:
  809. if (lock_flags)
  810. xfs_iunlock(ip, lock_flags);
  811. return error;
  812. out_trans_cancel:
  813. xfs_trans_cancel(tp);
  814. goto out_unlock;
  815. }
  816. STATIC int
  817. xfs_vn_setattr(
  818. struct dentry *dentry,
  819. struct iattr *iattr)
  820. {
  821. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  822. int error;
  823. if (iattr->ia_valid & ATTR_SIZE) {
  824. uint iolock = XFS_IOLOCK_EXCL;
  825. xfs_ilock(ip, iolock);
  826. error = xfs_break_layouts(d_inode(dentry), &iolock, true);
  827. if (!error) {
  828. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  829. iolock |= XFS_MMAPLOCK_EXCL;
  830. error = xfs_setattr_size(ip, iattr);
  831. }
  832. xfs_iunlock(ip, iolock);
  833. } else {
  834. error = xfs_setattr_nonsize(ip, iattr, 0);
  835. }
  836. return error;
  837. }
  838. STATIC int
  839. xfs_vn_update_time(
  840. struct inode *inode,
  841. struct timespec *now,
  842. int flags)
  843. {
  844. struct xfs_inode *ip = XFS_I(inode);
  845. struct xfs_mount *mp = ip->i_mount;
  846. struct xfs_trans *tp;
  847. int error;
  848. trace_xfs_update_time(ip);
  849. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  850. if (error)
  851. return error;
  852. xfs_ilock(ip, XFS_ILOCK_EXCL);
  853. if (flags & S_CTIME)
  854. inode->i_ctime = *now;
  855. if (flags & S_MTIME)
  856. inode->i_mtime = *now;
  857. if (flags & S_ATIME)
  858. inode->i_atime = *now;
  859. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  860. xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
  861. return xfs_trans_commit(tp);
  862. }
  863. STATIC int
  864. xfs_vn_fiemap(
  865. struct inode *inode,
  866. struct fiemap_extent_info *fieinfo,
  867. u64 start,
  868. u64 length)
  869. {
  870. int error;
  871. xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
  872. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  873. fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
  874. error = iomap_fiemap(inode, fieinfo, start, length,
  875. &xfs_xattr_iomap_ops);
  876. } else {
  877. error = iomap_fiemap(inode, fieinfo, start, length,
  878. &xfs_iomap_ops);
  879. }
  880. xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
  881. return error;
  882. }
  883. STATIC int
  884. xfs_vn_tmpfile(
  885. struct inode *dir,
  886. struct dentry *dentry,
  887. umode_t mode)
  888. {
  889. return xfs_generic_create(dir, dentry, mode, 0, true);
  890. }
  891. static const struct inode_operations xfs_inode_operations = {
  892. .get_acl = xfs_get_acl,
  893. .set_acl = xfs_set_acl,
  894. .getattr = xfs_vn_getattr,
  895. .setattr = xfs_vn_setattr,
  896. .setxattr = generic_setxattr,
  897. .getxattr = generic_getxattr,
  898. .removexattr = generic_removexattr,
  899. .listxattr = xfs_vn_listxattr,
  900. .fiemap = xfs_vn_fiemap,
  901. .update_time = xfs_vn_update_time,
  902. };
  903. static const struct inode_operations xfs_dir_inode_operations = {
  904. .create = xfs_vn_create,
  905. .lookup = xfs_vn_lookup,
  906. .link = xfs_vn_link,
  907. .unlink = xfs_vn_unlink,
  908. .symlink = xfs_vn_symlink,
  909. .mkdir = xfs_vn_mkdir,
  910. /*
  911. * Yes, XFS uses the same method for rmdir and unlink.
  912. *
  913. * There are some subtile differences deeper in the code,
  914. * but we use S_ISDIR to check for those.
  915. */
  916. .rmdir = xfs_vn_unlink,
  917. .mknod = xfs_vn_mknod,
  918. .rename2 = xfs_vn_rename,
  919. .get_acl = xfs_get_acl,
  920. .set_acl = xfs_set_acl,
  921. .getattr = xfs_vn_getattr,
  922. .setattr = xfs_vn_setattr,
  923. .setxattr = generic_setxattr,
  924. .getxattr = generic_getxattr,
  925. .removexattr = generic_removexattr,
  926. .listxattr = xfs_vn_listxattr,
  927. .update_time = xfs_vn_update_time,
  928. .tmpfile = xfs_vn_tmpfile,
  929. };
  930. static const struct inode_operations xfs_dir_ci_inode_operations = {
  931. .create = xfs_vn_create,
  932. .lookup = xfs_vn_ci_lookup,
  933. .link = xfs_vn_link,
  934. .unlink = xfs_vn_unlink,
  935. .symlink = xfs_vn_symlink,
  936. .mkdir = xfs_vn_mkdir,
  937. /*
  938. * Yes, XFS uses the same method for rmdir and unlink.
  939. *
  940. * There are some subtile differences deeper in the code,
  941. * but we use S_ISDIR to check for those.
  942. */
  943. .rmdir = xfs_vn_unlink,
  944. .mknod = xfs_vn_mknod,
  945. .rename2 = xfs_vn_rename,
  946. .get_acl = xfs_get_acl,
  947. .set_acl = xfs_set_acl,
  948. .getattr = xfs_vn_getattr,
  949. .setattr = xfs_vn_setattr,
  950. .setxattr = generic_setxattr,
  951. .getxattr = generic_getxattr,
  952. .removexattr = generic_removexattr,
  953. .listxattr = xfs_vn_listxattr,
  954. .update_time = xfs_vn_update_time,
  955. .tmpfile = xfs_vn_tmpfile,
  956. };
  957. static const struct inode_operations xfs_symlink_inode_operations = {
  958. .readlink = generic_readlink,
  959. .get_link = xfs_vn_get_link,
  960. .getattr = xfs_vn_getattr,
  961. .setattr = xfs_vn_setattr,
  962. .setxattr = generic_setxattr,
  963. .getxattr = generic_getxattr,
  964. .removexattr = generic_removexattr,
  965. .listxattr = xfs_vn_listxattr,
  966. .update_time = xfs_vn_update_time,
  967. };
  968. static const struct inode_operations xfs_inline_symlink_inode_operations = {
  969. .readlink = generic_readlink,
  970. .get_link = xfs_vn_get_link_inline,
  971. .getattr = xfs_vn_getattr,
  972. .setattr = xfs_vn_setattr,
  973. .setxattr = generic_setxattr,
  974. .getxattr = generic_getxattr,
  975. .removexattr = generic_removexattr,
  976. .listxattr = xfs_vn_listxattr,
  977. .update_time = xfs_vn_update_time,
  978. };
  979. STATIC void
  980. xfs_diflags_to_iflags(
  981. struct inode *inode,
  982. struct xfs_inode *ip)
  983. {
  984. uint16_t flags = ip->i_d.di_flags;
  985. inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC |
  986. S_NOATIME | S_DAX);
  987. if (flags & XFS_DIFLAG_IMMUTABLE)
  988. inode->i_flags |= S_IMMUTABLE;
  989. if (flags & XFS_DIFLAG_APPEND)
  990. inode->i_flags |= S_APPEND;
  991. if (flags & XFS_DIFLAG_SYNC)
  992. inode->i_flags |= S_SYNC;
  993. if (flags & XFS_DIFLAG_NOATIME)
  994. inode->i_flags |= S_NOATIME;
  995. if (S_ISREG(inode->i_mode) &&
  996. ip->i_mount->m_sb.sb_blocksize == PAGE_SIZE &&
  997. (ip->i_mount->m_flags & XFS_MOUNT_DAX ||
  998. ip->i_d.di_flags2 & XFS_DIFLAG2_DAX))
  999. inode->i_flags |= S_DAX;
  1000. }
  1001. /*
  1002. * Initialize the Linux inode.
  1003. *
  1004. * When reading existing inodes from disk this is called directly from xfs_iget,
  1005. * when creating a new inode it is called from xfs_ialloc after setting up the
  1006. * inode. These callers have different criteria for clearing XFS_INEW, so leave
  1007. * it up to the caller to deal with unlocking the inode appropriately.
  1008. */
  1009. void
  1010. xfs_setup_inode(
  1011. struct xfs_inode *ip)
  1012. {
  1013. struct inode *inode = &ip->i_vnode;
  1014. gfp_t gfp_mask;
  1015. inode->i_ino = ip->i_ino;
  1016. inode->i_state = I_NEW;
  1017. inode_sb_list_add(inode);
  1018. /* make the inode look hashed for the writeback code */
  1019. hlist_add_fake(&inode->i_hash);
  1020. inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
  1021. inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
  1022. switch (inode->i_mode & S_IFMT) {
  1023. case S_IFBLK:
  1024. case S_IFCHR:
  1025. inode->i_rdev =
  1026. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  1027. sysv_minor(ip->i_df.if_u2.if_rdev));
  1028. break;
  1029. default:
  1030. inode->i_rdev = 0;
  1031. break;
  1032. }
  1033. i_size_write(inode, ip->i_d.di_size);
  1034. xfs_diflags_to_iflags(inode, ip);
  1035. if (S_ISDIR(inode->i_mode)) {
  1036. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
  1037. ip->d_ops = ip->i_mount->m_dir_inode_ops;
  1038. } else {
  1039. ip->d_ops = ip->i_mount->m_nondir_inode_ops;
  1040. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
  1041. }
  1042. /*
  1043. * Ensure all page cache allocations are done from GFP_NOFS context to
  1044. * prevent direct reclaim recursion back into the filesystem and blowing
  1045. * stacks or deadlocking.
  1046. */
  1047. gfp_mask = mapping_gfp_mask(inode->i_mapping);
  1048. mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
  1049. /*
  1050. * If there is no attribute fork no ACL can exist on this inode,
  1051. * and it can't have any file capabilities attached to it either.
  1052. */
  1053. if (!XFS_IFORK_Q(ip)) {
  1054. inode_has_no_xattr(inode);
  1055. cache_no_acl(inode);
  1056. }
  1057. }
  1058. void
  1059. xfs_setup_iops(
  1060. struct xfs_inode *ip)
  1061. {
  1062. struct inode *inode = &ip->i_vnode;
  1063. switch (inode->i_mode & S_IFMT) {
  1064. case S_IFREG:
  1065. inode->i_op = &xfs_inode_operations;
  1066. inode->i_fop = &xfs_file_operations;
  1067. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1068. break;
  1069. case S_IFDIR:
  1070. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  1071. inode->i_op = &xfs_dir_ci_inode_operations;
  1072. else
  1073. inode->i_op = &xfs_dir_inode_operations;
  1074. inode->i_fop = &xfs_dir_file_operations;
  1075. break;
  1076. case S_IFLNK:
  1077. if (ip->i_df.if_flags & XFS_IFINLINE)
  1078. inode->i_op = &xfs_inline_symlink_inode_operations;
  1079. else
  1080. inode->i_op = &xfs_symlink_inode_operations;
  1081. break;
  1082. default:
  1083. inode->i_op = &xfs_inode_operations;
  1084. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1085. break;
  1086. }
  1087. }