xfs_iops.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. #include <linux/falloc.h>
  55. /*
  56. * Bring the atime in the XFS inode uptodate.
  57. * Used before logging the inode to disk or when the Linux inode goes away.
  58. */
  59. void
  60. xfs_synchronize_atime(
  61. xfs_inode_t *ip)
  62. {
  63. struct inode *inode = ip->i_vnode;
  64. if (inode) {
  65. ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
  66. ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
  67. }
  68. }
  69. /*
  70. * If the linux inode exists, mark it dirty.
  71. * Used when commiting a dirty inode into a transaction so that
  72. * the inode will get written back by the linux code
  73. */
  74. void
  75. xfs_mark_inode_dirty_sync(
  76. xfs_inode_t *ip)
  77. {
  78. struct inode *inode = ip->i_vnode;
  79. if (inode)
  80. mark_inode_dirty_sync(inode);
  81. }
  82. /*
  83. * Change the requested timestamp in the given inode.
  84. * We don't lock across timestamp updates, and we don't log them but
  85. * we do record the fact that there is dirty information in core.
  86. *
  87. * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
  88. * with XFS_ICHGTIME_ACC to be sure that access time
  89. * update will take. Calling first with XFS_ICHGTIME_ACC
  90. * and then XFS_ICHGTIME_MOD may fail to modify the access
  91. * timestamp if the filesystem is mounted noacctm.
  92. */
  93. void
  94. xfs_ichgtime(
  95. xfs_inode_t *ip,
  96. int flags)
  97. {
  98. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  99. timespec_t tv;
  100. nanotime(&tv);
  101. if (flags & XFS_ICHGTIME_MOD) {
  102. inode->i_mtime = tv;
  103. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  104. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  105. }
  106. if (flags & XFS_ICHGTIME_ACC) {
  107. inode->i_atime = tv;
  108. ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
  109. ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
  110. }
  111. if (flags & XFS_ICHGTIME_CHG) {
  112. inode->i_ctime = tv;
  113. ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
  114. ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
  115. }
  116. /*
  117. * We update the i_update_core field _after_ changing
  118. * the timestamps in order to coordinate properly with
  119. * xfs_iflush() so that we don't lose timestamp updates.
  120. * This keeps us from having to hold the inode lock
  121. * while doing this. We use the SYNCHRONIZE macro to
  122. * ensure that the compiler does not reorder the update
  123. * of i_update_core above the timestamp updates above.
  124. */
  125. SYNCHRONIZE();
  126. ip->i_update_core = 1;
  127. if (!(inode->i_state & I_NEW))
  128. mark_inode_dirty_sync(inode);
  129. }
  130. /*
  131. * Variant on the above which avoids querying the system clock
  132. * in situations where we know the Linux inode timestamps have
  133. * just been updated (and so we can update our inode cheaply).
  134. */
  135. void
  136. xfs_ichgtime_fast(
  137. xfs_inode_t *ip,
  138. struct inode *inode,
  139. int flags)
  140. {
  141. timespec_t *tvp;
  142. /*
  143. * Atime updates for read() & friends are handled lazily now, and
  144. * explicit updates must go through xfs_ichgtime()
  145. */
  146. ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
  147. if (flags & XFS_ICHGTIME_MOD) {
  148. tvp = &inode->i_mtime;
  149. ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
  150. ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
  151. }
  152. if (flags & XFS_ICHGTIME_CHG) {
  153. tvp = &inode->i_ctime;
  154. ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
  155. ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
  156. }
  157. /*
  158. * We update the i_update_core field _after_ changing
  159. * the timestamps in order to coordinate properly with
  160. * xfs_iflush() so that we don't lose timestamp updates.
  161. * This keeps us from having to hold the inode lock
  162. * while doing this. We use the SYNCHRONIZE macro to
  163. * ensure that the compiler does not reorder the update
  164. * of i_update_core above the timestamp updates above.
  165. */
  166. SYNCHRONIZE();
  167. ip->i_update_core = 1;
  168. if (!(inode->i_state & I_NEW))
  169. mark_inode_dirty_sync(inode);
  170. }
  171. /*
  172. * Pull the link count and size up from the xfs inode to the linux inode
  173. */
  174. STATIC void
  175. xfs_validate_fields(
  176. struct inode *inode)
  177. {
  178. struct xfs_inode *ip = XFS_I(inode);
  179. loff_t size;
  180. /* we're under i_sem so i_size can't change under us */
  181. size = XFS_ISIZE(ip);
  182. if (i_size_read(inode) != size)
  183. i_size_write(inode, size);
  184. }
  185. /*
  186. * Hook in SELinux. This is not quite correct yet, what we really need
  187. * here (as we do for default ACLs) is a mechanism by which creation of
  188. * these attrs can be journalled at inode creation time (along with the
  189. * inode, of course, such that log replay can't cause these to be lost).
  190. */
  191. STATIC int
  192. xfs_init_security(
  193. struct inode *inode,
  194. struct inode *dir)
  195. {
  196. struct xfs_inode *ip = XFS_I(inode);
  197. size_t length;
  198. void *value;
  199. char *name;
  200. int error;
  201. error = security_inode_init_security(inode, dir, &name,
  202. &value, &length);
  203. if (error) {
  204. if (error == -EOPNOTSUPP)
  205. return 0;
  206. return -error;
  207. }
  208. error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
  209. if (!error)
  210. xfs_iflags_set(ip, XFS_IMODIFIED);
  211. kfree(name);
  212. kfree(value);
  213. return error;
  214. }
  215. static void
  216. xfs_dentry_to_name(
  217. struct xfs_name *namep,
  218. struct dentry *dentry)
  219. {
  220. namep->name = dentry->d_name.name;
  221. namep->len = dentry->d_name.len;
  222. }
  223. STATIC void
  224. xfs_cleanup_inode(
  225. struct inode *dir,
  226. struct inode *inode,
  227. struct dentry *dentry,
  228. int mode)
  229. {
  230. struct xfs_name 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. xfs_dentry_to_name(&teardown, dentry);
  237. if (S_ISDIR(mode))
  238. xfs_rmdir(XFS_I(dir), &teardown, XFS_I(inode));
  239. else
  240. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  241. iput(inode);
  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 *inode;
  251. struct xfs_inode *ip = NULL;
  252. xfs_acl_t *default_acl = NULL;
  253. struct xfs_name name;
  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 (test_default_acl && test_default_acl(dir)) {
  263. if (!_ACL_ALLOC(default_acl)) {
  264. return -ENOMEM;
  265. }
  266. if (!_ACL_GET_DEFAULT(dir, default_acl)) {
  267. _ACL_FREE(default_acl);
  268. default_acl = NULL;
  269. }
  270. }
  271. xfs_dentry_to_name(&name, dentry);
  272. if (IS_POSIXACL(dir) && !default_acl)
  273. mode &= ~current->fs->umask;
  274. switch (mode & S_IFMT) {
  275. case S_IFCHR:
  276. case S_IFBLK:
  277. case S_IFIFO:
  278. case S_IFSOCK:
  279. rdev = sysv_encode_dev(rdev);
  280. case S_IFREG:
  281. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
  282. break;
  283. case S_IFDIR:
  284. error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
  285. break;
  286. default:
  287. error = EINVAL;
  288. break;
  289. }
  290. if (unlikely(error))
  291. goto out_free_acl;
  292. inode = ip->i_vnode;
  293. error = xfs_init_security(inode, dir);
  294. if (unlikely(error))
  295. goto out_cleanup_inode;
  296. if (default_acl) {
  297. error = _ACL_INHERIT(inode, mode, default_acl);
  298. if (unlikely(error))
  299. goto out_cleanup_inode;
  300. xfs_iflags_set(ip, XFS_IMODIFIED);
  301. _ACL_FREE(default_acl);
  302. }
  303. if (S_ISDIR(mode))
  304. xfs_validate_fields(inode);
  305. d_instantiate(dentry, inode);
  306. xfs_validate_fields(dir);
  307. return -error;
  308. out_cleanup_inode:
  309. xfs_cleanup_inode(dir, inode, dentry, mode);
  310. out_free_acl:
  311. if (default_acl)
  312. _ACL_FREE(default_acl);
  313. return -error;
  314. }
  315. STATIC int
  316. xfs_vn_create(
  317. struct inode *dir,
  318. struct dentry *dentry,
  319. int mode,
  320. struct nameidata *nd)
  321. {
  322. return xfs_vn_mknod(dir, dentry, mode, 0);
  323. }
  324. STATIC int
  325. xfs_vn_mkdir(
  326. struct inode *dir,
  327. struct dentry *dentry,
  328. int mode)
  329. {
  330. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  331. }
  332. STATIC struct dentry *
  333. xfs_vn_lookup(
  334. struct inode *dir,
  335. struct dentry *dentry,
  336. struct nameidata *nd)
  337. {
  338. struct xfs_inode *cip;
  339. struct xfs_name name;
  340. int error;
  341. if (dentry->d_name.len >= MAXNAMELEN)
  342. return ERR_PTR(-ENAMETOOLONG);
  343. xfs_dentry_to_name(&name, dentry);
  344. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  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(cip->i_vnode, dentry);
  352. }
  353. STATIC struct dentry *
  354. xfs_vn_ci_lookup(
  355. struct inode *dir,
  356. struct dentry *dentry,
  357. struct nameidata *nd)
  358. {
  359. struct xfs_inode *ip;
  360. struct xfs_name xname;
  361. struct xfs_name ci_name;
  362. struct qstr dname;
  363. int error;
  364. if (dentry->d_name.len >= MAXNAMELEN)
  365. return ERR_PTR(-ENAMETOOLONG);
  366. xfs_dentry_to_name(&xname, dentry);
  367. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  368. if (unlikely(error)) {
  369. if (unlikely(error != ENOENT))
  370. return ERR_PTR(-error);
  371. d_add(dentry, NULL);
  372. return NULL;
  373. }
  374. /* if exact match, just splice and exit */
  375. if (!ci_name.name)
  376. return d_splice_alias(ip->i_vnode, dentry);
  377. /* else case-insensitive match... */
  378. dname.name = ci_name.name;
  379. dname.len = ci_name.len;
  380. dentry = d_add_ci(ip->i_vnode, dentry, &dname);
  381. kmem_free(ci_name.name);
  382. return dentry;
  383. }
  384. STATIC int
  385. xfs_vn_link(
  386. struct dentry *old_dentry,
  387. struct inode *dir,
  388. struct dentry *dentry)
  389. {
  390. struct inode *inode; /* inode of guy being linked to */
  391. struct xfs_name name;
  392. int error;
  393. inode = old_dentry->d_inode;
  394. xfs_dentry_to_name(&name, dentry);
  395. igrab(inode);
  396. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  397. if (unlikely(error)) {
  398. iput(inode);
  399. return -error;
  400. }
  401. xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
  402. xfs_validate_fields(inode);
  403. d_instantiate(dentry, inode);
  404. return 0;
  405. }
  406. STATIC int
  407. xfs_vn_unlink(
  408. struct inode *dir,
  409. struct dentry *dentry)
  410. {
  411. struct inode *inode;
  412. struct xfs_name name;
  413. int error;
  414. inode = dentry->d_inode;
  415. xfs_dentry_to_name(&name, dentry);
  416. error = xfs_remove(XFS_I(dir), &name, XFS_I(inode));
  417. if (likely(!error)) {
  418. xfs_validate_fields(dir); /* size needs update */
  419. xfs_validate_fields(inode);
  420. }
  421. return -error;
  422. }
  423. STATIC int
  424. xfs_vn_symlink(
  425. struct inode *dir,
  426. struct dentry *dentry,
  427. const char *symname)
  428. {
  429. struct inode *inode;
  430. struct xfs_inode *cip = NULL;
  431. struct xfs_name name;
  432. int error;
  433. mode_t mode;
  434. mode = S_IFLNK |
  435. (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
  436. xfs_dentry_to_name(&name, dentry);
  437. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
  438. if (unlikely(error))
  439. goto out;
  440. inode = cip->i_vnode;
  441. error = xfs_init_security(inode, dir);
  442. if (unlikely(error))
  443. goto out_cleanup_inode;
  444. d_instantiate(dentry, inode);
  445. xfs_validate_fields(dir);
  446. xfs_validate_fields(inode);
  447. return 0;
  448. out_cleanup_inode:
  449. xfs_cleanup_inode(dir, inode, dentry, 0);
  450. out:
  451. return -error;
  452. }
  453. STATIC int
  454. xfs_vn_rmdir(
  455. struct inode *dir,
  456. struct dentry *dentry)
  457. {
  458. struct inode *inode = dentry->d_inode;
  459. struct xfs_name name;
  460. int error;
  461. xfs_dentry_to_name(&name, dentry);
  462. error = xfs_rmdir(XFS_I(dir), &name, XFS_I(inode));
  463. if (likely(!error)) {
  464. xfs_validate_fields(inode);
  465. xfs_validate_fields(dir);
  466. }
  467. return -error;
  468. }
  469. STATIC int
  470. xfs_vn_rename(
  471. struct inode *odir,
  472. struct dentry *odentry,
  473. struct inode *ndir,
  474. struct dentry *ndentry)
  475. {
  476. struct inode *new_inode = ndentry->d_inode;
  477. struct xfs_name oname;
  478. struct xfs_name nname;
  479. int error;
  480. xfs_dentry_to_name(&oname, odentry);
  481. xfs_dentry_to_name(&nname, ndentry);
  482. error = xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  483. XFS_I(ndir), &nname, new_inode ?
  484. XFS_I(new_inode) : NULL);
  485. if (likely(!error)) {
  486. if (new_inode)
  487. xfs_validate_fields(new_inode);
  488. xfs_validate_fields(odir);
  489. if (ndir != odir)
  490. xfs_validate_fields(ndir);
  491. }
  492. return -error;
  493. }
  494. /*
  495. * careful here - this function can get called recursively, so
  496. * we need to be very careful about how much stack we use.
  497. * uio is kmalloced for this reason...
  498. */
  499. STATIC void *
  500. xfs_vn_follow_link(
  501. struct dentry *dentry,
  502. struct nameidata *nd)
  503. {
  504. char *link;
  505. int error = -ENOMEM;
  506. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  507. if (!link)
  508. goto out_err;
  509. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  510. if (unlikely(error))
  511. goto out_kfree;
  512. nd_set_link(nd, link);
  513. return NULL;
  514. out_kfree:
  515. kfree(link);
  516. out_err:
  517. nd_set_link(nd, ERR_PTR(error));
  518. return NULL;
  519. }
  520. STATIC void
  521. xfs_vn_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. xfs_check_acl(
  533. struct inode *inode,
  534. int mask)
  535. {
  536. struct xfs_inode *ip = XFS_I(inode);
  537. int error;
  538. xfs_itrace_entry(ip);
  539. if (XFS_IFORK_Q(ip)) {
  540. error = xfs_acl_iaccess(ip, mask, NULL);
  541. if (error != -1)
  542. return -error;
  543. }
  544. return -EAGAIN;
  545. }
  546. STATIC int
  547. xfs_vn_permission(
  548. struct inode *inode,
  549. int mask)
  550. {
  551. return generic_permission(inode, mask, xfs_check_acl);
  552. }
  553. #else
  554. #define xfs_vn_permission NULL
  555. #endif
  556. STATIC int
  557. xfs_vn_getattr(
  558. struct vfsmount *mnt,
  559. struct dentry *dentry,
  560. struct kstat *stat)
  561. {
  562. struct inode *inode = dentry->d_inode;
  563. struct xfs_inode *ip = XFS_I(inode);
  564. struct xfs_mount *mp = ip->i_mount;
  565. xfs_itrace_entry(ip);
  566. if (XFS_FORCED_SHUTDOWN(mp))
  567. return XFS_ERROR(EIO);
  568. stat->size = XFS_ISIZE(ip);
  569. stat->dev = inode->i_sb->s_dev;
  570. stat->mode = ip->i_d.di_mode;
  571. stat->nlink = ip->i_d.di_nlink;
  572. stat->uid = ip->i_d.di_uid;
  573. stat->gid = ip->i_d.di_gid;
  574. stat->ino = ip->i_ino;
  575. #if XFS_BIG_INUMS
  576. stat->ino += mp->m_inoadd;
  577. #endif
  578. stat->atime = inode->i_atime;
  579. stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  580. stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  581. stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  582. stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  583. stat->blocks =
  584. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  585. switch (inode->i_mode & S_IFMT) {
  586. case S_IFBLK:
  587. case S_IFCHR:
  588. stat->blksize = BLKDEV_IOSIZE;
  589. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  590. sysv_minor(ip->i_df.if_u2.if_rdev));
  591. break;
  592. default:
  593. if (XFS_IS_REALTIME_INODE(ip)) {
  594. /*
  595. * If the file blocks are being allocated from a
  596. * realtime volume, then return the inode's realtime
  597. * extent size or the realtime volume's extent size.
  598. */
  599. stat->blksize =
  600. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  601. } else
  602. stat->blksize = xfs_preferred_iosize(mp);
  603. stat->rdev = 0;
  604. break;
  605. }
  606. return 0;
  607. }
  608. STATIC int
  609. xfs_vn_setattr(
  610. struct dentry *dentry,
  611. struct iattr *attr)
  612. {
  613. struct inode *inode = dentry->d_inode;
  614. unsigned int ia_valid = attr->ia_valid;
  615. bhv_vattr_t vattr = { 0 };
  616. int flags = 0;
  617. int error;
  618. if (ia_valid & ATTR_UID) {
  619. vattr.va_mask |= XFS_AT_UID;
  620. vattr.va_uid = attr->ia_uid;
  621. }
  622. if (ia_valid & ATTR_GID) {
  623. vattr.va_mask |= XFS_AT_GID;
  624. vattr.va_gid = attr->ia_gid;
  625. }
  626. if (ia_valid & ATTR_SIZE) {
  627. vattr.va_mask |= XFS_AT_SIZE;
  628. vattr.va_size = attr->ia_size;
  629. }
  630. if (ia_valid & ATTR_ATIME) {
  631. vattr.va_mask |= XFS_AT_ATIME;
  632. vattr.va_atime = attr->ia_atime;
  633. inode->i_atime = attr->ia_atime;
  634. }
  635. if (ia_valid & ATTR_MTIME) {
  636. vattr.va_mask |= XFS_AT_MTIME;
  637. vattr.va_mtime = attr->ia_mtime;
  638. }
  639. if (ia_valid & ATTR_CTIME) {
  640. vattr.va_mask |= XFS_AT_CTIME;
  641. vattr.va_ctime = attr->ia_ctime;
  642. }
  643. if (ia_valid & ATTR_MODE) {
  644. vattr.va_mask |= XFS_AT_MODE;
  645. vattr.va_mode = attr->ia_mode;
  646. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  647. inode->i_mode &= ~S_ISGID;
  648. }
  649. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
  650. flags |= ATTR_UTIME;
  651. #ifdef ATTR_NO_BLOCK
  652. if ((ia_valid & ATTR_NO_BLOCK))
  653. flags |= ATTR_NONBLOCK;
  654. #endif
  655. error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
  656. if (likely(!error))
  657. vn_revalidate(vn_from_inode(inode));
  658. return -error;
  659. }
  660. /*
  661. * block_truncate_page can return an error, but we can't propagate it
  662. * at all here. Leave a complaint + stack trace in the syslog because
  663. * this could be bad. If it is bad, we need to propagate the error further.
  664. */
  665. STATIC void
  666. xfs_vn_truncate(
  667. struct inode *inode)
  668. {
  669. int error;
  670. error = block_truncate_page(inode->i_mapping, inode->i_size,
  671. xfs_get_blocks);
  672. WARN_ON(error);
  673. }
  674. STATIC int
  675. xfs_vn_setxattr(
  676. struct dentry *dentry,
  677. const char *name,
  678. const void *data,
  679. size_t size,
  680. int flags)
  681. {
  682. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  683. char *attr = (char *)name;
  684. attrnames_t *namesp;
  685. int xflags = 0;
  686. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  687. if (!namesp)
  688. return -EOPNOTSUPP;
  689. attr += namesp->attr_namelen;
  690. /* Convert Linux syscall to XFS internal ATTR flags */
  691. if (flags & XATTR_CREATE)
  692. xflags |= ATTR_CREATE;
  693. if (flags & XATTR_REPLACE)
  694. xflags |= ATTR_REPLACE;
  695. xflags |= namesp->attr_flag;
  696. return namesp->attr_set(vp, attr, (void *)data, size, xflags);
  697. }
  698. STATIC ssize_t
  699. xfs_vn_getxattr(
  700. struct dentry *dentry,
  701. const char *name,
  702. void *data,
  703. size_t size)
  704. {
  705. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  706. char *attr = (char *)name;
  707. attrnames_t *namesp;
  708. int xflags = 0;
  709. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  710. if (!namesp)
  711. return -EOPNOTSUPP;
  712. attr += namesp->attr_namelen;
  713. /* Convert Linux syscall to XFS internal ATTR flags */
  714. if (!size) {
  715. xflags |= ATTR_KERNOVAL;
  716. data = NULL;
  717. }
  718. xflags |= namesp->attr_flag;
  719. return namesp->attr_get(vp, attr, (void *)data, size, xflags);
  720. }
  721. STATIC ssize_t
  722. xfs_vn_listxattr(
  723. struct dentry *dentry,
  724. char *data,
  725. size_t size)
  726. {
  727. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  728. int error, xflags = ATTR_KERNAMELS;
  729. ssize_t result;
  730. if (!size)
  731. xflags |= ATTR_KERNOVAL;
  732. xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
  733. error = attr_generic_list(vp, data, size, xflags, &result);
  734. if (error < 0)
  735. return error;
  736. return result;
  737. }
  738. STATIC int
  739. xfs_vn_removexattr(
  740. struct dentry *dentry,
  741. const char *name)
  742. {
  743. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  744. char *attr = (char *)name;
  745. attrnames_t *namesp;
  746. int xflags = 0;
  747. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  748. if (!namesp)
  749. return -EOPNOTSUPP;
  750. attr += namesp->attr_namelen;
  751. xflags |= namesp->attr_flag;
  752. return namesp->attr_remove(vp, attr, xflags);
  753. }
  754. STATIC long
  755. xfs_vn_fallocate(
  756. struct inode *inode,
  757. int mode,
  758. loff_t offset,
  759. loff_t len)
  760. {
  761. long error;
  762. loff_t new_size = 0;
  763. xfs_flock64_t bf;
  764. xfs_inode_t *ip = XFS_I(inode);
  765. /* preallocation on directories not yet supported */
  766. error = -ENODEV;
  767. if (S_ISDIR(inode->i_mode))
  768. goto out_error;
  769. bf.l_whence = 0;
  770. bf.l_start = offset;
  771. bf.l_len = len;
  772. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  773. error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
  774. 0, NULL, ATTR_NOLOCK);
  775. if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
  776. offset + len > i_size_read(inode))
  777. new_size = offset + len;
  778. /* Change file size if needed */
  779. if (new_size) {
  780. bhv_vattr_t va;
  781. va.va_mask = XFS_AT_SIZE;
  782. va.va_size = new_size;
  783. error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
  784. }
  785. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  786. out_error:
  787. return error;
  788. }
  789. const struct inode_operations xfs_inode_operations = {
  790. .permission = xfs_vn_permission,
  791. .truncate = xfs_vn_truncate,
  792. .getattr = xfs_vn_getattr,
  793. .setattr = xfs_vn_setattr,
  794. .setxattr = xfs_vn_setxattr,
  795. .getxattr = xfs_vn_getxattr,
  796. .listxattr = xfs_vn_listxattr,
  797. .removexattr = xfs_vn_removexattr,
  798. .fallocate = xfs_vn_fallocate,
  799. };
  800. const struct inode_operations xfs_dir_inode_operations = {
  801. .create = xfs_vn_create,
  802. .lookup = xfs_vn_lookup,
  803. .link = xfs_vn_link,
  804. .unlink = xfs_vn_unlink,
  805. .symlink = xfs_vn_symlink,
  806. .mkdir = xfs_vn_mkdir,
  807. .rmdir = xfs_vn_rmdir,
  808. .mknod = xfs_vn_mknod,
  809. .rename = xfs_vn_rename,
  810. .permission = xfs_vn_permission,
  811. .getattr = xfs_vn_getattr,
  812. .setattr = xfs_vn_setattr,
  813. .setxattr = xfs_vn_setxattr,
  814. .getxattr = xfs_vn_getxattr,
  815. .listxattr = xfs_vn_listxattr,
  816. .removexattr = xfs_vn_removexattr,
  817. };
  818. const struct inode_operations xfs_dir_ci_inode_operations = {
  819. .create = xfs_vn_create,
  820. .lookup = xfs_vn_ci_lookup,
  821. .link = xfs_vn_link,
  822. .unlink = xfs_vn_unlink,
  823. .symlink = xfs_vn_symlink,
  824. .mkdir = xfs_vn_mkdir,
  825. .rmdir = xfs_vn_rmdir,
  826. .mknod = xfs_vn_mknod,
  827. .rename = xfs_vn_rename,
  828. .permission = xfs_vn_permission,
  829. .getattr = xfs_vn_getattr,
  830. .setattr = xfs_vn_setattr,
  831. .setxattr = xfs_vn_setxattr,
  832. .getxattr = xfs_vn_getxattr,
  833. .listxattr = xfs_vn_listxattr,
  834. .removexattr = xfs_vn_removexattr,
  835. };
  836. const struct inode_operations xfs_symlink_inode_operations = {
  837. .readlink = generic_readlink,
  838. .follow_link = xfs_vn_follow_link,
  839. .put_link = xfs_vn_put_link,
  840. .permission = xfs_vn_permission,
  841. .getattr = xfs_vn_getattr,
  842. .setattr = xfs_vn_setattr,
  843. .setxattr = xfs_vn_setxattr,
  844. .getxattr = xfs_vn_getxattr,
  845. .listxattr = xfs_vn_listxattr,
  846. .removexattr = xfs_vn_removexattr,
  847. };