xfs_iops.c 29 KB

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