xfs_iops.c 19 KB

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