xfs_iops.c 30 KB

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