xfs_iops.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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_acl.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_quota.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_rtalloc.h"
  35. #include "xfs_error.h"
  36. #include "xfs_itable.h"
  37. #include "xfs_rw.h"
  38. #include "xfs_attr.h"
  39. #include "xfs_buf_item.h"
  40. #include "xfs_utils.h"
  41. #include "xfs_vnodeops.h"
  42. #include "xfs_inode_item.h"
  43. #include "xfs_trace.h"
  44. #include <linux/capability.h>
  45. #include <linux/xattr.h>
  46. #include <linux/namei.h>
  47. #include <linux/posix_acl.h>
  48. #include <linux/security.h>
  49. #include <linux/fiemap.h>
  50. #include <linux/slab.h>
  51. static int
  52. xfs_initxattrs(
  53. struct inode *inode,
  54. const struct xattr *xattr_array,
  55. void *fs_info)
  56. {
  57. const struct xattr *xattr;
  58. struct xfs_inode *ip = XFS_I(inode);
  59. int error = 0;
  60. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  61. error = xfs_attr_set(ip, xattr->name, xattr->value,
  62. xattr->value_len, ATTR_SECURE);
  63. if (error < 0)
  64. break;
  65. }
  66. return error;
  67. }
  68. /*
  69. * Hook in SELinux. This is not quite correct yet, what we really need
  70. * here (as we do for default ACLs) is a mechanism by which creation of
  71. * these attrs can be journalled at inode creation time (along with the
  72. * inode, of course, such that log replay can't cause these to be lost).
  73. */
  74. STATIC int
  75. xfs_init_security(
  76. struct inode *inode,
  77. struct inode *dir,
  78. const struct qstr *qstr)
  79. {
  80. return security_inode_init_security(inode, dir, qstr,
  81. &xfs_initxattrs, NULL);
  82. }
  83. static void
  84. xfs_dentry_to_name(
  85. struct xfs_name *namep,
  86. struct dentry *dentry)
  87. {
  88. namep->name = dentry->d_name.name;
  89. namep->len = dentry->d_name.len;
  90. }
  91. STATIC void
  92. xfs_cleanup_inode(
  93. struct inode *dir,
  94. struct inode *inode,
  95. struct dentry *dentry)
  96. {
  97. struct xfs_name teardown;
  98. /* Oh, the horror.
  99. * If we can't add the ACL or we fail in
  100. * xfs_init_security we must back out.
  101. * ENOSPC can hit here, among other things.
  102. */
  103. xfs_dentry_to_name(&teardown, dentry);
  104. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  105. iput(inode);
  106. }
  107. STATIC int
  108. xfs_vn_mknod(
  109. struct inode *dir,
  110. struct dentry *dentry,
  111. umode_t mode,
  112. dev_t rdev)
  113. {
  114. struct inode *inode;
  115. struct xfs_inode *ip = NULL;
  116. struct posix_acl *default_acl = NULL;
  117. struct xfs_name name;
  118. int error;
  119. /*
  120. * Irix uses Missed'em'V split, but doesn't want to see
  121. * the upper 5 bits of (14bit) major.
  122. */
  123. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  124. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  125. return -EINVAL;
  126. rdev = sysv_encode_dev(rdev);
  127. } else {
  128. rdev = 0;
  129. }
  130. if (IS_POSIXACL(dir)) {
  131. default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
  132. if (IS_ERR(default_acl))
  133. return PTR_ERR(default_acl);
  134. if (!default_acl)
  135. mode &= ~current_umask();
  136. }
  137. xfs_dentry_to_name(&name, dentry);
  138. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
  139. if (unlikely(error))
  140. goto out_free_acl;
  141. inode = VFS_I(ip);
  142. error = xfs_init_security(inode, dir, &dentry->d_name);
  143. if (unlikely(error))
  144. goto out_cleanup_inode;
  145. if (default_acl) {
  146. error = -xfs_inherit_acl(inode, default_acl);
  147. default_acl = NULL;
  148. if (unlikely(error))
  149. goto out_cleanup_inode;
  150. }
  151. d_instantiate(dentry, inode);
  152. return -error;
  153. out_cleanup_inode:
  154. xfs_cleanup_inode(dir, inode, dentry);
  155. out_free_acl:
  156. posix_acl_release(default_acl);
  157. return -error;
  158. }
  159. STATIC int
  160. xfs_vn_create(
  161. struct inode *dir,
  162. struct dentry *dentry,
  163. umode_t mode,
  164. struct nameidata *nd)
  165. {
  166. return xfs_vn_mknod(dir, dentry, mode, 0);
  167. }
  168. STATIC int
  169. xfs_vn_mkdir(
  170. struct inode *dir,
  171. struct dentry *dentry,
  172. umode_t mode)
  173. {
  174. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  175. }
  176. STATIC struct dentry *
  177. xfs_vn_lookup(
  178. struct inode *dir,
  179. struct dentry *dentry,
  180. struct nameidata *nd)
  181. {
  182. struct xfs_inode *cip;
  183. struct xfs_name name;
  184. int error;
  185. if (dentry->d_name.len >= MAXNAMELEN)
  186. return ERR_PTR(-ENAMETOOLONG);
  187. xfs_dentry_to_name(&name, dentry);
  188. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  189. if (unlikely(error)) {
  190. if (unlikely(error != ENOENT))
  191. return ERR_PTR(-error);
  192. d_add(dentry, NULL);
  193. return NULL;
  194. }
  195. return d_splice_alias(VFS_I(cip), dentry);
  196. }
  197. STATIC struct dentry *
  198. xfs_vn_ci_lookup(
  199. struct inode *dir,
  200. struct dentry *dentry,
  201. struct nameidata *nd)
  202. {
  203. struct xfs_inode *ip;
  204. struct xfs_name xname;
  205. struct xfs_name ci_name;
  206. struct qstr dname;
  207. int error;
  208. if (dentry->d_name.len >= MAXNAMELEN)
  209. return ERR_PTR(-ENAMETOOLONG);
  210. xfs_dentry_to_name(&xname, dentry);
  211. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  212. if (unlikely(error)) {
  213. if (unlikely(error != ENOENT))
  214. return ERR_PTR(-error);
  215. /*
  216. * call d_add(dentry, NULL) here when d_drop_negative_children
  217. * is called in xfs_vn_mknod (ie. allow negative dentries
  218. * with CI filesystems).
  219. */
  220. return NULL;
  221. }
  222. /* if exact match, just splice and exit */
  223. if (!ci_name.name)
  224. return d_splice_alias(VFS_I(ip), dentry);
  225. /* else case-insensitive match... */
  226. dname.name = ci_name.name;
  227. dname.len = ci_name.len;
  228. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  229. kmem_free(ci_name.name);
  230. return dentry;
  231. }
  232. STATIC int
  233. xfs_vn_link(
  234. struct dentry *old_dentry,
  235. struct inode *dir,
  236. struct dentry *dentry)
  237. {
  238. struct inode *inode = old_dentry->d_inode;
  239. struct xfs_name name;
  240. int error;
  241. xfs_dentry_to_name(&name, dentry);
  242. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  243. if (unlikely(error))
  244. return -error;
  245. ihold(inode);
  246. d_instantiate(dentry, inode);
  247. return 0;
  248. }
  249. STATIC int
  250. xfs_vn_unlink(
  251. struct inode *dir,
  252. struct dentry *dentry)
  253. {
  254. struct xfs_name name;
  255. int error;
  256. xfs_dentry_to_name(&name, dentry);
  257. error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
  258. if (error)
  259. return error;
  260. /*
  261. * With unlink, the VFS makes the dentry "negative": no inode,
  262. * but still hashed. This is incompatible with case-insensitive
  263. * mode, so invalidate (unhash) the dentry in CI-mode.
  264. */
  265. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  266. d_invalidate(dentry);
  267. return 0;
  268. }
  269. STATIC int
  270. xfs_vn_symlink(
  271. struct inode *dir,
  272. struct dentry *dentry,
  273. const char *symname)
  274. {
  275. struct inode *inode;
  276. struct xfs_inode *cip = NULL;
  277. struct xfs_name name;
  278. int error;
  279. umode_t mode;
  280. mode = S_IFLNK |
  281. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  282. xfs_dentry_to_name(&name, dentry);
  283. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
  284. if (unlikely(error))
  285. goto out;
  286. inode = VFS_I(cip);
  287. error = xfs_init_security(inode, dir, &dentry->d_name);
  288. if (unlikely(error))
  289. goto out_cleanup_inode;
  290. d_instantiate(dentry, inode);
  291. return 0;
  292. out_cleanup_inode:
  293. xfs_cleanup_inode(dir, inode, dentry);
  294. out:
  295. return -error;
  296. }
  297. STATIC int
  298. xfs_vn_rename(
  299. struct inode *odir,
  300. struct dentry *odentry,
  301. struct inode *ndir,
  302. struct dentry *ndentry)
  303. {
  304. struct inode *new_inode = ndentry->d_inode;
  305. struct xfs_name oname;
  306. struct xfs_name nname;
  307. xfs_dentry_to_name(&oname, odentry);
  308. xfs_dentry_to_name(&nname, ndentry);
  309. return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  310. XFS_I(ndir), &nname, new_inode ?
  311. XFS_I(new_inode) : NULL);
  312. }
  313. /*
  314. * careful here - this function can get called recursively, so
  315. * we need to be very careful about how much stack we use.
  316. * uio is kmalloced for this reason...
  317. */
  318. STATIC void *
  319. xfs_vn_follow_link(
  320. struct dentry *dentry,
  321. struct nameidata *nd)
  322. {
  323. char *link;
  324. int error = -ENOMEM;
  325. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  326. if (!link)
  327. goto out_err;
  328. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  329. if (unlikely(error))
  330. goto out_kfree;
  331. nd_set_link(nd, link);
  332. return NULL;
  333. out_kfree:
  334. kfree(link);
  335. out_err:
  336. nd_set_link(nd, ERR_PTR(error));
  337. return NULL;
  338. }
  339. STATIC void
  340. xfs_vn_put_link(
  341. struct dentry *dentry,
  342. struct nameidata *nd,
  343. void *p)
  344. {
  345. char *s = nd_get_link(nd);
  346. if (!IS_ERR(s))
  347. kfree(s);
  348. }
  349. STATIC int
  350. xfs_vn_getattr(
  351. struct vfsmount *mnt,
  352. struct dentry *dentry,
  353. struct kstat *stat)
  354. {
  355. struct inode *inode = dentry->d_inode;
  356. struct xfs_inode *ip = XFS_I(inode);
  357. struct xfs_mount *mp = ip->i_mount;
  358. trace_xfs_getattr(ip);
  359. if (XFS_FORCED_SHUTDOWN(mp))
  360. return -XFS_ERROR(EIO);
  361. stat->size = XFS_ISIZE(ip);
  362. stat->dev = inode->i_sb->s_dev;
  363. stat->mode = ip->i_d.di_mode;
  364. stat->nlink = ip->i_d.di_nlink;
  365. stat->uid = ip->i_d.di_uid;
  366. stat->gid = ip->i_d.di_gid;
  367. stat->ino = ip->i_ino;
  368. stat->atime = inode->i_atime;
  369. stat->mtime = inode->i_mtime;
  370. stat->ctime = inode->i_ctime;
  371. stat->blocks =
  372. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  373. switch (inode->i_mode & S_IFMT) {
  374. case S_IFBLK:
  375. case S_IFCHR:
  376. stat->blksize = BLKDEV_IOSIZE;
  377. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  378. sysv_minor(ip->i_df.if_u2.if_rdev));
  379. break;
  380. default:
  381. if (XFS_IS_REALTIME_INODE(ip)) {
  382. /*
  383. * If the file blocks are being allocated from a
  384. * realtime volume, then return the inode's realtime
  385. * extent size or the realtime volume's extent size.
  386. */
  387. stat->blksize =
  388. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  389. } else
  390. stat->blksize = xfs_preferred_iosize(mp);
  391. stat->rdev = 0;
  392. break;
  393. }
  394. return 0;
  395. }
  396. int
  397. xfs_setattr_nonsize(
  398. struct xfs_inode *ip,
  399. struct iattr *iattr,
  400. int flags)
  401. {
  402. xfs_mount_t *mp = ip->i_mount;
  403. struct inode *inode = VFS_I(ip);
  404. int mask = iattr->ia_valid;
  405. xfs_trans_t *tp;
  406. int error;
  407. uid_t uid = 0, iuid = 0;
  408. gid_t gid = 0, igid = 0;
  409. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  410. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  411. trace_xfs_setattr(ip);
  412. if (mp->m_flags & XFS_MOUNT_RDONLY)
  413. return XFS_ERROR(EROFS);
  414. if (XFS_FORCED_SHUTDOWN(mp))
  415. return XFS_ERROR(EIO);
  416. error = -inode_change_ok(inode, iattr);
  417. if (error)
  418. return XFS_ERROR(error);
  419. ASSERT((mask & ATTR_SIZE) == 0);
  420. /*
  421. * If disk quotas is on, we make sure that the dquots do exist on disk,
  422. * before we start any other transactions. Trying to do this later
  423. * is messy. We don't care to take a readlock to look at the ids
  424. * in inode here, because we can't hold it across the trans_reserve.
  425. * If the IDs do change before we take the ilock, we're covered
  426. * because the i_*dquot fields will get updated anyway.
  427. */
  428. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  429. uint qflags = 0;
  430. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  431. uid = iattr->ia_uid;
  432. qflags |= XFS_QMOPT_UQUOTA;
  433. } else {
  434. uid = ip->i_d.di_uid;
  435. }
  436. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  437. gid = iattr->ia_gid;
  438. qflags |= XFS_QMOPT_GQUOTA;
  439. } else {
  440. gid = ip->i_d.di_gid;
  441. }
  442. /*
  443. * We take a reference when we initialize udqp and gdqp,
  444. * so it is important that we never blindly double trip on
  445. * the same variable. See xfs_create() for an example.
  446. */
  447. ASSERT(udqp == NULL);
  448. ASSERT(gdqp == NULL);
  449. error = xfs_qm_vop_dqalloc(ip, uid, gid, xfs_get_projid(ip),
  450. qflags, &udqp, &gdqp);
  451. if (error)
  452. return error;
  453. }
  454. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  455. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  456. if (error)
  457. goto out_dqrele;
  458. xfs_ilock(ip, XFS_ILOCK_EXCL);
  459. /*
  460. * Change file ownership. Must be the owner or privileged.
  461. */
  462. if (mask & (ATTR_UID|ATTR_GID)) {
  463. /*
  464. * These IDs could have changed since we last looked at them.
  465. * But, we're assured that if the ownership did change
  466. * while we didn't have the inode locked, inode's dquot(s)
  467. * would have changed also.
  468. */
  469. iuid = ip->i_d.di_uid;
  470. igid = ip->i_d.di_gid;
  471. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  472. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  473. /*
  474. * Do a quota reservation only if uid/gid is actually
  475. * going to change.
  476. */
  477. if (XFS_IS_QUOTA_RUNNING(mp) &&
  478. ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  479. (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
  480. ASSERT(tp);
  481. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  482. capable(CAP_FOWNER) ?
  483. XFS_QMOPT_FORCE_RES : 0);
  484. if (error) /* out of quota */
  485. goto out_trans_cancel;
  486. }
  487. }
  488. xfs_trans_ijoin(tp, ip, 0);
  489. /*
  490. * Change file ownership. Must be the owner or privileged.
  491. */
  492. if (mask & (ATTR_UID|ATTR_GID)) {
  493. /*
  494. * CAP_FSETID overrides the following restrictions:
  495. *
  496. * The set-user-ID and set-group-ID bits of a file will be
  497. * cleared upon successful return from chown()
  498. */
  499. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  500. !capable(CAP_FSETID))
  501. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  502. /*
  503. * Change the ownerships and register quota modifications
  504. * in the transaction.
  505. */
  506. if (iuid != uid) {
  507. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  508. ASSERT(mask & ATTR_UID);
  509. ASSERT(udqp);
  510. olddquot1 = xfs_qm_vop_chown(tp, ip,
  511. &ip->i_udquot, udqp);
  512. }
  513. ip->i_d.di_uid = uid;
  514. inode->i_uid = uid;
  515. }
  516. if (igid != gid) {
  517. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  518. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  519. ASSERT(mask & ATTR_GID);
  520. ASSERT(gdqp);
  521. olddquot2 = xfs_qm_vop_chown(tp, ip,
  522. &ip->i_gdquot, gdqp);
  523. }
  524. ip->i_d.di_gid = gid;
  525. inode->i_gid = gid;
  526. }
  527. }
  528. /*
  529. * Change file access modes.
  530. */
  531. if (mask & ATTR_MODE) {
  532. umode_t mode = iattr->ia_mode;
  533. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  534. mode &= ~S_ISGID;
  535. ip->i_d.di_mode &= S_IFMT;
  536. ip->i_d.di_mode |= mode & ~S_IFMT;
  537. inode->i_mode &= S_IFMT;
  538. inode->i_mode |= mode & ~S_IFMT;
  539. }
  540. /*
  541. * Change file access or modified times.
  542. */
  543. if (mask & ATTR_ATIME) {
  544. inode->i_atime = iattr->ia_atime;
  545. ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
  546. ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
  547. }
  548. if (mask & ATTR_CTIME) {
  549. inode->i_ctime = iattr->ia_ctime;
  550. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  551. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  552. }
  553. if (mask & ATTR_MTIME) {
  554. inode->i_mtime = iattr->ia_mtime;
  555. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  556. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  557. }
  558. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  559. XFS_STATS_INC(xs_ig_attrchg);
  560. if (mp->m_flags & XFS_MOUNT_WSYNC)
  561. xfs_trans_set_sync(tp);
  562. error = xfs_trans_commit(tp, 0);
  563. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  564. /*
  565. * Release any dquot(s) the inode had kept before chown.
  566. */
  567. xfs_qm_dqrele(olddquot1);
  568. xfs_qm_dqrele(olddquot2);
  569. xfs_qm_dqrele(udqp);
  570. xfs_qm_dqrele(gdqp);
  571. if (error)
  572. return XFS_ERROR(error);
  573. /*
  574. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  575. * update. We could avoid this with linked transactions
  576. * and passing down the transaction pointer all the way
  577. * to attr_set. No previous user of the generic
  578. * Posix ACL code seems to care about this issue either.
  579. */
  580. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  581. error = -xfs_acl_chmod(inode);
  582. if (error)
  583. return XFS_ERROR(error);
  584. }
  585. return 0;
  586. out_trans_cancel:
  587. xfs_trans_cancel(tp, 0);
  588. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  589. out_dqrele:
  590. xfs_qm_dqrele(udqp);
  591. xfs_qm_dqrele(gdqp);
  592. return error;
  593. }
  594. /*
  595. * Truncate file. Must have write permission and not be a directory.
  596. */
  597. int
  598. xfs_setattr_size(
  599. struct xfs_inode *ip,
  600. struct iattr *iattr,
  601. int flags)
  602. {
  603. struct xfs_mount *mp = ip->i_mount;
  604. struct inode *inode = VFS_I(ip);
  605. int mask = iattr->ia_valid;
  606. xfs_off_t oldsize, newsize;
  607. struct xfs_trans *tp;
  608. int error;
  609. uint lock_flags;
  610. uint commit_flags = 0;
  611. trace_xfs_setattr(ip);
  612. if (mp->m_flags & XFS_MOUNT_RDONLY)
  613. return XFS_ERROR(EROFS);
  614. if (XFS_FORCED_SHUTDOWN(mp))
  615. return XFS_ERROR(EIO);
  616. error = -inode_change_ok(inode, iattr);
  617. if (error)
  618. return XFS_ERROR(error);
  619. ASSERT(S_ISREG(ip->i_d.di_mode));
  620. ASSERT((mask & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  621. ATTR_MTIME_SET|ATTR_KILL_SUID|ATTR_KILL_SGID|
  622. ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  623. lock_flags = XFS_ILOCK_EXCL;
  624. if (!(flags & XFS_ATTR_NOLOCK))
  625. lock_flags |= XFS_IOLOCK_EXCL;
  626. xfs_ilock(ip, lock_flags);
  627. oldsize = inode->i_size;
  628. newsize = iattr->ia_size;
  629. /*
  630. * Short circuit the truncate case for zero length files.
  631. */
  632. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  633. if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
  634. goto out_unlock;
  635. /*
  636. * Use the regular setattr path to update the timestamps.
  637. */
  638. xfs_iunlock(ip, lock_flags);
  639. iattr->ia_valid &= ~ATTR_SIZE;
  640. return xfs_setattr_nonsize(ip, iattr, 0);
  641. }
  642. /*
  643. * Make sure that the dquots are attached to the inode.
  644. */
  645. error = xfs_qm_dqattach_locked(ip, 0);
  646. if (error)
  647. goto out_unlock;
  648. /*
  649. * Now we can make the changes. Before we join the inode to the
  650. * transaction, take care of the part of the truncation that must be
  651. * done without the inode lock. This needs to be done before joining
  652. * the inode to the transaction, because the inode cannot be unlocked
  653. * once it is a part of the transaction.
  654. */
  655. if (newsize > oldsize) {
  656. /*
  657. * Do the first part of growing a file: zero any data in the
  658. * last block that is beyond the old EOF. We need to do this
  659. * before the inode is joined to the transaction to modify
  660. * i_size.
  661. */
  662. error = xfs_zero_eof(ip, newsize, oldsize);
  663. if (error)
  664. goto out_unlock;
  665. }
  666. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  667. lock_flags &= ~XFS_ILOCK_EXCL;
  668. /*
  669. * We are going to log the inode size change in this transaction so
  670. * any previous writes that are beyond the on disk EOF and the new
  671. * EOF that have not been written out need to be written here. If we
  672. * do not write the data out, we expose ourselves to the null files
  673. * problem.
  674. *
  675. * Only flush from the on disk size to the smaller of the in memory
  676. * file size or the new size as that's the range we really care about
  677. * here and prevents waiting for other data not within the range we
  678. * care about here.
  679. */
  680. if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
  681. error = xfs_flush_pages(ip, ip->i_d.di_size, newsize, 0,
  682. FI_NONE);
  683. if (error)
  684. goto out_unlock;
  685. }
  686. /*
  687. * Wait for all direct I/O to complete.
  688. */
  689. inode_dio_wait(inode);
  690. error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
  691. if (error)
  692. goto out_unlock;
  693. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  694. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  695. XFS_TRANS_PERM_LOG_RES,
  696. XFS_ITRUNCATE_LOG_COUNT);
  697. if (error)
  698. goto out_trans_cancel;
  699. truncate_setsize(inode, newsize);
  700. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  701. lock_flags |= XFS_ILOCK_EXCL;
  702. xfs_ilock(ip, XFS_ILOCK_EXCL);
  703. xfs_trans_ijoin(tp, ip, 0);
  704. /*
  705. * Only change the c/mtime if we are changing the size or we are
  706. * explicitly asked to change it. This handles the semantic difference
  707. * between truncate() and ftruncate() as implemented in the VFS.
  708. *
  709. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  710. * special case where we need to update the times despite not having
  711. * these flags set. For all other operations the VFS set these flags
  712. * explicitly if it wants a timestamp update.
  713. */
  714. if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
  715. iattr->ia_ctime = iattr->ia_mtime =
  716. current_fs_time(inode->i_sb);
  717. mask |= ATTR_CTIME | ATTR_MTIME;
  718. }
  719. /*
  720. * The first thing we do is set the size to new_size permanently on
  721. * disk. This way we don't have to worry about anyone ever being able
  722. * to look at the data being freed even in the face of a crash.
  723. * What we're getting around here is the case where we free a block, it
  724. * is allocated to another file, it is written to, and then we crash.
  725. * If the new data gets written to the file but the log buffers
  726. * containing the free and reallocation don't, then we'd end up with
  727. * garbage in the blocks being freed. As long as we make the new size
  728. * permanent before actually freeing any blocks it doesn't matter if
  729. * they get written to.
  730. */
  731. ip->i_d.di_size = newsize;
  732. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  733. if (newsize <= oldsize) {
  734. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  735. if (error)
  736. goto out_trans_abort;
  737. /*
  738. * Truncated "down", so we're removing references to old data
  739. * here - if we delay flushing for a long time, we expose
  740. * ourselves unduly to the notorious NULL files problem. So,
  741. * we mark this inode and flush it when the file is closed,
  742. * and do not wait the usual (long) time for writeout.
  743. */
  744. xfs_iflags_set(ip, XFS_ITRUNCATED);
  745. }
  746. if (mask & ATTR_CTIME) {
  747. inode->i_ctime = iattr->ia_ctime;
  748. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  749. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  750. }
  751. if (mask & ATTR_MTIME) {
  752. inode->i_mtime = iattr->ia_mtime;
  753. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  754. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  755. }
  756. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  757. XFS_STATS_INC(xs_ig_attrchg);
  758. if (mp->m_flags & XFS_MOUNT_WSYNC)
  759. xfs_trans_set_sync(tp);
  760. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  761. out_unlock:
  762. if (lock_flags)
  763. xfs_iunlock(ip, lock_flags);
  764. return error;
  765. out_trans_abort:
  766. commit_flags |= XFS_TRANS_ABORT;
  767. out_trans_cancel:
  768. xfs_trans_cancel(tp, commit_flags);
  769. goto out_unlock;
  770. }
  771. STATIC int
  772. xfs_vn_setattr(
  773. struct dentry *dentry,
  774. struct iattr *iattr)
  775. {
  776. if (iattr->ia_valid & ATTR_SIZE)
  777. return -xfs_setattr_size(XFS_I(dentry->d_inode), iattr, 0);
  778. return -xfs_setattr_nonsize(XFS_I(dentry->d_inode), iattr, 0);
  779. }
  780. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  781. /*
  782. * Call fiemap helper to fill in user data.
  783. * Returns positive errors to xfs_getbmap.
  784. */
  785. STATIC int
  786. xfs_fiemap_format(
  787. void **arg,
  788. struct getbmapx *bmv,
  789. int *full)
  790. {
  791. int error;
  792. struct fiemap_extent_info *fieinfo = *arg;
  793. u32 fiemap_flags = 0;
  794. u64 logical, physical, length;
  795. /* Do nothing for a hole */
  796. if (bmv->bmv_block == -1LL)
  797. return 0;
  798. logical = BBTOB(bmv->bmv_offset);
  799. physical = BBTOB(bmv->bmv_block);
  800. length = BBTOB(bmv->bmv_length);
  801. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  802. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  803. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  804. fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
  805. physical = 0; /* no block yet */
  806. }
  807. if (bmv->bmv_oflags & BMV_OF_LAST)
  808. fiemap_flags |= FIEMAP_EXTENT_LAST;
  809. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  810. length, fiemap_flags);
  811. if (error > 0) {
  812. error = 0;
  813. *full = 1; /* user array now full */
  814. }
  815. return -error;
  816. }
  817. STATIC int
  818. xfs_vn_fiemap(
  819. struct inode *inode,
  820. struct fiemap_extent_info *fieinfo,
  821. u64 start,
  822. u64 length)
  823. {
  824. xfs_inode_t *ip = XFS_I(inode);
  825. struct getbmapx bm;
  826. int error;
  827. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  828. if (error)
  829. return error;
  830. /* Set up bmap header for xfs internal routine */
  831. bm.bmv_offset = BTOBB(start);
  832. /* Special case for whole file */
  833. if (length == FIEMAP_MAX_OFFSET)
  834. bm.bmv_length = -1LL;
  835. else
  836. bm.bmv_length = BTOBB(length);
  837. /* We add one because in getbmap world count includes the header */
  838. bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
  839. fieinfo->fi_extents_max + 1;
  840. bm.bmv_count = min_t(__s32, bm.bmv_count,
  841. (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
  842. bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
  843. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  844. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  845. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  846. bm.bmv_iflags |= BMV_IF_DELALLOC;
  847. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  848. if (error)
  849. return -error;
  850. return 0;
  851. }
  852. static const struct inode_operations xfs_inode_operations = {
  853. .get_acl = xfs_get_acl,
  854. .getattr = xfs_vn_getattr,
  855. .setattr = xfs_vn_setattr,
  856. .setxattr = generic_setxattr,
  857. .getxattr = generic_getxattr,
  858. .removexattr = generic_removexattr,
  859. .listxattr = xfs_vn_listxattr,
  860. .fiemap = xfs_vn_fiemap,
  861. };
  862. static const struct inode_operations xfs_dir_inode_operations = {
  863. .create = xfs_vn_create,
  864. .lookup = xfs_vn_lookup,
  865. .link = xfs_vn_link,
  866. .unlink = xfs_vn_unlink,
  867. .symlink = xfs_vn_symlink,
  868. .mkdir = xfs_vn_mkdir,
  869. /*
  870. * Yes, XFS uses the same method for rmdir and unlink.
  871. *
  872. * There are some subtile differences deeper in the code,
  873. * but we use S_ISDIR to check for those.
  874. */
  875. .rmdir = xfs_vn_unlink,
  876. .mknod = xfs_vn_mknod,
  877. .rename = xfs_vn_rename,
  878. .get_acl = xfs_get_acl,
  879. .getattr = xfs_vn_getattr,
  880. .setattr = xfs_vn_setattr,
  881. .setxattr = generic_setxattr,
  882. .getxattr = generic_getxattr,
  883. .removexattr = generic_removexattr,
  884. .listxattr = xfs_vn_listxattr,
  885. };
  886. static const struct inode_operations xfs_dir_ci_inode_operations = {
  887. .create = xfs_vn_create,
  888. .lookup = xfs_vn_ci_lookup,
  889. .link = xfs_vn_link,
  890. .unlink = xfs_vn_unlink,
  891. .symlink = xfs_vn_symlink,
  892. .mkdir = xfs_vn_mkdir,
  893. /*
  894. * Yes, XFS uses the same method for rmdir and unlink.
  895. *
  896. * There are some subtile differences deeper in the code,
  897. * but we use S_ISDIR to check for those.
  898. */
  899. .rmdir = xfs_vn_unlink,
  900. .mknod = xfs_vn_mknod,
  901. .rename = xfs_vn_rename,
  902. .get_acl = xfs_get_acl,
  903. .getattr = xfs_vn_getattr,
  904. .setattr = xfs_vn_setattr,
  905. .setxattr = generic_setxattr,
  906. .getxattr = generic_getxattr,
  907. .removexattr = generic_removexattr,
  908. .listxattr = xfs_vn_listxattr,
  909. };
  910. static const struct inode_operations xfs_symlink_inode_operations = {
  911. .readlink = generic_readlink,
  912. .follow_link = xfs_vn_follow_link,
  913. .put_link = xfs_vn_put_link,
  914. .get_acl = xfs_get_acl,
  915. .getattr = xfs_vn_getattr,
  916. .setattr = xfs_vn_setattr,
  917. .setxattr = generic_setxattr,
  918. .getxattr = generic_getxattr,
  919. .removexattr = generic_removexattr,
  920. .listxattr = xfs_vn_listxattr,
  921. };
  922. STATIC void
  923. xfs_diflags_to_iflags(
  924. struct inode *inode,
  925. struct xfs_inode *ip)
  926. {
  927. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  928. inode->i_flags |= S_IMMUTABLE;
  929. else
  930. inode->i_flags &= ~S_IMMUTABLE;
  931. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  932. inode->i_flags |= S_APPEND;
  933. else
  934. inode->i_flags &= ~S_APPEND;
  935. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  936. inode->i_flags |= S_SYNC;
  937. else
  938. inode->i_flags &= ~S_SYNC;
  939. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  940. inode->i_flags |= S_NOATIME;
  941. else
  942. inode->i_flags &= ~S_NOATIME;
  943. }
  944. /*
  945. * Initialize the Linux inode, set up the operation vectors and
  946. * unlock the inode.
  947. *
  948. * When reading existing inodes from disk this is called directly
  949. * from xfs_iget, when creating a new inode it is called from
  950. * xfs_ialloc after setting up the inode.
  951. *
  952. * We are always called with an uninitialised linux inode here.
  953. * We need to initialise the necessary fields and take a reference
  954. * on it.
  955. */
  956. void
  957. xfs_setup_inode(
  958. struct xfs_inode *ip)
  959. {
  960. struct inode *inode = &ip->i_vnode;
  961. inode->i_ino = ip->i_ino;
  962. inode->i_state = I_NEW;
  963. inode_sb_list_add(inode);
  964. /* make the inode look hashed for the writeback code */
  965. hlist_add_fake(&inode->i_hash);
  966. inode->i_mode = ip->i_d.di_mode;
  967. set_nlink(inode, ip->i_d.di_nlink);
  968. inode->i_uid = ip->i_d.di_uid;
  969. inode->i_gid = ip->i_d.di_gid;
  970. switch (inode->i_mode & S_IFMT) {
  971. case S_IFBLK:
  972. case S_IFCHR:
  973. inode->i_rdev =
  974. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  975. sysv_minor(ip->i_df.if_u2.if_rdev));
  976. break;
  977. default:
  978. inode->i_rdev = 0;
  979. break;
  980. }
  981. inode->i_generation = ip->i_d.di_gen;
  982. i_size_write(inode, ip->i_d.di_size);
  983. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  984. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  985. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  986. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  987. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  988. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  989. xfs_diflags_to_iflags(inode, ip);
  990. switch (inode->i_mode & S_IFMT) {
  991. case S_IFREG:
  992. inode->i_op = &xfs_inode_operations;
  993. inode->i_fop = &xfs_file_operations;
  994. inode->i_mapping->a_ops = &xfs_address_space_operations;
  995. break;
  996. case S_IFDIR:
  997. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  998. inode->i_op = &xfs_dir_ci_inode_operations;
  999. else
  1000. inode->i_op = &xfs_dir_inode_operations;
  1001. inode->i_fop = &xfs_dir_file_operations;
  1002. break;
  1003. case S_IFLNK:
  1004. inode->i_op = &xfs_symlink_inode_operations;
  1005. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  1006. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1007. break;
  1008. default:
  1009. inode->i_op = &xfs_inode_operations;
  1010. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1011. break;
  1012. }
  1013. /*
  1014. * If there is no attribute fork no ACL can exist on this inode,
  1015. * and it can't have any file capabilities attached to it either.
  1016. */
  1017. if (!XFS_IFORK_Q(ip)) {
  1018. inode_has_no_xattr(inode);
  1019. cache_no_acl(inode);
  1020. }
  1021. xfs_iflags_clear(ip, XFS_INEW);
  1022. barrier();
  1023. unlock_new_inode(inode);
  1024. }