xfs_iops.c 18 KB

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