xfs_iops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 <linux/capability.h>
  50. #include <linux/xattr.h>
  51. #include <linux/namei.h>
  52. #include <linux/security.h>
  53. /*
  54. * Get a XFS inode from a given vnode.
  55. */
  56. xfs_inode_t *
  57. xfs_vtoi(
  58. bhv_vnode_t *vp)
  59. {
  60. bhv_desc_t *bdp;
  61. bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
  62. VNODE_POSITION_XFS, VNODE_POSITION_XFS);
  63. if (unlikely(bdp == NULL))
  64. return NULL;
  65. return XFS_BHVTOI(bdp);
  66. }
  67. /*
  68. * Bring the atime in the XFS inode uptodate.
  69. * Used before logging the inode to disk or when the Linux inode goes away.
  70. */
  71. void
  72. xfs_synchronize_atime(
  73. xfs_inode_t *ip)
  74. {
  75. bhv_vnode_t *vp;
  76. vp = XFS_ITOV_NULL(ip);
  77. if (vp) {
  78. struct inode *inode = &vp->v_inode;
  79. ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
  80. ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
  81. }
  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_LOCK))
  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_LOCK))
  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 *ip,
  184. bhv_vattr_t *vattr)
  185. {
  186. vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
  187. if (!bhv_vop_getattr(vn_from_inode(ip), vattr, ATTR_LAZY, NULL)) {
  188. ip->i_nlink = vattr->va_nlink;
  189. ip->i_blocks = vattr->va_nblocks;
  190. /* we're under i_sem so i_size can't change under us */
  191. if (i_size_read(ip) != vattr->va_size)
  192. i_size_write(ip, vattr->va_size);
  193. }
  194. }
  195. /*
  196. * Hook in SELinux. This is not quite correct yet, what we really need
  197. * here (as we do for default ACLs) is a mechanism by which creation of
  198. * these attrs can be journalled at inode creation time (along with the
  199. * inode, of course, such that log replay can't cause these to be lost).
  200. */
  201. STATIC int
  202. xfs_init_security(
  203. bhv_vnode_t *vp,
  204. struct inode *dir)
  205. {
  206. struct inode *ip = vn_to_inode(vp);
  207. size_t length;
  208. void *value;
  209. char *name;
  210. int error;
  211. error = security_inode_init_security(ip, dir, &name, &value, &length);
  212. if (error) {
  213. if (error == -EOPNOTSUPP)
  214. return 0;
  215. return -error;
  216. }
  217. error = bhv_vop_attr_set(vp, name, value, length, ATTR_SECURE, NULL);
  218. if (!error)
  219. VMODIFY(vp);
  220. kfree(name);
  221. kfree(value);
  222. return error;
  223. }
  224. /*
  225. * Determine whether a process has a valid fs_struct (kernel daemons
  226. * like knfsd don't have an fs_struct).
  227. *
  228. * XXX(hch): nfsd is broken, better fix it instead.
  229. */
  230. STATIC_INLINE int
  231. xfs_has_fs_struct(struct task_struct *task)
  232. {
  233. return (task->fs != init_task.fs);
  234. }
  235. STATIC void
  236. xfs_cleanup_inode(
  237. bhv_vnode_t *dvp,
  238. bhv_vnode_t *vp,
  239. struct dentry *dentry,
  240. int mode)
  241. {
  242. struct dentry teardown = {};
  243. /* Oh, the horror.
  244. * If we can't add the ACL or we fail in
  245. * xfs_init_security we must back out.
  246. * ENOSPC can hit here, among other things.
  247. */
  248. teardown.d_inode = vn_to_inode(vp);
  249. teardown.d_name = dentry->d_name;
  250. if (S_ISDIR(mode))
  251. bhv_vop_rmdir(dvp, &teardown, NULL);
  252. else
  253. bhv_vop_remove(dvp, &teardown, NULL);
  254. VN_RELE(vp);
  255. }
  256. STATIC int
  257. xfs_vn_mknod(
  258. struct inode *dir,
  259. struct dentry *dentry,
  260. int mode,
  261. dev_t rdev)
  262. {
  263. struct inode *ip;
  264. bhv_vattr_t vattr = { 0 };
  265. bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
  266. xfs_acl_t *default_acl = NULL;
  267. attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
  268. int error;
  269. /*
  270. * Irix uses Missed'em'V split, but doesn't want to see
  271. * the upper 5 bits of (14bit) major.
  272. */
  273. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  274. return -EINVAL;
  275. if (unlikely(test_default_acl && test_default_acl(dvp))) {
  276. if (!_ACL_ALLOC(default_acl)) {
  277. return -ENOMEM;
  278. }
  279. if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
  280. _ACL_FREE(default_acl);
  281. default_acl = NULL;
  282. }
  283. }
  284. if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
  285. mode &= ~current->fs->umask;
  286. vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  287. vattr.va_mode = mode;
  288. switch (mode & S_IFMT) {
  289. case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
  290. vattr.va_rdev = sysv_encode_dev(rdev);
  291. vattr.va_mask |= XFS_AT_RDEV;
  292. /*FALLTHROUGH*/
  293. case S_IFREG:
  294. error = bhv_vop_create(dvp, dentry, &vattr, &vp, NULL);
  295. break;
  296. case S_IFDIR:
  297. error = bhv_vop_mkdir(dvp, dentry, &vattr, &vp, NULL);
  298. break;
  299. default:
  300. error = EINVAL;
  301. break;
  302. }
  303. if (unlikely(!error)) {
  304. error = xfs_init_security(vp, dir);
  305. if (error)
  306. xfs_cleanup_inode(dvp, vp, dentry, mode);
  307. }
  308. if (unlikely(default_acl)) {
  309. if (!error) {
  310. error = _ACL_INHERIT(vp, &vattr, default_acl);
  311. if (!error)
  312. VMODIFY(vp);
  313. else
  314. xfs_cleanup_inode(dvp, vp, dentry, mode);
  315. }
  316. _ACL_FREE(default_acl);
  317. }
  318. if (likely(!error)) {
  319. ASSERT(vp);
  320. ip = vn_to_inode(vp);
  321. if (S_ISCHR(mode) || S_ISBLK(mode))
  322. ip->i_rdev = rdev;
  323. else if (S_ISDIR(mode))
  324. xfs_validate_fields(ip, &vattr);
  325. d_instantiate(dentry, ip);
  326. xfs_validate_fields(dir, &vattr);
  327. }
  328. return -error;
  329. }
  330. STATIC int
  331. xfs_vn_create(
  332. struct inode *dir,
  333. struct dentry *dentry,
  334. int mode,
  335. struct nameidata *nd)
  336. {
  337. return xfs_vn_mknod(dir, dentry, mode, 0);
  338. }
  339. STATIC int
  340. xfs_vn_mkdir(
  341. struct inode *dir,
  342. struct dentry *dentry,
  343. int mode)
  344. {
  345. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  346. }
  347. STATIC struct dentry *
  348. xfs_vn_lookup(
  349. struct inode *dir,
  350. struct dentry *dentry,
  351. struct nameidata *nd)
  352. {
  353. bhv_vnode_t *vp = vn_from_inode(dir), *cvp;
  354. int error;
  355. if (dentry->d_name.len >= MAXNAMELEN)
  356. return ERR_PTR(-ENAMETOOLONG);
  357. error = bhv_vop_lookup(vp, dentry, &cvp, 0, NULL, NULL);
  358. if (unlikely(error)) {
  359. if (unlikely(error != ENOENT))
  360. return ERR_PTR(-error);
  361. d_add(dentry, NULL);
  362. return NULL;
  363. }
  364. return d_splice_alias(vn_to_inode(cvp), dentry);
  365. }
  366. STATIC int
  367. xfs_vn_link(
  368. struct dentry *old_dentry,
  369. struct inode *dir,
  370. struct dentry *dentry)
  371. {
  372. struct inode *ip; /* inode of guy being linked to */
  373. bhv_vnode_t *tdvp; /* target directory for new name/link */
  374. bhv_vnode_t *vp; /* vp of name being linked */
  375. bhv_vattr_t vattr;
  376. int error;
  377. ip = old_dentry->d_inode; /* inode being linked to */
  378. tdvp = vn_from_inode(dir);
  379. vp = vn_from_inode(ip);
  380. VN_HOLD(vp);
  381. error = bhv_vop_link(tdvp, vp, dentry, NULL);
  382. if (unlikely(error)) {
  383. VN_RELE(vp);
  384. } else {
  385. VMODIFY(tdvp);
  386. xfs_validate_fields(ip, &vattr);
  387. d_instantiate(dentry, ip);
  388. }
  389. return -error;
  390. }
  391. STATIC int
  392. xfs_vn_unlink(
  393. struct inode *dir,
  394. struct dentry *dentry)
  395. {
  396. struct inode *inode;
  397. bhv_vnode_t *dvp; /* directory containing name to remove */
  398. bhv_vattr_t vattr;
  399. int error;
  400. inode = dentry->d_inode;
  401. dvp = vn_from_inode(dir);
  402. error = bhv_vop_remove(dvp, dentry, NULL);
  403. if (likely(!error)) {
  404. xfs_validate_fields(dir, &vattr); /* size needs update */
  405. xfs_validate_fields(inode, &vattr);
  406. }
  407. return -error;
  408. }
  409. STATIC int
  410. xfs_vn_symlink(
  411. struct inode *dir,
  412. struct dentry *dentry,
  413. const char *symname)
  414. {
  415. struct inode *ip;
  416. bhv_vattr_t va = { 0 };
  417. bhv_vnode_t *dvp; /* directory containing name of symlink */
  418. bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
  419. int error;
  420. dvp = vn_from_inode(dir);
  421. cvp = NULL;
  422. va.va_mode = S_IFLNK |
  423. (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
  424. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  425. error = bhv_vop_symlink(dvp, dentry, &va, (char *)symname, &cvp, NULL);
  426. if (likely(!error && cvp)) {
  427. error = xfs_init_security(cvp, dir);
  428. if (likely(!error)) {
  429. ip = vn_to_inode(cvp);
  430. d_instantiate(dentry, ip);
  431. xfs_validate_fields(dir, &va);
  432. xfs_validate_fields(ip, &va);
  433. } else {
  434. xfs_cleanup_inode(dvp, cvp, dentry, 0);
  435. }
  436. }
  437. return -error;
  438. }
  439. STATIC int
  440. xfs_vn_rmdir(
  441. struct inode *dir,
  442. struct dentry *dentry)
  443. {
  444. struct inode *inode = dentry->d_inode;
  445. bhv_vnode_t *dvp = vn_from_inode(dir);
  446. bhv_vattr_t vattr;
  447. int error;
  448. error = bhv_vop_rmdir(dvp, dentry, NULL);
  449. if (likely(!error)) {
  450. xfs_validate_fields(inode, &vattr);
  451. xfs_validate_fields(dir, &vattr);
  452. }
  453. return -error;
  454. }
  455. STATIC int
  456. xfs_vn_rename(
  457. struct inode *odir,
  458. struct dentry *odentry,
  459. struct inode *ndir,
  460. struct dentry *ndentry)
  461. {
  462. struct inode *new_inode = ndentry->d_inode;
  463. bhv_vnode_t *fvp; /* from directory */
  464. bhv_vnode_t *tvp; /* target directory */
  465. bhv_vattr_t vattr;
  466. int error;
  467. fvp = vn_from_inode(odir);
  468. tvp = vn_from_inode(ndir);
  469. error = bhv_vop_rename(fvp, odentry, tvp, ndentry, NULL);
  470. if (likely(!error)) {
  471. if (new_inode)
  472. xfs_validate_fields(new_inode, &vattr);
  473. xfs_validate_fields(odir, &vattr);
  474. if (ndir != odir)
  475. xfs_validate_fields(ndir, &vattr);
  476. }
  477. return -error;
  478. }
  479. /*
  480. * careful here - this function can get called recursively, so
  481. * we need to be very careful about how much stack we use.
  482. * uio is kmalloced for this reason...
  483. */
  484. STATIC void *
  485. xfs_vn_follow_link(
  486. struct dentry *dentry,
  487. struct nameidata *nd)
  488. {
  489. bhv_vnode_t *vp;
  490. uio_t *uio;
  491. iovec_t iov;
  492. int error;
  493. char *link;
  494. ASSERT(dentry);
  495. ASSERT(nd);
  496. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  497. if (!link) {
  498. nd_set_link(nd, ERR_PTR(-ENOMEM));
  499. return NULL;
  500. }
  501. uio = kmalloc(sizeof(uio_t), GFP_KERNEL);
  502. if (!uio) {
  503. kfree(link);
  504. nd_set_link(nd, ERR_PTR(-ENOMEM));
  505. return NULL;
  506. }
  507. vp = vn_from_inode(dentry->d_inode);
  508. iov.iov_base = link;
  509. iov.iov_len = MAXPATHLEN;
  510. uio->uio_iov = &iov;
  511. uio->uio_offset = 0;
  512. uio->uio_segflg = UIO_SYSSPACE;
  513. uio->uio_resid = MAXPATHLEN;
  514. uio->uio_iovcnt = 1;
  515. error = bhv_vop_readlink(vp, uio, 0, NULL);
  516. if (unlikely(error)) {
  517. kfree(link);
  518. link = ERR_PTR(-error);
  519. } else {
  520. link[MAXPATHLEN - uio->uio_resid] = '\0';
  521. }
  522. kfree(uio);
  523. nd_set_link(nd, link);
  524. return NULL;
  525. }
  526. STATIC void
  527. xfs_vn_put_link(
  528. struct dentry *dentry,
  529. struct nameidata *nd,
  530. void *p)
  531. {
  532. char *s = nd_get_link(nd);
  533. if (!IS_ERR(s))
  534. kfree(s);
  535. }
  536. #ifdef CONFIG_XFS_POSIX_ACL
  537. STATIC int
  538. xfs_vn_permission(
  539. struct inode *inode,
  540. int mode,
  541. struct nameidata *nd)
  542. {
  543. return -bhv_vop_access(vn_from_inode(inode), mode << 6, NULL);
  544. }
  545. #else
  546. #define xfs_vn_permission NULL
  547. #endif
  548. STATIC int
  549. xfs_vn_getattr(
  550. struct vfsmount *mnt,
  551. struct dentry *dentry,
  552. struct kstat *stat)
  553. {
  554. struct inode *inode = dentry->d_inode;
  555. bhv_vnode_t *vp = vn_from_inode(inode);
  556. bhv_vattr_t vattr = { .va_mask = XFS_AT_STAT };
  557. int error;
  558. error = bhv_vop_getattr(vp, &vattr, ATTR_LAZY, NULL);
  559. if (likely(!error)) {
  560. stat->size = i_size_read(inode);
  561. stat->dev = inode->i_sb->s_dev;
  562. stat->rdev = (vattr.va_rdev == 0) ? 0 :
  563. MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
  564. sysv_minor(vattr.va_rdev));
  565. stat->mode = vattr.va_mode;
  566. stat->nlink = vattr.va_nlink;
  567. stat->uid = vattr.va_uid;
  568. stat->gid = vattr.va_gid;
  569. stat->ino = vattr.va_nodeid;
  570. stat->atime = vattr.va_atime;
  571. stat->mtime = vattr.va_mtime;
  572. stat->ctime = vattr.va_ctime;
  573. stat->blocks = vattr.va_nblocks;
  574. stat->blksize = vattr.va_blocksize;
  575. }
  576. return -error;
  577. }
  578. STATIC int
  579. xfs_vn_setattr(
  580. struct dentry *dentry,
  581. struct iattr *attr)
  582. {
  583. struct inode *inode = dentry->d_inode;
  584. unsigned int ia_valid = attr->ia_valid;
  585. bhv_vnode_t *vp = vn_from_inode(inode);
  586. bhv_vattr_t vattr = { 0 };
  587. int flags = 0;
  588. int error;
  589. if (ia_valid & ATTR_UID) {
  590. vattr.va_mask |= XFS_AT_UID;
  591. vattr.va_uid = attr->ia_uid;
  592. }
  593. if (ia_valid & ATTR_GID) {
  594. vattr.va_mask |= XFS_AT_GID;
  595. vattr.va_gid = attr->ia_gid;
  596. }
  597. if (ia_valid & ATTR_SIZE) {
  598. vattr.va_mask |= XFS_AT_SIZE;
  599. vattr.va_size = attr->ia_size;
  600. }
  601. if (ia_valid & ATTR_ATIME) {
  602. vattr.va_mask |= XFS_AT_ATIME;
  603. vattr.va_atime = attr->ia_atime;
  604. inode->i_atime = attr->ia_atime;
  605. }
  606. if (ia_valid & ATTR_MTIME) {
  607. vattr.va_mask |= XFS_AT_MTIME;
  608. vattr.va_mtime = attr->ia_mtime;
  609. }
  610. if (ia_valid & ATTR_CTIME) {
  611. vattr.va_mask |= XFS_AT_CTIME;
  612. vattr.va_ctime = attr->ia_ctime;
  613. }
  614. if (ia_valid & ATTR_MODE) {
  615. vattr.va_mask |= XFS_AT_MODE;
  616. vattr.va_mode = attr->ia_mode;
  617. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  618. inode->i_mode &= ~S_ISGID;
  619. }
  620. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
  621. flags |= ATTR_UTIME;
  622. #ifdef ATTR_NO_BLOCK
  623. if ((ia_valid & ATTR_NO_BLOCK))
  624. flags |= ATTR_NONBLOCK;
  625. #endif
  626. error = bhv_vop_setattr(vp, &vattr, flags, NULL);
  627. if (likely(!error))
  628. __vn_revalidate(vp, &vattr);
  629. return -error;
  630. }
  631. STATIC void
  632. xfs_vn_truncate(
  633. struct inode *inode)
  634. {
  635. block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
  636. }
  637. STATIC int
  638. xfs_vn_setxattr(
  639. struct dentry *dentry,
  640. const char *name,
  641. const void *data,
  642. size_t size,
  643. int flags)
  644. {
  645. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  646. char *attr = (char *)name;
  647. attrnames_t *namesp;
  648. int xflags = 0;
  649. int error;
  650. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  651. if (!namesp)
  652. return -EOPNOTSUPP;
  653. attr += namesp->attr_namelen;
  654. error = namesp->attr_capable(vp, NULL);
  655. if (error)
  656. return error;
  657. /* Convert Linux syscall to XFS internal ATTR flags */
  658. if (flags & XATTR_CREATE)
  659. xflags |= ATTR_CREATE;
  660. if (flags & XATTR_REPLACE)
  661. xflags |= ATTR_REPLACE;
  662. xflags |= namesp->attr_flag;
  663. return namesp->attr_set(vp, attr, (void *)data, size, xflags);
  664. }
  665. STATIC ssize_t
  666. xfs_vn_getxattr(
  667. struct dentry *dentry,
  668. const char *name,
  669. void *data,
  670. size_t size)
  671. {
  672. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  673. char *attr = (char *)name;
  674. attrnames_t *namesp;
  675. int xflags = 0;
  676. ssize_t error;
  677. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  678. if (!namesp)
  679. return -EOPNOTSUPP;
  680. attr += namesp->attr_namelen;
  681. error = namesp->attr_capable(vp, NULL);
  682. if (error)
  683. return error;
  684. /* Convert Linux syscall to XFS internal ATTR flags */
  685. if (!size) {
  686. xflags |= ATTR_KERNOVAL;
  687. data = NULL;
  688. }
  689. xflags |= namesp->attr_flag;
  690. return namesp->attr_get(vp, attr, (void *)data, size, xflags);
  691. }
  692. STATIC ssize_t
  693. xfs_vn_listxattr(
  694. struct dentry *dentry,
  695. char *data,
  696. size_t size)
  697. {
  698. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  699. int error, xflags = ATTR_KERNAMELS;
  700. ssize_t result;
  701. if (!size)
  702. xflags |= ATTR_KERNOVAL;
  703. xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
  704. error = attr_generic_list(vp, data, size, xflags, &result);
  705. if (error < 0)
  706. return error;
  707. return result;
  708. }
  709. STATIC int
  710. xfs_vn_removexattr(
  711. struct dentry *dentry,
  712. const char *name)
  713. {
  714. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  715. char *attr = (char *)name;
  716. attrnames_t *namesp;
  717. int xflags = 0;
  718. int error;
  719. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  720. if (!namesp)
  721. return -EOPNOTSUPP;
  722. attr += namesp->attr_namelen;
  723. error = namesp->attr_capable(vp, NULL);
  724. if (error)
  725. return error;
  726. xflags |= namesp->attr_flag;
  727. return namesp->attr_remove(vp, attr, xflags);
  728. }
  729. const struct inode_operations xfs_inode_operations = {
  730. .permission = xfs_vn_permission,
  731. .truncate = xfs_vn_truncate,
  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_dir_inode_operations = {
  740. .create = xfs_vn_create,
  741. .lookup = xfs_vn_lookup,
  742. .link = xfs_vn_link,
  743. .unlink = xfs_vn_unlink,
  744. .symlink = xfs_vn_symlink,
  745. .mkdir = xfs_vn_mkdir,
  746. .rmdir = xfs_vn_rmdir,
  747. .mknod = xfs_vn_mknod,
  748. .rename = xfs_vn_rename,
  749. .permission = xfs_vn_permission,
  750. .getattr = xfs_vn_getattr,
  751. .setattr = xfs_vn_setattr,
  752. .setxattr = xfs_vn_setxattr,
  753. .getxattr = xfs_vn_getxattr,
  754. .listxattr = xfs_vn_listxattr,
  755. .removexattr = xfs_vn_removexattr,
  756. };
  757. const struct inode_operations xfs_symlink_inode_operations = {
  758. .readlink = generic_readlink,
  759. .follow_link = xfs_vn_follow_link,
  760. .put_link = xfs_vn_put_link,
  761. .permission = xfs_vn_permission,
  762. .getattr = xfs_vn_getattr,
  763. .setattr = xfs_vn_setattr,
  764. .setxattr = xfs_vn_setxattr,
  765. .getxattr = xfs_vn_getxattr,
  766. .listxattr = xfs_vn_listxattr,
  767. .removexattr = xfs_vn_removexattr,
  768. };