xfs_iops.c 29 KB

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