xfs_iops.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_alloc.h"
  26. #include "xfs_quota.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_rtalloc.h"
  33. #include "xfs_error.h"
  34. #include "xfs_itable.h"
  35. #include "xfs_attr.h"
  36. #include "xfs_buf_item.h"
  37. #include "xfs_utils.h"
  38. #include "xfs_vnodeops.h"
  39. #include "xfs_inode_item.h"
  40. #include "xfs_trace.h"
  41. #include <linux/capability.h>
  42. #include <linux/xattr.h>
  43. #include <linux/namei.h>
  44. #include <linux/posix_acl.h>
  45. #include <linux/security.h>
  46. #include <linux/fiemap.h>
  47. #include <linux/slab.h>
  48. static int
  49. xfs_initxattrs(
  50. struct inode *inode,
  51. 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. bool flags)
  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. unsigned int flags)
  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. unsigned int flags)
  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 = 0;
  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. if (!(flags & XFS_ATTR_NOLOCK)) {
  621. lock_flags |= XFS_IOLOCK_EXCL;
  622. xfs_ilock(ip, lock_flags);
  623. }
  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(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. /*
  664. * We are going to log the inode size change in this transaction so
  665. * any previous writes that are beyond the on disk EOF and the new
  666. * EOF that have not been written out need to be written here. If we
  667. * do not write the data out, we expose ourselves to the null files
  668. * problem.
  669. *
  670. * Only flush from the on disk size to the smaller of the in memory
  671. * file size or the new size as that's the range we really care about
  672. * here and prevents waiting for other data not within the range we
  673. * care about here.
  674. */
  675. if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
  676. error = xfs_flush_pages(ip, ip->i_d.di_size, newsize, 0,
  677. FI_NONE);
  678. if (error)
  679. goto out_unlock;
  680. }
  681. /*
  682. * Wait for all direct I/O to complete.
  683. */
  684. inode_dio_wait(inode);
  685. error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
  686. if (error)
  687. goto out_unlock;
  688. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  689. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  690. XFS_TRANS_PERM_LOG_RES,
  691. XFS_ITRUNCATE_LOG_COUNT);
  692. if (error)
  693. goto out_trans_cancel;
  694. truncate_setsize(inode, newsize);
  695. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  696. lock_flags |= XFS_ILOCK_EXCL;
  697. xfs_ilock(ip, XFS_ILOCK_EXCL);
  698. xfs_trans_ijoin(tp, ip, 0);
  699. /*
  700. * Only change the c/mtime if we are changing the size or we are
  701. * explicitly asked to change it. This handles the semantic difference
  702. * between truncate() and ftruncate() as implemented in the VFS.
  703. *
  704. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  705. * special case where we need to update the times despite not having
  706. * these flags set. For all other operations the VFS set these flags
  707. * explicitly if it wants a timestamp update.
  708. */
  709. if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
  710. iattr->ia_ctime = iattr->ia_mtime =
  711. current_fs_time(inode->i_sb);
  712. mask |= ATTR_CTIME | ATTR_MTIME;
  713. }
  714. /*
  715. * The first thing we do is set the size to new_size permanently on
  716. * disk. This way we don't have to worry about anyone ever being able
  717. * to look at the data being freed even in the face of a crash.
  718. * What we're getting around here is the case where we free a block, it
  719. * is allocated to another file, it is written to, and then we crash.
  720. * If the new data gets written to the file but the log buffers
  721. * containing the free and reallocation don't, then we'd end up with
  722. * garbage in the blocks being freed. As long as we make the new size
  723. * permanent before actually freeing any blocks it doesn't matter if
  724. * they get written to.
  725. */
  726. ip->i_d.di_size = newsize;
  727. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  728. if (newsize <= oldsize) {
  729. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  730. if (error)
  731. goto out_trans_abort;
  732. /*
  733. * Truncated "down", so we're removing references to old data
  734. * here - if we delay flushing for a long time, we expose
  735. * ourselves unduly to the notorious NULL files problem. So,
  736. * we mark this inode and flush it when the file is closed,
  737. * and do not wait the usual (long) time for writeout.
  738. */
  739. xfs_iflags_set(ip, XFS_ITRUNCATED);
  740. }
  741. if (mask & ATTR_CTIME) {
  742. inode->i_ctime = iattr->ia_ctime;
  743. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  744. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  745. }
  746. if (mask & ATTR_MTIME) {
  747. inode->i_mtime = iattr->ia_mtime;
  748. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  749. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  750. }
  751. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  752. XFS_STATS_INC(xs_ig_attrchg);
  753. if (mp->m_flags & XFS_MOUNT_WSYNC)
  754. xfs_trans_set_sync(tp);
  755. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  756. out_unlock:
  757. if (lock_flags)
  758. xfs_iunlock(ip, lock_flags);
  759. return error;
  760. out_trans_abort:
  761. commit_flags |= XFS_TRANS_ABORT;
  762. out_trans_cancel:
  763. xfs_trans_cancel(tp, commit_flags);
  764. goto out_unlock;
  765. }
  766. STATIC int
  767. xfs_vn_setattr(
  768. struct dentry *dentry,
  769. struct iattr *iattr)
  770. {
  771. if (iattr->ia_valid & ATTR_SIZE)
  772. return -xfs_setattr_size(XFS_I(dentry->d_inode), iattr, 0);
  773. return -xfs_setattr_nonsize(XFS_I(dentry->d_inode), iattr, 0);
  774. }
  775. STATIC int
  776. xfs_vn_update_time(
  777. struct inode *inode,
  778. struct timespec *now,
  779. int flags)
  780. {
  781. struct xfs_inode *ip = XFS_I(inode);
  782. struct xfs_mount *mp = ip->i_mount;
  783. struct xfs_trans *tp;
  784. int error;
  785. trace_xfs_update_time(ip);
  786. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  787. error = xfs_trans_reserve(tp, 0, XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
  788. if (error) {
  789. xfs_trans_cancel(tp, 0);
  790. return -error;
  791. }
  792. xfs_ilock(ip, XFS_ILOCK_EXCL);
  793. if (flags & S_CTIME) {
  794. inode->i_ctime = *now;
  795. ip->i_d.di_ctime.t_sec = (__int32_t)now->tv_sec;
  796. ip->i_d.di_ctime.t_nsec = (__int32_t)now->tv_nsec;
  797. }
  798. if (flags & S_MTIME) {
  799. inode->i_mtime = *now;
  800. ip->i_d.di_mtime.t_sec = (__int32_t)now->tv_sec;
  801. ip->i_d.di_mtime.t_nsec = (__int32_t)now->tv_nsec;
  802. }
  803. if (flags & S_ATIME) {
  804. inode->i_atime = *now;
  805. ip->i_d.di_atime.t_sec = (__int32_t)now->tv_sec;
  806. ip->i_d.di_atime.t_nsec = (__int32_t)now->tv_nsec;
  807. }
  808. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  809. xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
  810. return -xfs_trans_commit(tp, 0);
  811. }
  812. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  813. /*
  814. * Call fiemap helper to fill in user data.
  815. * Returns positive errors to xfs_getbmap.
  816. */
  817. STATIC int
  818. xfs_fiemap_format(
  819. void **arg,
  820. struct getbmapx *bmv,
  821. int *full)
  822. {
  823. int error;
  824. struct fiemap_extent_info *fieinfo = *arg;
  825. u32 fiemap_flags = 0;
  826. u64 logical, physical, length;
  827. /* Do nothing for a hole */
  828. if (bmv->bmv_block == -1LL)
  829. return 0;
  830. logical = BBTOB(bmv->bmv_offset);
  831. physical = BBTOB(bmv->bmv_block);
  832. length = BBTOB(bmv->bmv_length);
  833. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  834. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  835. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  836. fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
  837. physical = 0; /* no block yet */
  838. }
  839. if (bmv->bmv_oflags & BMV_OF_LAST)
  840. fiemap_flags |= FIEMAP_EXTENT_LAST;
  841. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  842. length, fiemap_flags);
  843. if (error > 0) {
  844. error = 0;
  845. *full = 1; /* user array now full */
  846. }
  847. return -error;
  848. }
  849. STATIC int
  850. xfs_vn_fiemap(
  851. struct inode *inode,
  852. struct fiemap_extent_info *fieinfo,
  853. u64 start,
  854. u64 length)
  855. {
  856. xfs_inode_t *ip = XFS_I(inode);
  857. struct getbmapx bm;
  858. int error;
  859. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  860. if (error)
  861. return error;
  862. /* Set up bmap header for xfs internal routine */
  863. bm.bmv_offset = BTOBB(start);
  864. /* Special case for whole file */
  865. if (length == FIEMAP_MAX_OFFSET)
  866. bm.bmv_length = -1LL;
  867. else
  868. bm.bmv_length = BTOBB(length);
  869. /* We add one because in getbmap world count includes the header */
  870. bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
  871. fieinfo->fi_extents_max + 1;
  872. bm.bmv_count = min_t(__s32, bm.bmv_count,
  873. (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
  874. bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
  875. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  876. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  877. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  878. bm.bmv_iflags |= BMV_IF_DELALLOC;
  879. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  880. if (error)
  881. return -error;
  882. return 0;
  883. }
  884. static const struct inode_operations xfs_inode_operations = {
  885. .get_acl = xfs_get_acl,
  886. .getattr = xfs_vn_getattr,
  887. .setattr = xfs_vn_setattr,
  888. .setxattr = generic_setxattr,
  889. .getxattr = generic_getxattr,
  890. .removexattr = generic_removexattr,
  891. .listxattr = xfs_vn_listxattr,
  892. .fiemap = xfs_vn_fiemap,
  893. .update_time = xfs_vn_update_time,
  894. };
  895. static const struct inode_operations xfs_dir_inode_operations = {
  896. .create = xfs_vn_create,
  897. .lookup = xfs_vn_lookup,
  898. .link = xfs_vn_link,
  899. .unlink = xfs_vn_unlink,
  900. .symlink = xfs_vn_symlink,
  901. .mkdir = xfs_vn_mkdir,
  902. /*
  903. * Yes, XFS uses the same method for rmdir and unlink.
  904. *
  905. * There are some subtile differences deeper in the code,
  906. * but we use S_ISDIR to check for those.
  907. */
  908. .rmdir = xfs_vn_unlink,
  909. .mknod = xfs_vn_mknod,
  910. .rename = xfs_vn_rename,
  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. .update_time = xfs_vn_update_time,
  919. };
  920. static const struct inode_operations xfs_dir_ci_inode_operations = {
  921. .create = xfs_vn_create,
  922. .lookup = xfs_vn_ci_lookup,
  923. .link = xfs_vn_link,
  924. .unlink = xfs_vn_unlink,
  925. .symlink = xfs_vn_symlink,
  926. .mkdir = xfs_vn_mkdir,
  927. /*
  928. * Yes, XFS uses the same method for rmdir and unlink.
  929. *
  930. * There are some subtile differences deeper in the code,
  931. * but we use S_ISDIR to check for those.
  932. */
  933. .rmdir = xfs_vn_unlink,
  934. .mknod = xfs_vn_mknod,
  935. .rename = xfs_vn_rename,
  936. .get_acl = xfs_get_acl,
  937. .getattr = xfs_vn_getattr,
  938. .setattr = xfs_vn_setattr,
  939. .setxattr = generic_setxattr,
  940. .getxattr = generic_getxattr,
  941. .removexattr = generic_removexattr,
  942. .listxattr = xfs_vn_listxattr,
  943. .update_time = xfs_vn_update_time,
  944. };
  945. static const struct inode_operations xfs_symlink_inode_operations = {
  946. .readlink = generic_readlink,
  947. .follow_link = xfs_vn_follow_link,
  948. .put_link = xfs_vn_put_link,
  949. .get_acl = xfs_get_acl,
  950. .getattr = xfs_vn_getattr,
  951. .setattr = xfs_vn_setattr,
  952. .setxattr = generic_setxattr,
  953. .getxattr = generic_getxattr,
  954. .removexattr = generic_removexattr,
  955. .listxattr = xfs_vn_listxattr,
  956. .update_time = xfs_vn_update_time,
  957. };
  958. STATIC void
  959. xfs_diflags_to_iflags(
  960. struct inode *inode,
  961. struct xfs_inode *ip)
  962. {
  963. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  964. inode->i_flags |= S_IMMUTABLE;
  965. else
  966. inode->i_flags &= ~S_IMMUTABLE;
  967. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  968. inode->i_flags |= S_APPEND;
  969. else
  970. inode->i_flags &= ~S_APPEND;
  971. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  972. inode->i_flags |= S_SYNC;
  973. else
  974. inode->i_flags &= ~S_SYNC;
  975. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  976. inode->i_flags |= S_NOATIME;
  977. else
  978. inode->i_flags &= ~S_NOATIME;
  979. }
  980. /*
  981. * Initialize the Linux inode, set up the operation vectors and
  982. * unlock the inode.
  983. *
  984. * When reading existing inodes from disk this is called directly
  985. * from xfs_iget, when creating a new inode it is called from
  986. * xfs_ialloc after setting up the inode.
  987. *
  988. * We are always called with an uninitialised linux inode here.
  989. * We need to initialise the necessary fields and take a reference
  990. * on it.
  991. */
  992. void
  993. xfs_setup_inode(
  994. struct xfs_inode *ip)
  995. {
  996. struct inode *inode = &ip->i_vnode;
  997. inode->i_ino = ip->i_ino;
  998. inode->i_state = I_NEW;
  999. inode_sb_list_add(inode);
  1000. /* make the inode look hashed for the writeback code */
  1001. hlist_add_fake(&inode->i_hash);
  1002. inode->i_mode = ip->i_d.di_mode;
  1003. set_nlink(inode, ip->i_d.di_nlink);
  1004. inode->i_uid = ip->i_d.di_uid;
  1005. inode->i_gid = ip->i_d.di_gid;
  1006. switch (inode->i_mode & S_IFMT) {
  1007. case S_IFBLK:
  1008. case S_IFCHR:
  1009. inode->i_rdev =
  1010. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  1011. sysv_minor(ip->i_df.if_u2.if_rdev));
  1012. break;
  1013. default:
  1014. inode->i_rdev = 0;
  1015. break;
  1016. }
  1017. inode->i_generation = ip->i_d.di_gen;
  1018. i_size_write(inode, ip->i_d.di_size);
  1019. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  1020. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  1021. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  1022. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  1023. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  1024. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  1025. xfs_diflags_to_iflags(inode, ip);
  1026. switch (inode->i_mode & S_IFMT) {
  1027. case S_IFREG:
  1028. inode->i_op = &xfs_inode_operations;
  1029. inode->i_fop = &xfs_file_operations;
  1030. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1031. break;
  1032. case S_IFDIR:
  1033. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  1034. inode->i_op = &xfs_dir_ci_inode_operations;
  1035. else
  1036. inode->i_op = &xfs_dir_inode_operations;
  1037. inode->i_fop = &xfs_dir_file_operations;
  1038. break;
  1039. case S_IFLNK:
  1040. inode->i_op = &xfs_symlink_inode_operations;
  1041. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  1042. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1043. break;
  1044. default:
  1045. inode->i_op = &xfs_inode_operations;
  1046. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1047. break;
  1048. }
  1049. /*
  1050. * If there is no attribute fork no ACL can exist on this inode,
  1051. * and it can't have any file capabilities attached to it either.
  1052. */
  1053. if (!XFS_IFORK_Q(ip)) {
  1054. inode_has_no_xattr(inode);
  1055. cache_no_acl(inode);
  1056. }
  1057. xfs_iflags_clear(ip, XFS_INEW);
  1058. barrier();
  1059. unlock_new_inode(inode);
  1060. }