xfs_iops.c 19 KB

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