xfs_iops.c 18 KB

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