xfs_iops.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. struct vnode *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. 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 = LINVFS_GET_IP(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. validate_fields(
  187. struct inode *ip)
  188. {
  189. vnode_t *vp = LINVFS_GET_VP(ip);
  190. vattr_t va;
  191. int error;
  192. va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
  193. VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
  194. if (likely(!error)) {
  195. ip->i_nlink = va.va_nlink;
  196. ip->i_blocks = va.va_nblocks;
  197. /* we're under i_mutex so i_size can't change under us */
  198. if (i_size_read(ip) != va.va_size)
  199. i_size_write(ip, va.va_size);
  200. }
  201. }
  202. /*
  203. * Hook in SELinux. This is not quite correct yet, what we really need
  204. * here (as we do for default ACLs) is a mechanism by which creation of
  205. * these attrs can be journalled at inode creation time (along with the
  206. * inode, of course, such that log replay can't cause these to be lost).
  207. */
  208. STATIC int
  209. linvfs_init_security(
  210. struct vnode *vp,
  211. struct inode *dir)
  212. {
  213. struct inode *ip = LINVFS_GET_IP(vp);
  214. size_t length;
  215. void *value;
  216. char *name;
  217. int error;
  218. error = security_inode_init_security(ip, dir, &name, &value, &length);
  219. if (error) {
  220. if (error == -EOPNOTSUPP)
  221. return 0;
  222. return -error;
  223. }
  224. VOP_ATTR_SET(vp, name, value, length, ATTR_SECURE, NULL, error);
  225. if (!error)
  226. VMODIFY(vp);
  227. kfree(name);
  228. kfree(value);
  229. return error;
  230. }
  231. /*
  232. * Determine whether a process has a valid fs_struct (kernel daemons
  233. * like knfsd don't have an fs_struct).
  234. *
  235. * XXX(hch): nfsd is broken, better fix it instead.
  236. */
  237. STATIC inline int
  238. has_fs_struct(struct task_struct *task)
  239. {
  240. return (task->fs != init_task.fs);
  241. }
  242. STATIC inline void
  243. cleanup_inode(
  244. vnode_t *dvp,
  245. vnode_t *vp,
  246. struct dentry *dentry,
  247. int mode)
  248. {
  249. struct dentry teardown = {};
  250. int err2;
  251. /* Oh, the horror.
  252. * If we can't add the ACL or we fail in
  253. * linvfs_init_security we must back out.
  254. * ENOSPC can hit here, among other things.
  255. */
  256. teardown.d_inode = LINVFS_GET_IP(vp);
  257. teardown.d_name = dentry->d_name;
  258. if (S_ISDIR(mode))
  259. VOP_RMDIR(dvp, &teardown, NULL, err2);
  260. else
  261. VOP_REMOVE(dvp, &teardown, NULL, err2);
  262. VN_RELE(vp);
  263. }
  264. STATIC int
  265. linvfs_mknod(
  266. struct inode *dir,
  267. struct dentry *dentry,
  268. int mode,
  269. dev_t rdev)
  270. {
  271. struct inode *ip;
  272. vattr_t va;
  273. vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
  274. xfs_acl_t *default_acl = NULL;
  275. attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
  276. int error;
  277. /*
  278. * Irix uses Missed'em'V split, but doesn't want to see
  279. * the upper 5 bits of (14bit) major.
  280. */
  281. if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
  282. return -EINVAL;
  283. if (test_default_acl && test_default_acl(dvp)) {
  284. if (!_ACL_ALLOC(default_acl))
  285. return -ENOMEM;
  286. if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
  287. _ACL_FREE(default_acl);
  288. default_acl = NULL;
  289. }
  290. }
  291. if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
  292. mode &= ~current->fs->umask;
  293. memset(&va, 0, sizeof(va));
  294. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  295. va.va_mode = mode;
  296. switch (mode & S_IFMT) {
  297. case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
  298. va.va_rdev = sysv_encode_dev(rdev);
  299. va.va_mask |= XFS_AT_RDEV;
  300. /*FALLTHROUGH*/
  301. case S_IFREG:
  302. VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
  303. break;
  304. case S_IFDIR:
  305. VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
  306. break;
  307. default:
  308. error = EINVAL;
  309. break;
  310. }
  311. if (!error)
  312. {
  313. error = linvfs_init_security(vp, dir);
  314. if (error)
  315. cleanup_inode(dvp, vp, dentry, mode);
  316. }
  317. if (default_acl) {
  318. if (!error) {
  319. error = _ACL_INHERIT(vp, &va, default_acl);
  320. if (!error)
  321. VMODIFY(vp);
  322. else
  323. cleanup_inode(dvp, vp, dentry, mode);
  324. }
  325. _ACL_FREE(default_acl);
  326. }
  327. if (!error) {
  328. ASSERT(vp);
  329. ip = LINVFS_GET_IP(vp);
  330. if (S_ISCHR(mode) || S_ISBLK(mode))
  331. ip->i_rdev = rdev;
  332. else if (S_ISDIR(mode))
  333. validate_fields(ip);
  334. d_instantiate(dentry, ip);
  335. validate_fields(dir);
  336. }
  337. return -error;
  338. }
  339. STATIC int
  340. linvfs_create(
  341. struct inode *dir,
  342. struct dentry *dentry,
  343. int mode,
  344. struct nameidata *nd)
  345. {
  346. return linvfs_mknod(dir, dentry, mode, 0);
  347. }
  348. STATIC int
  349. linvfs_mkdir(
  350. struct inode *dir,
  351. struct dentry *dentry,
  352. int mode)
  353. {
  354. return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
  355. }
  356. STATIC struct dentry *
  357. linvfs_lookup(
  358. struct inode *dir,
  359. struct dentry *dentry,
  360. struct nameidata *nd)
  361. {
  362. struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
  363. int error;
  364. if (dentry->d_name.len >= MAXNAMELEN)
  365. return ERR_PTR(-ENAMETOOLONG);
  366. VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
  367. if (error) {
  368. if (unlikely(error != ENOENT))
  369. return ERR_PTR(-error);
  370. d_add(dentry, NULL);
  371. return NULL;
  372. }
  373. return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
  374. }
  375. STATIC int
  376. linvfs_link(
  377. struct dentry *old_dentry,
  378. struct inode *dir,
  379. struct dentry *dentry)
  380. {
  381. struct inode *ip; /* inode of guy being linked to */
  382. vnode_t *tdvp; /* target directory for new name/link */
  383. vnode_t *vp; /* vp of name being linked */
  384. int error;
  385. ip = old_dentry->d_inode; /* inode being linked to */
  386. if (S_ISDIR(ip->i_mode))
  387. return -EPERM;
  388. tdvp = LINVFS_GET_VP(dir);
  389. vp = LINVFS_GET_VP(ip);
  390. VOP_LINK(tdvp, vp, dentry, NULL, error);
  391. if (!error) {
  392. VMODIFY(tdvp);
  393. VN_HOLD(vp);
  394. validate_fields(ip);
  395. d_instantiate(dentry, ip);
  396. }
  397. return -error;
  398. }
  399. STATIC int
  400. linvfs_unlink(
  401. struct inode *dir,
  402. struct dentry *dentry)
  403. {
  404. struct inode *inode;
  405. vnode_t *dvp; /* directory containing name to remove */
  406. int error;
  407. inode = dentry->d_inode;
  408. dvp = LINVFS_GET_VP(dir);
  409. VOP_REMOVE(dvp, dentry, NULL, error);
  410. if (!error) {
  411. validate_fields(dir); /* For size only */
  412. validate_fields(inode);
  413. }
  414. return -error;
  415. }
  416. STATIC int
  417. linvfs_symlink(
  418. struct inode *dir,
  419. struct dentry *dentry,
  420. const char *symname)
  421. {
  422. struct inode *ip;
  423. vattr_t va;
  424. vnode_t *dvp; /* directory containing name of symlink */
  425. vnode_t *cvp; /* used to lookup symlink to put in dentry */
  426. int error;
  427. dvp = LINVFS_GET_VP(dir);
  428. cvp = NULL;
  429. memset(&va, 0, sizeof(va));
  430. va.va_mode = S_IFLNK |
  431. (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
  432. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  433. error = 0;
  434. VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
  435. if (likely(!error && cvp)) {
  436. error = linvfs_init_security(cvp, dir);
  437. if (likely(!error)) {
  438. ip = LINVFS_GET_IP(cvp);
  439. d_instantiate(dentry, ip);
  440. validate_fields(dir);
  441. validate_fields(ip);
  442. }
  443. }
  444. return -error;
  445. }
  446. STATIC int
  447. linvfs_rmdir(
  448. struct inode *dir,
  449. struct dentry *dentry)
  450. {
  451. struct inode *inode = dentry->d_inode;
  452. vnode_t *dvp = LINVFS_GET_VP(dir);
  453. int error;
  454. VOP_RMDIR(dvp, dentry, NULL, error);
  455. if (!error) {
  456. validate_fields(inode);
  457. validate_fields(dir);
  458. }
  459. return -error;
  460. }
  461. STATIC int
  462. linvfs_rename(
  463. struct inode *odir,
  464. struct dentry *odentry,
  465. struct inode *ndir,
  466. struct dentry *ndentry)
  467. {
  468. struct inode *new_inode = ndentry->d_inode;
  469. vnode_t *fvp; /* from directory */
  470. vnode_t *tvp; /* target directory */
  471. int error;
  472. fvp = LINVFS_GET_VP(odir);
  473. tvp = LINVFS_GET_VP(ndir);
  474. VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
  475. if (error)
  476. return -error;
  477. if (new_inode)
  478. validate_fields(new_inode);
  479. validate_fields(odir);
  480. if (ndir != odir)
  481. validate_fields(ndir);
  482. return 0;
  483. }
  484. /*
  485. * careful here - this function can get called recursively, so
  486. * we need to be very careful about how much stack we use.
  487. * uio is kmalloced for this reason...
  488. */
  489. STATIC void *
  490. linvfs_follow_link(
  491. struct dentry *dentry,
  492. struct nameidata *nd)
  493. {
  494. vnode_t *vp;
  495. uio_t *uio;
  496. iovec_t iov;
  497. int error;
  498. char *link;
  499. ASSERT(dentry);
  500. ASSERT(nd);
  501. link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  502. if (!link) {
  503. nd_set_link(nd, ERR_PTR(-ENOMEM));
  504. return NULL;
  505. }
  506. uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
  507. if (!uio) {
  508. kfree(link);
  509. nd_set_link(nd, ERR_PTR(-ENOMEM));
  510. return NULL;
  511. }
  512. vp = LINVFS_GET_VP(dentry->d_inode);
  513. iov.iov_base = link;
  514. iov.iov_len = MAXPATHLEN;
  515. uio->uio_iov = &iov;
  516. uio->uio_offset = 0;
  517. uio->uio_segflg = UIO_SYSSPACE;
  518. uio->uio_resid = MAXPATHLEN;
  519. uio->uio_iovcnt = 1;
  520. VOP_READLINK(vp, uio, 0, NULL, error);
  521. if (error) {
  522. kfree(link);
  523. link = ERR_PTR(-error);
  524. } else {
  525. link[MAXPATHLEN - uio->uio_resid] = '\0';
  526. }
  527. kfree(uio);
  528. nd_set_link(nd, link);
  529. return NULL;
  530. }
  531. STATIC void
  532. linvfs_put_link(
  533. struct dentry *dentry,
  534. struct nameidata *nd,
  535. void *p)
  536. {
  537. char *s = nd_get_link(nd);
  538. if (!IS_ERR(s))
  539. kfree(s);
  540. }
  541. #ifdef CONFIG_XFS_POSIX_ACL
  542. STATIC int
  543. linvfs_permission(
  544. struct inode *inode,
  545. int mode,
  546. struct nameidata *nd)
  547. {
  548. vnode_t *vp = LINVFS_GET_VP(inode);
  549. int error;
  550. mode <<= 6; /* convert from linux to vnode access bits */
  551. VOP_ACCESS(vp, mode, NULL, error);
  552. return -error;
  553. }
  554. #else
  555. #define linvfs_permission NULL
  556. #endif
  557. STATIC int
  558. linvfs_getattr(
  559. struct vfsmount *mnt,
  560. struct dentry *dentry,
  561. struct kstat *stat)
  562. {
  563. struct inode *inode = dentry->d_inode;
  564. vnode_t *vp = LINVFS_GET_VP(inode);
  565. int error = 0;
  566. if (unlikely(vp->v_flag & VMODIFIED))
  567. error = vn_revalidate(vp);
  568. if (!error)
  569. generic_fillattr(inode, stat);
  570. return 0;
  571. }
  572. STATIC int
  573. linvfs_setattr(
  574. struct dentry *dentry,
  575. struct iattr *attr)
  576. {
  577. struct inode *inode = dentry->d_inode;
  578. unsigned int ia_valid = attr->ia_valid;
  579. vnode_t *vp = LINVFS_GET_VP(inode);
  580. vattr_t vattr;
  581. int flags = 0;
  582. int error;
  583. memset(&vattr, 0, sizeof(vattr_t));
  584. if (ia_valid & ATTR_UID) {
  585. vattr.va_mask |= XFS_AT_UID;
  586. vattr.va_uid = attr->ia_uid;
  587. }
  588. if (ia_valid & ATTR_GID) {
  589. vattr.va_mask |= XFS_AT_GID;
  590. vattr.va_gid = attr->ia_gid;
  591. }
  592. if (ia_valid & ATTR_SIZE) {
  593. vattr.va_mask |= XFS_AT_SIZE;
  594. vattr.va_size = attr->ia_size;
  595. }
  596. if (ia_valid & ATTR_ATIME) {
  597. vattr.va_mask |= XFS_AT_ATIME;
  598. vattr.va_atime = attr->ia_atime;
  599. }
  600. if (ia_valid & ATTR_MTIME) {
  601. vattr.va_mask |= XFS_AT_MTIME;
  602. vattr.va_mtime = attr->ia_mtime;
  603. }
  604. if (ia_valid & ATTR_CTIME) {
  605. vattr.va_mask |= XFS_AT_CTIME;
  606. vattr.va_ctime = attr->ia_ctime;
  607. }
  608. if (ia_valid & ATTR_MODE) {
  609. vattr.va_mask |= XFS_AT_MODE;
  610. vattr.va_mode = attr->ia_mode;
  611. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  612. inode->i_mode &= ~S_ISGID;
  613. }
  614. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
  615. flags |= ATTR_UTIME;
  616. #ifdef ATTR_NO_BLOCK
  617. if ((ia_valid & ATTR_NO_BLOCK))
  618. flags |= ATTR_NONBLOCK;
  619. #endif
  620. VOP_SETATTR(vp, &vattr, flags, NULL, error);
  621. if (error)
  622. return -error;
  623. vn_revalidate(vp);
  624. return error;
  625. }
  626. STATIC void
  627. linvfs_truncate(
  628. struct inode *inode)
  629. {
  630. block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
  631. }
  632. STATIC int
  633. linvfs_setxattr(
  634. struct dentry *dentry,
  635. const char *name,
  636. const void *data,
  637. size_t size,
  638. int flags)
  639. {
  640. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  641. char *attr = (char *)name;
  642. attrnames_t *namesp;
  643. int xflags = 0;
  644. int error;
  645. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  646. if (!namesp)
  647. return -EOPNOTSUPP;
  648. attr += namesp->attr_namelen;
  649. error = namesp->attr_capable(vp, NULL);
  650. if (error)
  651. return error;
  652. /* Convert Linux syscall to XFS internal ATTR flags */
  653. if (flags & XATTR_CREATE)
  654. xflags |= ATTR_CREATE;
  655. if (flags & XATTR_REPLACE)
  656. xflags |= ATTR_REPLACE;
  657. xflags |= namesp->attr_flag;
  658. return namesp->attr_set(vp, attr, (void *)data, size, xflags);
  659. }
  660. STATIC ssize_t
  661. linvfs_getxattr(
  662. struct dentry *dentry,
  663. const char *name,
  664. void *data,
  665. size_t size)
  666. {
  667. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  668. char *attr = (char *)name;
  669. attrnames_t *namesp;
  670. int xflags = 0;
  671. ssize_t error;
  672. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  673. if (!namesp)
  674. return -EOPNOTSUPP;
  675. attr += namesp->attr_namelen;
  676. error = namesp->attr_capable(vp, NULL);
  677. if (error)
  678. return error;
  679. /* Convert Linux syscall to XFS internal ATTR flags */
  680. if (!size) {
  681. xflags |= ATTR_KERNOVAL;
  682. data = NULL;
  683. }
  684. xflags |= namesp->attr_flag;
  685. return namesp->attr_get(vp, attr, (void *)data, size, xflags);
  686. }
  687. STATIC ssize_t
  688. linvfs_listxattr(
  689. struct dentry *dentry,
  690. char *data,
  691. size_t size)
  692. {
  693. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  694. int error, xflags = ATTR_KERNAMELS;
  695. ssize_t result;
  696. if (!size)
  697. xflags |= ATTR_KERNOVAL;
  698. xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
  699. error = attr_generic_list(vp, data, size, xflags, &result);
  700. if (error < 0)
  701. return error;
  702. return result;
  703. }
  704. STATIC int
  705. linvfs_removexattr(
  706. struct dentry *dentry,
  707. const char *name)
  708. {
  709. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  710. char *attr = (char *)name;
  711. attrnames_t *namesp;
  712. int xflags = 0;
  713. int error;
  714. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  715. if (!namesp)
  716. return -EOPNOTSUPP;
  717. attr += namesp->attr_namelen;
  718. error = namesp->attr_capable(vp, NULL);
  719. if (error)
  720. return error;
  721. xflags |= namesp->attr_flag;
  722. return namesp->attr_remove(vp, attr, xflags);
  723. }
  724. struct inode_operations linvfs_file_inode_operations = {
  725. .permission = linvfs_permission,
  726. .truncate = linvfs_truncate,
  727. .getattr = linvfs_getattr,
  728. .setattr = linvfs_setattr,
  729. .setxattr = linvfs_setxattr,
  730. .getxattr = linvfs_getxattr,
  731. .listxattr = linvfs_listxattr,
  732. .removexattr = linvfs_removexattr,
  733. };
  734. struct inode_operations linvfs_dir_inode_operations = {
  735. .create = linvfs_create,
  736. .lookup = linvfs_lookup,
  737. .link = linvfs_link,
  738. .unlink = linvfs_unlink,
  739. .symlink = linvfs_symlink,
  740. .mkdir = linvfs_mkdir,
  741. .rmdir = linvfs_rmdir,
  742. .mknod = linvfs_mknod,
  743. .rename = linvfs_rename,
  744. .permission = linvfs_permission,
  745. .getattr = linvfs_getattr,
  746. .setattr = linvfs_setattr,
  747. .setxattr = linvfs_setxattr,
  748. .getxattr = linvfs_getxattr,
  749. .listxattr = linvfs_listxattr,
  750. .removexattr = linvfs_removexattr,
  751. };
  752. struct inode_operations linvfs_symlink_inode_operations = {
  753. .readlink = generic_readlink,
  754. .follow_link = linvfs_follow_link,
  755. .put_link = linvfs_put_link,
  756. .permission = linvfs_permission,
  757. .getattr = linvfs_getattr,
  758. .setattr = linvfs_setattr,
  759. .setxattr = linvfs_setxattr,
  760. .getxattr = linvfs_getxattr,
  761. .listxattr = linvfs_listxattr,
  762. .removexattr = linvfs_removexattr,
  763. };