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_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_quota.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_rtalloc.h"
  34. #include "xfs_error.h"
  35. #include "xfs_itable.h"
  36. #include "xfs_rw.h"
  37. #include "xfs_attr.h"
  38. #include "xfs_buf_item.h"
  39. #include "xfs_utils.h"
  40. #include "xfs_vnodeops.h"
  41. #include "xfs_inode_item.h"
  42. #include "xfs_trace.h"
  43. #include <linux/capability.h>
  44. #include <linux/xattr.h>
  45. #include <linux/namei.h>
  46. #include <linux/posix_acl.h>
  47. #include <linux/security.h>
  48. #include <linux/fiemap.h>
  49. #include <linux/slab.h>
  50. static int
  51. xfs_initxattrs(
  52. struct inode *inode,
  53. const struct xattr *xattr_array,
  54. void *fs_info)
  55. {
  56. const struct xattr *xattr;
  57. struct xfs_inode *ip = XFS_I(inode);
  58. int error = 0;
  59. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  60. error = xfs_attr_set(ip, xattr->name, xattr->value,
  61. xattr->value_len, ATTR_SECURE);
  62. if (error < 0)
  63. break;
  64. }
  65. return error;
  66. }
  67. /*
  68. * Hook in SELinux. This is not quite correct yet, what we really need
  69. * here (as we do for default ACLs) is a mechanism by which creation of
  70. * these attrs can be journalled at inode creation time (along with the
  71. * inode, of course, such that log replay can't cause these to be lost).
  72. */
  73. STATIC int
  74. xfs_init_security(
  75. struct inode *inode,
  76. struct inode *dir,
  77. const struct qstr *qstr)
  78. {
  79. return security_inode_init_security(inode, dir, qstr,
  80. &xfs_initxattrs, NULL);
  81. }
  82. static void
  83. xfs_dentry_to_name(
  84. struct xfs_name *namep,
  85. struct dentry *dentry)
  86. {
  87. namep->name = dentry->d_name.name;
  88. namep->len = dentry->d_name.len;
  89. }
  90. STATIC void
  91. xfs_cleanup_inode(
  92. struct inode *dir,
  93. struct inode *inode,
  94. struct dentry *dentry)
  95. {
  96. struct xfs_name teardown;
  97. /* Oh, the horror.
  98. * If we can't add the ACL or we fail in
  99. * xfs_init_security we must back out.
  100. * ENOSPC can hit here, among other things.
  101. */
  102. xfs_dentry_to_name(&teardown, dentry);
  103. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  104. iput(inode);
  105. }
  106. STATIC int
  107. xfs_vn_mknod(
  108. struct inode *dir,
  109. struct dentry *dentry,
  110. umode_t mode,
  111. dev_t rdev)
  112. {
  113. struct inode *inode;
  114. struct xfs_inode *ip = NULL;
  115. struct posix_acl *default_acl = NULL;
  116. struct xfs_name name;
  117. int error;
  118. /*
  119. * Irix uses Missed'em'V split, but doesn't want to see
  120. * the upper 5 bits of (14bit) major.
  121. */
  122. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  123. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  124. return -EINVAL;
  125. rdev = sysv_encode_dev(rdev);
  126. } else {
  127. rdev = 0;
  128. }
  129. if (IS_POSIXACL(dir)) {
  130. default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
  131. if (IS_ERR(default_acl))
  132. return PTR_ERR(default_acl);
  133. if (!default_acl)
  134. mode &= ~current_umask();
  135. }
  136. xfs_dentry_to_name(&name, dentry);
  137. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
  138. if (unlikely(error))
  139. goto out_free_acl;
  140. inode = VFS_I(ip);
  141. error = xfs_init_security(inode, dir, &dentry->d_name);
  142. if (unlikely(error))
  143. goto out_cleanup_inode;
  144. if (default_acl) {
  145. error = -xfs_inherit_acl(inode, default_acl);
  146. default_acl = NULL;
  147. if (unlikely(error))
  148. goto out_cleanup_inode;
  149. }
  150. d_instantiate(dentry, inode);
  151. return -error;
  152. out_cleanup_inode:
  153. xfs_cleanup_inode(dir, inode, dentry);
  154. out_free_acl:
  155. posix_acl_release(default_acl);
  156. return -error;
  157. }
  158. STATIC int
  159. xfs_vn_create(
  160. struct inode *dir,
  161. struct dentry *dentry,
  162. umode_t mode,
  163. struct nameidata *nd)
  164. {
  165. return xfs_vn_mknod(dir, dentry, mode, 0);
  166. }
  167. STATIC int
  168. xfs_vn_mkdir(
  169. struct inode *dir,
  170. struct dentry *dentry,
  171. umode_t mode)
  172. {
  173. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  174. }
  175. STATIC struct dentry *
  176. xfs_vn_lookup(
  177. struct inode *dir,
  178. struct dentry *dentry,
  179. struct nameidata *nd)
  180. {
  181. struct xfs_inode *cip;
  182. struct xfs_name name;
  183. int error;
  184. if (dentry->d_name.len >= MAXNAMELEN)
  185. return ERR_PTR(-ENAMETOOLONG);
  186. xfs_dentry_to_name(&name, dentry);
  187. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  188. if (unlikely(error)) {
  189. if (unlikely(error != ENOENT))
  190. return ERR_PTR(-error);
  191. d_add(dentry, NULL);
  192. return NULL;
  193. }
  194. return d_splice_alias(VFS_I(cip), dentry);
  195. }
  196. STATIC struct dentry *
  197. xfs_vn_ci_lookup(
  198. struct inode *dir,
  199. struct dentry *dentry,
  200. struct nameidata *nd)
  201. {
  202. struct xfs_inode *ip;
  203. struct xfs_name xname;
  204. struct xfs_name ci_name;
  205. struct qstr dname;
  206. int error;
  207. if (dentry->d_name.len >= MAXNAMELEN)
  208. return ERR_PTR(-ENAMETOOLONG);
  209. xfs_dentry_to_name(&xname, dentry);
  210. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  211. if (unlikely(error)) {
  212. if (unlikely(error != ENOENT))
  213. return ERR_PTR(-error);
  214. /*
  215. * call d_add(dentry, NULL) here when d_drop_negative_children
  216. * is called in xfs_vn_mknod (ie. allow negative dentries
  217. * with CI filesystems).
  218. */
  219. return NULL;
  220. }
  221. /* if exact match, just splice and exit */
  222. if (!ci_name.name)
  223. return d_splice_alias(VFS_I(ip), dentry);
  224. /* else case-insensitive match... */
  225. dname.name = ci_name.name;
  226. dname.len = ci_name.len;
  227. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  228. kmem_free(ci_name.name);
  229. return dentry;
  230. }
  231. STATIC int
  232. xfs_vn_link(
  233. struct dentry *old_dentry,
  234. struct inode *dir,
  235. struct dentry *dentry)
  236. {
  237. struct inode *inode = old_dentry->d_inode;
  238. struct xfs_name name;
  239. int error;
  240. xfs_dentry_to_name(&name, dentry);
  241. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  242. if (unlikely(error))
  243. return -error;
  244. ihold(inode);
  245. d_instantiate(dentry, inode);
  246. return 0;
  247. }
  248. STATIC int
  249. xfs_vn_unlink(
  250. struct inode *dir,
  251. struct dentry *dentry)
  252. {
  253. struct xfs_name name;
  254. int error;
  255. xfs_dentry_to_name(&name, dentry);
  256. error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
  257. if (error)
  258. return error;
  259. /*
  260. * With unlink, the VFS makes the dentry "negative": no inode,
  261. * but still hashed. This is incompatible with case-insensitive
  262. * mode, so invalidate (unhash) the dentry in CI-mode.
  263. */
  264. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  265. d_invalidate(dentry);
  266. return 0;
  267. }
  268. STATIC int
  269. xfs_vn_symlink(
  270. struct inode *dir,
  271. struct dentry *dentry,
  272. const char *symname)
  273. {
  274. struct inode *inode;
  275. struct xfs_inode *cip = NULL;
  276. struct xfs_name name;
  277. int error;
  278. umode_t mode;
  279. mode = S_IFLNK |
  280. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  281. xfs_dentry_to_name(&name, dentry);
  282. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
  283. if (unlikely(error))
  284. goto out;
  285. inode = VFS_I(cip);
  286. error = xfs_init_security(inode, dir, &dentry->d_name);
  287. if (unlikely(error))
  288. goto out_cleanup_inode;
  289. d_instantiate(dentry, inode);
  290. return 0;
  291. out_cleanup_inode:
  292. xfs_cleanup_inode(dir, inode, dentry);
  293. out:
  294. return -error;
  295. }
  296. STATIC int
  297. xfs_vn_rename(
  298. struct inode *odir,
  299. struct dentry *odentry,
  300. struct inode *ndir,
  301. struct dentry *ndentry)
  302. {
  303. struct inode *new_inode = ndentry->d_inode;
  304. struct xfs_name oname;
  305. struct xfs_name nname;
  306. xfs_dentry_to_name(&oname, odentry);
  307. xfs_dentry_to_name(&nname, ndentry);
  308. return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  309. XFS_I(ndir), &nname, new_inode ?
  310. XFS_I(new_inode) : NULL);
  311. }
  312. /*
  313. * careful here - this function can get called recursively, so
  314. * we need to be very careful about how much stack we use.
  315. * uio is kmalloced for this reason...
  316. */
  317. STATIC void *
  318. xfs_vn_follow_link(
  319. struct dentry *dentry,
  320. struct nameidata *nd)
  321. {
  322. char *link;
  323. int error = -ENOMEM;
  324. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  325. if (!link)
  326. goto out_err;
  327. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  328. if (unlikely(error))
  329. goto out_kfree;
  330. nd_set_link(nd, link);
  331. return NULL;
  332. out_kfree:
  333. kfree(link);
  334. out_err:
  335. nd_set_link(nd, ERR_PTR(error));
  336. return NULL;
  337. }
  338. STATIC void
  339. xfs_vn_put_link(
  340. struct dentry *dentry,
  341. struct nameidata *nd,
  342. void *p)
  343. {
  344. char *s = nd_get_link(nd);
  345. if (!IS_ERR(s))
  346. kfree(s);
  347. }
  348. STATIC int
  349. xfs_vn_getattr(
  350. struct vfsmount *mnt,
  351. struct dentry *dentry,
  352. struct kstat *stat)
  353. {
  354. struct inode *inode = dentry->d_inode;
  355. struct xfs_inode *ip = XFS_I(inode);
  356. struct xfs_mount *mp = ip->i_mount;
  357. trace_xfs_getattr(ip);
  358. if (XFS_FORCED_SHUTDOWN(mp))
  359. return -XFS_ERROR(EIO);
  360. stat->size = XFS_ISIZE(ip);
  361. stat->dev = inode->i_sb->s_dev;
  362. stat->mode = ip->i_d.di_mode;
  363. stat->nlink = ip->i_d.di_nlink;
  364. stat->uid = ip->i_d.di_uid;
  365. stat->gid = ip->i_d.di_gid;
  366. stat->ino = ip->i_ino;
  367. stat->atime = inode->i_atime;
  368. stat->mtime = inode->i_mtime;
  369. stat->ctime = inode->i_ctime;
  370. stat->blocks =
  371. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  372. switch (inode->i_mode & S_IFMT) {
  373. case S_IFBLK:
  374. case S_IFCHR:
  375. stat->blksize = BLKDEV_IOSIZE;
  376. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  377. sysv_minor(ip->i_df.if_u2.if_rdev));
  378. break;
  379. default:
  380. if (XFS_IS_REALTIME_INODE(ip)) {
  381. /*
  382. * If the file blocks are being allocated from a
  383. * realtime volume, then return the inode's realtime
  384. * extent size or the realtime volume's extent size.
  385. */
  386. stat->blksize =
  387. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  388. } else
  389. stat->blksize = xfs_preferred_iosize(mp);
  390. stat->rdev = 0;
  391. break;
  392. }
  393. return 0;
  394. }
  395. int
  396. xfs_setattr_nonsize(
  397. struct xfs_inode *ip,
  398. struct iattr *iattr,
  399. int flags)
  400. {
  401. xfs_mount_t *mp = ip->i_mount;
  402. struct inode *inode = VFS_I(ip);
  403. int mask = iattr->ia_valid;
  404. xfs_trans_t *tp;
  405. int error;
  406. uid_t uid = 0, iuid = 0;
  407. gid_t gid = 0, igid = 0;
  408. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  409. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  410. trace_xfs_setattr(ip);
  411. if (mp->m_flags & XFS_MOUNT_RDONLY)
  412. return XFS_ERROR(EROFS);
  413. if (XFS_FORCED_SHUTDOWN(mp))
  414. return XFS_ERROR(EIO);
  415. error = -inode_change_ok(inode, iattr);
  416. if (error)
  417. return XFS_ERROR(error);
  418. ASSERT((mask & ATTR_SIZE) == 0);
  419. /*
  420. * If disk quotas is on, we make sure that the dquots do exist on disk,
  421. * before we start any other transactions. Trying to do this later
  422. * is messy. We don't care to take a readlock to look at the ids
  423. * in inode here, because we can't hold it across the trans_reserve.
  424. * If the IDs do change before we take the ilock, we're covered
  425. * because the i_*dquot fields will get updated anyway.
  426. */
  427. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  428. uint qflags = 0;
  429. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  430. uid = iattr->ia_uid;
  431. qflags |= XFS_QMOPT_UQUOTA;
  432. } else {
  433. uid = ip->i_d.di_uid;
  434. }
  435. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  436. gid = iattr->ia_gid;
  437. qflags |= XFS_QMOPT_GQUOTA;
  438. } else {
  439. gid = ip->i_d.di_gid;
  440. }
  441. /*
  442. * We take a reference when we initialize udqp and gdqp,
  443. * so it is important that we never blindly double trip on
  444. * the same variable. See xfs_create() for an example.
  445. */
  446. ASSERT(udqp == NULL);
  447. ASSERT(gdqp == NULL);
  448. error = xfs_qm_vop_dqalloc(ip, uid, gid, xfs_get_projid(ip),
  449. qflags, &udqp, &gdqp);
  450. if (error)
  451. return error;
  452. }
  453. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  454. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  455. if (error)
  456. goto out_dqrele;
  457. xfs_ilock(ip, XFS_ILOCK_EXCL);
  458. /*
  459. * Change file ownership. Must be the owner or privileged.
  460. */
  461. if (mask & (ATTR_UID|ATTR_GID)) {
  462. /*
  463. * These IDs could have changed since we last looked at them.
  464. * But, we're assured that if the ownership did change
  465. * while we didn't have the inode locked, inode's dquot(s)
  466. * would have changed also.
  467. */
  468. iuid = ip->i_d.di_uid;
  469. igid = ip->i_d.di_gid;
  470. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  471. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  472. /*
  473. * Do a quota reservation only if uid/gid is actually
  474. * going to change.
  475. */
  476. if (XFS_IS_QUOTA_RUNNING(mp) &&
  477. ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  478. (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
  479. ASSERT(tp);
  480. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  481. capable(CAP_FOWNER) ?
  482. XFS_QMOPT_FORCE_RES : 0);
  483. if (error) /* out of quota */
  484. goto out_trans_cancel;
  485. }
  486. }
  487. xfs_trans_ijoin(tp, ip, 0);
  488. /*
  489. * Change file ownership. Must be the owner or privileged.
  490. */
  491. if (mask & (ATTR_UID|ATTR_GID)) {
  492. /*
  493. * CAP_FSETID overrides the following restrictions:
  494. *
  495. * The set-user-ID and set-group-ID bits of a file will be
  496. * cleared upon successful return from chown()
  497. */
  498. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  499. !capable(CAP_FSETID))
  500. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  501. /*
  502. * Change the ownerships and register quota modifications
  503. * in the transaction.
  504. */
  505. if (iuid != uid) {
  506. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  507. ASSERT(mask & ATTR_UID);
  508. ASSERT(udqp);
  509. olddquot1 = xfs_qm_vop_chown(tp, ip,
  510. &ip->i_udquot, udqp);
  511. }
  512. ip->i_d.di_uid = uid;
  513. inode->i_uid = uid;
  514. }
  515. if (igid != gid) {
  516. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  517. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  518. ASSERT(mask & ATTR_GID);
  519. ASSERT(gdqp);
  520. olddquot2 = xfs_qm_vop_chown(tp, ip,
  521. &ip->i_gdquot, gdqp);
  522. }
  523. ip->i_d.di_gid = gid;
  524. inode->i_gid = gid;
  525. }
  526. }
  527. /*
  528. * Change file access modes.
  529. */
  530. if (mask & ATTR_MODE) {
  531. umode_t mode = iattr->ia_mode;
  532. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  533. mode &= ~S_ISGID;
  534. ip->i_d.di_mode &= S_IFMT;
  535. ip->i_d.di_mode |= mode & ~S_IFMT;
  536. inode->i_mode &= S_IFMT;
  537. inode->i_mode |= mode & ~S_IFMT;
  538. }
  539. /*
  540. * Change file access or modified times.
  541. */
  542. if (mask & ATTR_ATIME) {
  543. inode->i_atime = iattr->ia_atime;
  544. ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
  545. ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
  546. }
  547. if (mask & ATTR_CTIME) {
  548. inode->i_ctime = iattr->ia_ctime;
  549. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  550. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  551. }
  552. if (mask & ATTR_MTIME) {
  553. inode->i_mtime = iattr->ia_mtime;
  554. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  555. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  556. }
  557. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  558. XFS_STATS_INC(xs_ig_attrchg);
  559. if (mp->m_flags & XFS_MOUNT_WSYNC)
  560. xfs_trans_set_sync(tp);
  561. error = xfs_trans_commit(tp, 0);
  562. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  563. /*
  564. * Release any dquot(s) the inode had kept before chown.
  565. */
  566. xfs_qm_dqrele(olddquot1);
  567. xfs_qm_dqrele(olddquot2);
  568. xfs_qm_dqrele(udqp);
  569. xfs_qm_dqrele(gdqp);
  570. if (error)
  571. return XFS_ERROR(error);
  572. /*
  573. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  574. * update. We could avoid this with linked transactions
  575. * and passing down the transaction pointer all the way
  576. * to attr_set. No previous user of the generic
  577. * Posix ACL code seems to care about this issue either.
  578. */
  579. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  580. error = -xfs_acl_chmod(inode);
  581. if (error)
  582. return XFS_ERROR(error);
  583. }
  584. return 0;
  585. out_trans_cancel:
  586. xfs_trans_cancel(tp, 0);
  587. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  588. out_dqrele:
  589. xfs_qm_dqrele(udqp);
  590. xfs_qm_dqrele(gdqp);
  591. return error;
  592. }
  593. /*
  594. * Truncate file. Must have write permission and not be a directory.
  595. */
  596. int
  597. xfs_setattr_size(
  598. struct xfs_inode *ip,
  599. struct iattr *iattr,
  600. int flags)
  601. {
  602. struct xfs_mount *mp = ip->i_mount;
  603. struct inode *inode = VFS_I(ip);
  604. int mask = iattr->ia_valid;
  605. xfs_off_t oldsize, newsize;
  606. struct xfs_trans *tp;
  607. int error;
  608. uint lock_flags = 0;
  609. uint commit_flags = 0;
  610. trace_xfs_setattr(ip);
  611. if (mp->m_flags & XFS_MOUNT_RDONLY)
  612. return XFS_ERROR(EROFS);
  613. if (XFS_FORCED_SHUTDOWN(mp))
  614. return XFS_ERROR(EIO);
  615. error = -inode_change_ok(inode, iattr);
  616. if (error)
  617. return XFS_ERROR(error);
  618. ASSERT(S_ISREG(ip->i_d.di_mode));
  619. ASSERT((mask & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  620. ATTR_MTIME_SET|ATTR_KILL_SUID|ATTR_KILL_SGID|
  621. ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  622. if (!(flags & XFS_ATTR_NOLOCK)) {
  623. lock_flags |= XFS_IOLOCK_EXCL;
  624. xfs_ilock(ip, lock_flags);
  625. }
  626. oldsize = inode->i_size;
  627. newsize = iattr->ia_size;
  628. /*
  629. * Short circuit the truncate case for zero length files.
  630. */
  631. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  632. if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
  633. goto out_unlock;
  634. /*
  635. * Use the regular setattr path to update the timestamps.
  636. */
  637. xfs_iunlock(ip, lock_flags);
  638. iattr->ia_valid &= ~ATTR_SIZE;
  639. return xfs_setattr_nonsize(ip, iattr, 0);
  640. }
  641. /*
  642. * Make sure that the dquots are attached to the inode.
  643. */
  644. error = xfs_qm_dqattach(ip, 0);
  645. if (error)
  646. goto out_unlock;
  647. /*
  648. * Now we can make the changes. Before we join the inode to the
  649. * transaction, take care of the part of the truncation that must be
  650. * done without the inode lock. This needs to be done before joining
  651. * the inode to the transaction, because the inode cannot be unlocked
  652. * once it is a part of the transaction.
  653. */
  654. if (newsize > oldsize) {
  655. /*
  656. * Do the first part of growing a file: zero any data in the
  657. * last block that is beyond the old EOF. We need to do this
  658. * before the inode is joined to the transaction to modify
  659. * i_size.
  660. */
  661. error = xfs_zero_eof(ip, newsize, oldsize);
  662. if (error)
  663. goto out_unlock;
  664. }
  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. }