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