xfs_iops.c 19 KB

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