xfs_iops.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_bmap.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_ialloc.h"
  41. #include "xfs_rtalloc.h"
  42. #include "xfs_error.h"
  43. #include "xfs_itable.h"
  44. #include "xfs_rw.h"
  45. #include "xfs_acl.h"
  46. #include "xfs_attr.h"
  47. #include "xfs_buf_item.h"
  48. #include "xfs_utils.h"
  49. #include "xfs_vnodeops.h"
  50. #include <linux/capability.h>
  51. #include <linux/xattr.h>
  52. #include <linux/namei.h>
  53. #include <linux/security.h>
  54. /*
  55. * Bring the atime in the XFS inode uptodate.
  56. * Used before logging the inode to disk or when the Linux inode goes away.
  57. */
  58. void
  59. xfs_synchronize_atime(
  60. xfs_inode_t *ip)
  61. {
  62. bhv_vnode_t *vp;
  63. vp = XFS_ITOV_NULL(ip);
  64. if (vp) {
  65. ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec;
  66. ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec;
  67. }
  68. }
  69. /*
  70. * Change the requested timestamp in the given inode.
  71. * We don't lock across timestamp updates, and we don't log them but
  72. * we do record the fact that there is dirty information in core.
  73. *
  74. * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
  75. * with XFS_ICHGTIME_ACC to be sure that access time
  76. * update will take. Calling first with XFS_ICHGTIME_ACC
  77. * and then XFS_ICHGTIME_MOD may fail to modify the access
  78. * timestamp if the filesystem is mounted noacctm.
  79. */
  80. void
  81. xfs_ichgtime(
  82. xfs_inode_t *ip,
  83. int flags)
  84. {
  85. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  86. timespec_t tv;
  87. nanotime(&tv);
  88. if (flags & XFS_ICHGTIME_MOD) {
  89. inode->i_mtime = tv;
  90. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  91. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  92. }
  93. if (flags & XFS_ICHGTIME_ACC) {
  94. inode->i_atime = tv;
  95. ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
  96. ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
  97. }
  98. if (flags & XFS_ICHGTIME_CHG) {
  99. inode->i_ctime = tv;
  100. ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
  101. ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
  102. }
  103. /*
  104. * We update the i_update_core field _after_ changing
  105. * the timestamps in order to coordinate properly with
  106. * xfs_iflush() so that we don't lose timestamp updates.
  107. * This keeps us from having to hold the inode lock
  108. * while doing this. We use the SYNCHRONIZE macro to
  109. * ensure that the compiler does not reorder the update
  110. * of i_update_core above the timestamp updates above.
  111. */
  112. SYNCHRONIZE();
  113. ip->i_update_core = 1;
  114. if (!(inode->i_state & I_NEW))
  115. mark_inode_dirty_sync(inode);
  116. }
  117. /*
  118. * Variant on the above which avoids querying the system clock
  119. * in situations where we know the Linux inode timestamps have
  120. * just been updated (and so we can update our inode cheaply).
  121. */
  122. void
  123. xfs_ichgtime_fast(
  124. xfs_inode_t *ip,
  125. struct inode *inode,
  126. int flags)
  127. {
  128. timespec_t *tvp;
  129. /*
  130. * Atime updates for read() & friends are handled lazily now, and
  131. * explicit updates must go through xfs_ichgtime()
  132. */
  133. ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
  134. /*
  135. * We're not supposed to change timestamps in readonly-mounted
  136. * filesystems. Throw it away if anyone asks us.
  137. */
  138. if (unlikely(IS_RDONLY(inode)))
  139. return;
  140. if (flags & XFS_ICHGTIME_MOD) {
  141. tvp = &inode->i_mtime;
  142. ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
  143. ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
  144. }
  145. if (flags & XFS_ICHGTIME_CHG) {
  146. tvp = &inode->i_ctime;
  147. ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
  148. ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
  149. }
  150. /*
  151. * We update the i_update_core field _after_ changing
  152. * the timestamps in order to coordinate properly with
  153. * xfs_iflush() so that we don't lose timestamp updates.
  154. * This keeps us from having to hold the inode lock
  155. * while doing this. We use the SYNCHRONIZE macro to
  156. * ensure that the compiler does not reorder the update
  157. * of i_update_core above the timestamp updates above.
  158. */
  159. SYNCHRONIZE();
  160. ip->i_update_core = 1;
  161. if (!(inode->i_state & I_NEW))
  162. mark_inode_dirty_sync(inode);
  163. }
  164. /*
  165. * Pull the link count and size up from the xfs inode to the linux inode
  166. */
  167. STATIC void
  168. xfs_validate_fields(
  169. struct inode *inode)
  170. {
  171. struct xfs_inode *ip = XFS_I(inode);
  172. loff_t size;
  173. inode->i_nlink = ip->i_d.di_nlink;
  174. inode->i_blocks =
  175. XFS_FSB_TO_BB(ip->i_mount, ip->i_d.di_nblocks +
  176. ip->i_delayed_blks);
  177. /* we're under i_sem so i_size can't change under us */
  178. size = XFS_ISIZE(ip);
  179. if (i_size_read(inode) != size)
  180. i_size_write(inode, size);
  181. }
  182. /*
  183. * Hook in SELinux. This is not quite correct yet, what we really need
  184. * here (as we do for default ACLs) is a mechanism by which creation of
  185. * these attrs can be journalled at inode creation time (along with the
  186. * inode, of course, such that log replay can't cause these to be lost).
  187. */
  188. STATIC int
  189. xfs_init_security(
  190. bhv_vnode_t *vp,
  191. struct inode *dir)
  192. {
  193. struct inode *ip = vn_to_inode(vp);
  194. size_t length;
  195. void *value;
  196. char *name;
  197. int error;
  198. error = security_inode_init_security(ip, dir, &name, &value, &length);
  199. if (error) {
  200. if (error == -EOPNOTSUPP)
  201. return 0;
  202. return -error;
  203. }
  204. error = xfs_attr_set(XFS_I(ip), name, value,
  205. length, ATTR_SECURE);
  206. if (!error)
  207. xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
  208. kfree(name);
  209. kfree(value);
  210. return error;
  211. }
  212. /*
  213. * Determine whether a process has a valid fs_struct (kernel daemons
  214. * like knfsd don't have an fs_struct).
  215. *
  216. * XXX(hch): nfsd is broken, better fix it instead.
  217. */
  218. STATIC_INLINE int
  219. xfs_has_fs_struct(struct task_struct *task)
  220. {
  221. return (task->fs != init_task.fs);
  222. }
  223. STATIC void
  224. xfs_cleanup_inode(
  225. struct inode *dir,
  226. bhv_vnode_t *vp,
  227. struct dentry *dentry,
  228. int mode)
  229. {
  230. struct dentry teardown = {};
  231. /* Oh, the horror.
  232. * If we can't add the ACL or we fail in
  233. * xfs_init_security we must back out.
  234. * ENOSPC can hit here, among other things.
  235. */
  236. teardown.d_inode = vn_to_inode(vp);
  237. teardown.d_name = dentry->d_name;
  238. if (S_ISDIR(mode))
  239. xfs_rmdir(XFS_I(dir), &teardown);
  240. else
  241. xfs_remove(XFS_I(dir), &teardown);
  242. VN_RELE(vp);
  243. }
  244. STATIC int
  245. xfs_vn_mknod(
  246. struct inode *dir,
  247. struct dentry *dentry,
  248. int mode,
  249. dev_t rdev)
  250. {
  251. struct inode *ip;
  252. bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
  253. xfs_acl_t *default_acl = NULL;
  254. attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
  255. int error;
  256. /*
  257. * Irix uses Missed'em'V split, but doesn't want to see
  258. * the upper 5 bits of (14bit) major.
  259. */
  260. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  261. return -EINVAL;
  262. if (unlikely(test_default_acl && test_default_acl(dvp))) {
  263. if (!_ACL_ALLOC(default_acl)) {
  264. return -ENOMEM;
  265. }
  266. if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
  267. _ACL_FREE(default_acl);
  268. default_acl = NULL;
  269. }
  270. }
  271. if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
  272. mode &= ~current->fs->umask;
  273. switch (mode & S_IFMT) {
  274. case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
  275. rdev = sysv_encode_dev(rdev);
  276. case S_IFREG:
  277. error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL);
  278. break;
  279. case S_IFDIR:
  280. error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL);
  281. break;
  282. default:
  283. error = EINVAL;
  284. break;
  285. }
  286. if (unlikely(!error)) {
  287. error = xfs_init_security(vp, dir);
  288. if (error)
  289. xfs_cleanup_inode(dir, vp, dentry, mode);
  290. }
  291. if (unlikely(default_acl)) {
  292. if (!error) {
  293. error = _ACL_INHERIT(vp, mode, default_acl);
  294. if (!error)
  295. xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
  296. else
  297. xfs_cleanup_inode(dir, vp, dentry, mode);
  298. }
  299. _ACL_FREE(default_acl);
  300. }
  301. if (likely(!error)) {
  302. ASSERT(vp);
  303. ip = vn_to_inode(vp);
  304. if (S_ISDIR(mode))
  305. xfs_validate_fields(ip);
  306. d_instantiate(dentry, ip);
  307. xfs_validate_fields(dir);
  308. }
  309. return -error;
  310. }
  311. STATIC int
  312. xfs_vn_create(
  313. struct inode *dir,
  314. struct dentry *dentry,
  315. int mode,
  316. struct nameidata *nd)
  317. {
  318. return xfs_vn_mknod(dir, dentry, mode, 0);
  319. }
  320. STATIC int
  321. xfs_vn_mkdir(
  322. struct inode *dir,
  323. struct dentry *dentry,
  324. int mode)
  325. {
  326. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  327. }
  328. STATIC struct dentry *
  329. xfs_vn_lookup(
  330. struct inode *dir,
  331. struct dentry *dentry,
  332. struct nameidata *nd)
  333. {
  334. bhv_vnode_t *cvp;
  335. int error;
  336. if (dentry->d_name.len >= MAXNAMELEN)
  337. return ERR_PTR(-ENAMETOOLONG);
  338. error = xfs_lookup(XFS_I(dir), dentry, &cvp);
  339. if (unlikely(error)) {
  340. if (unlikely(error != ENOENT))
  341. return ERR_PTR(-error);
  342. d_add(dentry, NULL);
  343. return NULL;
  344. }
  345. return d_splice_alias(vn_to_inode(cvp), dentry);
  346. }
  347. STATIC int
  348. xfs_vn_link(
  349. struct dentry *old_dentry,
  350. struct inode *dir,
  351. struct dentry *dentry)
  352. {
  353. struct inode *ip; /* inode of guy being linked to */
  354. bhv_vnode_t *vp; /* vp of name being linked */
  355. int error;
  356. ip = old_dentry->d_inode; /* inode being linked to */
  357. vp = vn_from_inode(ip);
  358. VN_HOLD(vp);
  359. error = xfs_link(XFS_I(dir), vp, dentry);
  360. if (unlikely(error)) {
  361. VN_RELE(vp);
  362. } else {
  363. xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
  364. xfs_validate_fields(ip);
  365. d_instantiate(dentry, ip);
  366. }
  367. return -error;
  368. }
  369. STATIC int
  370. xfs_vn_unlink(
  371. struct inode *dir,
  372. struct dentry *dentry)
  373. {
  374. struct inode *inode;
  375. int error;
  376. inode = dentry->d_inode;
  377. error = xfs_remove(XFS_I(dir), dentry);
  378. if (likely(!error)) {
  379. xfs_validate_fields(dir); /* size needs update */
  380. xfs_validate_fields(inode);
  381. }
  382. return -error;
  383. }
  384. STATIC int
  385. xfs_vn_symlink(
  386. struct inode *dir,
  387. struct dentry *dentry,
  388. const char *symname)
  389. {
  390. struct inode *ip;
  391. bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
  392. int error;
  393. mode_t mode;
  394. cvp = NULL;
  395. mode = S_IFLNK |
  396. (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
  397. error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
  398. &cvp, NULL);
  399. if (likely(!error && cvp)) {
  400. error = xfs_init_security(cvp, dir);
  401. if (likely(!error)) {
  402. ip = vn_to_inode(cvp);
  403. d_instantiate(dentry, ip);
  404. xfs_validate_fields(dir);
  405. xfs_validate_fields(ip);
  406. } else {
  407. xfs_cleanup_inode(dir, cvp, dentry, 0);
  408. }
  409. }
  410. return -error;
  411. }
  412. STATIC int
  413. xfs_vn_rmdir(
  414. struct inode *dir,
  415. struct dentry *dentry)
  416. {
  417. struct inode *inode = dentry->d_inode;
  418. int error;
  419. error = xfs_rmdir(XFS_I(dir), dentry);
  420. if (likely(!error)) {
  421. xfs_validate_fields(inode);
  422. xfs_validate_fields(dir);
  423. }
  424. return -error;
  425. }
  426. STATIC int
  427. xfs_vn_rename(
  428. struct inode *odir,
  429. struct dentry *odentry,
  430. struct inode *ndir,
  431. struct dentry *ndentry)
  432. {
  433. struct inode *new_inode = ndentry->d_inode;
  434. bhv_vnode_t *tvp; /* target directory */
  435. int error;
  436. tvp = vn_from_inode(ndir);
  437. error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
  438. if (likely(!error)) {
  439. if (new_inode)
  440. xfs_validate_fields(new_inode);
  441. xfs_validate_fields(odir);
  442. if (ndir != odir)
  443. xfs_validate_fields(ndir);
  444. }
  445. return -error;
  446. }
  447. /*
  448. * careful here - this function can get called recursively, so
  449. * we need to be very careful about how much stack we use.
  450. * uio is kmalloced for this reason...
  451. */
  452. STATIC void *
  453. xfs_vn_follow_link(
  454. struct dentry *dentry,
  455. struct nameidata *nd)
  456. {
  457. char *link;
  458. int error = -ENOMEM;
  459. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  460. if (!link)
  461. goto out_err;
  462. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  463. if (unlikely(error))
  464. goto out_kfree;
  465. nd_set_link(nd, link);
  466. return NULL;
  467. out_kfree:
  468. kfree(link);
  469. out_err:
  470. nd_set_link(nd, ERR_PTR(error));
  471. return NULL;
  472. }
  473. STATIC void
  474. xfs_vn_put_link(
  475. struct dentry *dentry,
  476. struct nameidata *nd,
  477. void *p)
  478. {
  479. char *s = nd_get_link(nd);
  480. if (!IS_ERR(s))
  481. kfree(s);
  482. }
  483. #ifdef CONFIG_XFS_POSIX_ACL
  484. STATIC int
  485. xfs_vn_permission(
  486. struct inode *inode,
  487. int mode,
  488. struct nameidata *nd)
  489. {
  490. return -xfs_access(XFS_I(inode), mode << 6, NULL);
  491. }
  492. #else
  493. #define xfs_vn_permission NULL
  494. #endif
  495. STATIC int
  496. xfs_vn_getattr(
  497. struct vfsmount *mnt,
  498. struct dentry *dentry,
  499. struct kstat *stat)
  500. {
  501. struct inode *inode = dentry->d_inode;
  502. struct xfs_inode *ip = XFS_I(inode);
  503. struct xfs_mount *mp = ip->i_mount;
  504. xfs_itrace_entry(ip);
  505. if (XFS_FORCED_SHUTDOWN(mp))
  506. return XFS_ERROR(EIO);
  507. stat->size = XFS_ISIZE(ip);
  508. stat->dev = inode->i_sb->s_dev;
  509. stat->mode = ip->i_d.di_mode;
  510. stat->nlink = ip->i_d.di_nlink;
  511. stat->uid = ip->i_d.di_uid;
  512. stat->gid = ip->i_d.di_gid;
  513. stat->ino = ip->i_ino;
  514. #if XFS_BIG_INUMS
  515. stat->ino += mp->m_inoadd;
  516. #endif
  517. stat->atime = inode->i_atime;
  518. stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  519. stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  520. stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  521. stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  522. stat->blocks =
  523. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  524. switch (inode->i_mode & S_IFMT) {
  525. case S_IFBLK:
  526. case S_IFCHR:
  527. stat->blksize = BLKDEV_IOSIZE;
  528. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  529. sysv_minor(ip->i_df.if_u2.if_rdev));
  530. break;
  531. default:
  532. if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
  533. /*
  534. * If the file blocks are being allocated from a
  535. * realtime volume, then return the inode's realtime
  536. * extent size or the realtime volume's extent size.
  537. */
  538. stat->blksize =
  539. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  540. } else
  541. stat->blksize = xfs_preferred_iosize(mp);
  542. stat->rdev = 0;
  543. break;
  544. }
  545. return 0;
  546. }
  547. STATIC int
  548. xfs_vn_setattr(
  549. struct dentry *dentry,
  550. struct iattr *attr)
  551. {
  552. struct inode *inode = dentry->d_inode;
  553. unsigned int ia_valid = attr->ia_valid;
  554. bhv_vattr_t vattr = { 0 };
  555. int flags = 0;
  556. int error;
  557. if (ia_valid & ATTR_UID) {
  558. vattr.va_mask |= XFS_AT_UID;
  559. vattr.va_uid = attr->ia_uid;
  560. }
  561. if (ia_valid & ATTR_GID) {
  562. vattr.va_mask |= XFS_AT_GID;
  563. vattr.va_gid = attr->ia_gid;
  564. }
  565. if (ia_valid & ATTR_SIZE) {
  566. vattr.va_mask |= XFS_AT_SIZE;
  567. vattr.va_size = attr->ia_size;
  568. }
  569. if (ia_valid & ATTR_ATIME) {
  570. vattr.va_mask |= XFS_AT_ATIME;
  571. vattr.va_atime = attr->ia_atime;
  572. inode->i_atime = attr->ia_atime;
  573. }
  574. if (ia_valid & ATTR_MTIME) {
  575. vattr.va_mask |= XFS_AT_MTIME;
  576. vattr.va_mtime = attr->ia_mtime;
  577. }
  578. if (ia_valid & ATTR_CTIME) {
  579. vattr.va_mask |= XFS_AT_CTIME;
  580. vattr.va_ctime = attr->ia_ctime;
  581. }
  582. if (ia_valid & ATTR_MODE) {
  583. vattr.va_mask |= XFS_AT_MODE;
  584. vattr.va_mode = attr->ia_mode;
  585. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  586. inode->i_mode &= ~S_ISGID;
  587. }
  588. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
  589. flags |= ATTR_UTIME;
  590. #ifdef ATTR_NO_BLOCK
  591. if ((ia_valid & ATTR_NO_BLOCK))
  592. flags |= ATTR_NONBLOCK;
  593. #endif
  594. error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
  595. if (likely(!error))
  596. vn_revalidate(vn_from_inode(inode));
  597. return -error;
  598. }
  599. STATIC void
  600. xfs_vn_truncate(
  601. struct inode *inode)
  602. {
  603. block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
  604. }
  605. STATIC int
  606. xfs_vn_setxattr(
  607. struct dentry *dentry,
  608. const char *name,
  609. const void *data,
  610. size_t size,
  611. int flags)
  612. {
  613. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  614. char *attr = (char *)name;
  615. attrnames_t *namesp;
  616. int xflags = 0;
  617. int error;
  618. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  619. if (!namesp)
  620. return -EOPNOTSUPP;
  621. attr += namesp->attr_namelen;
  622. error = namesp->attr_capable(vp, NULL);
  623. if (error)
  624. return error;
  625. /* Convert Linux syscall to XFS internal ATTR flags */
  626. if (flags & XATTR_CREATE)
  627. xflags |= ATTR_CREATE;
  628. if (flags & XATTR_REPLACE)
  629. xflags |= ATTR_REPLACE;
  630. xflags |= namesp->attr_flag;
  631. return namesp->attr_set(vp, attr, (void *)data, size, xflags);
  632. }
  633. STATIC ssize_t
  634. xfs_vn_getxattr(
  635. struct dentry *dentry,
  636. const char *name,
  637. void *data,
  638. size_t size)
  639. {
  640. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  641. char *attr = (char *)name;
  642. attrnames_t *namesp;
  643. int xflags = 0;
  644. ssize_t error;
  645. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  646. if (!namesp)
  647. return -EOPNOTSUPP;
  648. attr += namesp->attr_namelen;
  649. error = namesp->attr_capable(vp, NULL);
  650. if (error)
  651. return error;
  652. /* Convert Linux syscall to XFS internal ATTR flags */
  653. if (!size) {
  654. xflags |= ATTR_KERNOVAL;
  655. data = NULL;
  656. }
  657. xflags |= namesp->attr_flag;
  658. return namesp->attr_get(vp, attr, (void *)data, size, xflags);
  659. }
  660. STATIC ssize_t
  661. xfs_vn_listxattr(
  662. struct dentry *dentry,
  663. char *data,
  664. size_t size)
  665. {
  666. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  667. int error, xflags = ATTR_KERNAMELS;
  668. ssize_t result;
  669. if (!size)
  670. xflags |= ATTR_KERNOVAL;
  671. xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
  672. error = attr_generic_list(vp, data, size, xflags, &result);
  673. if (error < 0)
  674. return error;
  675. return result;
  676. }
  677. STATIC int
  678. xfs_vn_removexattr(
  679. struct dentry *dentry,
  680. const char *name)
  681. {
  682. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  683. char *attr = (char *)name;
  684. attrnames_t *namesp;
  685. int xflags = 0;
  686. int error;
  687. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  688. if (!namesp)
  689. return -EOPNOTSUPP;
  690. attr += namesp->attr_namelen;
  691. error = namesp->attr_capable(vp, NULL);
  692. if (error)
  693. return error;
  694. xflags |= namesp->attr_flag;
  695. return namesp->attr_remove(vp, attr, xflags);
  696. }
  697. const struct inode_operations xfs_inode_operations = {
  698. .permission = xfs_vn_permission,
  699. .truncate = xfs_vn_truncate,
  700. .getattr = xfs_vn_getattr,
  701. .setattr = xfs_vn_setattr,
  702. .setxattr = xfs_vn_setxattr,
  703. .getxattr = xfs_vn_getxattr,
  704. .listxattr = xfs_vn_listxattr,
  705. .removexattr = xfs_vn_removexattr,
  706. };
  707. const struct inode_operations xfs_dir_inode_operations = {
  708. .create = xfs_vn_create,
  709. .lookup = xfs_vn_lookup,
  710. .link = xfs_vn_link,
  711. .unlink = xfs_vn_unlink,
  712. .symlink = xfs_vn_symlink,
  713. .mkdir = xfs_vn_mkdir,
  714. .rmdir = xfs_vn_rmdir,
  715. .mknod = xfs_vn_mknod,
  716. .rename = xfs_vn_rename,
  717. .permission = xfs_vn_permission,
  718. .getattr = xfs_vn_getattr,
  719. .setattr = xfs_vn_setattr,
  720. .setxattr = xfs_vn_setxattr,
  721. .getxattr = xfs_vn_getxattr,
  722. .listxattr = xfs_vn_listxattr,
  723. .removexattr = xfs_vn_removexattr,
  724. };
  725. const struct inode_operations xfs_symlink_inode_operations = {
  726. .readlink = generic_readlink,
  727. .follow_link = xfs_vn_follow_link,
  728. .put_link = xfs_vn_put_link,
  729. .permission = xfs_vn_permission,
  730. .getattr = xfs_vn_getattr,
  731. .setattr = xfs_vn_setattr,
  732. .setxattr = xfs_vn_setxattr,
  733. .getxattr = xfs_vn_getxattr,
  734. .listxattr = xfs_vn_listxattr,
  735. .removexattr = xfs_vn_removexattr,
  736. };