xfs_iops.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_da_format.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_bmap.h"
  28. #include "xfs_bmap_util.h"
  29. #include "xfs_acl.h"
  30. #include "xfs_quota.h"
  31. #include "xfs_error.h"
  32. #include "xfs_attr.h"
  33. #include "xfs_trans.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_icache.h"
  36. #include "xfs_symlink.h"
  37. #include "xfs_da_btree.h"
  38. #include "xfs_dir2.h"
  39. #include "xfs_trans_space.h"
  40. #include "xfs_pnfs.h"
  41. #include "xfs_iomap.h"
  42. #include <linux/capability.h>
  43. #include <linux/xattr.h>
  44. #include <linux/posix_acl.h>
  45. #include <linux/security.h>
  46. #include <linux/fiemap.h>
  47. #include <linux/slab.h>
  48. /*
  49. * Directories have different lock order w.r.t. mmap_sem compared to regular
  50. * files. This is due to readdir potentially triggering page faults on a user
  51. * buffer inside filldir(), and this happens with the ilock on the directory
  52. * held. For regular files, the lock order is the other way around - the
  53. * mmap_sem is taken during the page fault, and then we lock the ilock to do
  54. * block mapping. Hence we need a different class for the directory ilock so
  55. * that lockdep can tell them apart.
  56. */
  57. static struct lock_class_key xfs_nondir_ilock_class;
  58. static struct lock_class_key xfs_dir_ilock_class;
  59. static int
  60. xfs_initxattrs(
  61. struct inode *inode,
  62. const struct xattr *xattr_array,
  63. void *fs_info)
  64. {
  65. const struct xattr *xattr;
  66. struct xfs_inode *ip = XFS_I(inode);
  67. int error = 0;
  68. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  69. error = xfs_attr_set(ip, xattr->name, xattr->value,
  70. xattr->value_len, ATTR_SECURE);
  71. if (error < 0)
  72. break;
  73. }
  74. return error;
  75. }
  76. /*
  77. * Hook in SELinux. This is not quite correct yet, what we really need
  78. * here (as we do for default ACLs) is a mechanism by which creation of
  79. * these attrs can be journalled at inode creation time (along with the
  80. * inode, of course, such that log replay can't cause these to be lost).
  81. */
  82. STATIC int
  83. xfs_init_security(
  84. struct inode *inode,
  85. struct inode *dir,
  86. const struct qstr *qstr)
  87. {
  88. return security_inode_init_security(inode, dir, qstr,
  89. &xfs_initxattrs, NULL);
  90. }
  91. static void
  92. xfs_dentry_to_name(
  93. struct xfs_name *namep,
  94. struct dentry *dentry,
  95. int mode)
  96. {
  97. namep->name = dentry->d_name.name;
  98. namep->len = dentry->d_name.len;
  99. namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
  100. }
  101. STATIC void
  102. xfs_cleanup_inode(
  103. struct inode *dir,
  104. struct inode *inode,
  105. struct dentry *dentry)
  106. {
  107. struct xfs_name teardown;
  108. /* Oh, the horror.
  109. * If we can't add the ACL or we fail in
  110. * xfs_init_security we must back out.
  111. * ENOSPC can hit here, among other things.
  112. */
  113. xfs_dentry_to_name(&teardown, dentry, 0);
  114. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  115. }
  116. STATIC int
  117. xfs_generic_create(
  118. struct inode *dir,
  119. struct dentry *dentry,
  120. umode_t mode,
  121. dev_t rdev,
  122. bool tmpfile) /* unnamed file */
  123. {
  124. struct inode *inode;
  125. struct xfs_inode *ip = NULL;
  126. struct posix_acl *default_acl, *acl;
  127. struct xfs_name name;
  128. int error;
  129. /*
  130. * Irix uses Missed'em'V split, but doesn't want to see
  131. * the upper 5 bits of (14bit) major.
  132. */
  133. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  134. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  135. return -EINVAL;
  136. rdev = sysv_encode_dev(rdev);
  137. } else {
  138. rdev = 0;
  139. }
  140. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  141. if (error)
  142. return error;
  143. if (!tmpfile) {
  144. xfs_dentry_to_name(&name, dentry, mode);
  145. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
  146. } else {
  147. error = xfs_create_tmpfile(XFS_I(dir), dentry, mode, &ip);
  148. }
  149. if (unlikely(error))
  150. goto out_free_acl;
  151. inode = VFS_I(ip);
  152. error = xfs_init_security(inode, dir, &dentry->d_name);
  153. if (unlikely(error))
  154. goto out_cleanup_inode;
  155. #ifdef CONFIG_XFS_POSIX_ACL
  156. if (default_acl) {
  157. error = xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  158. if (error)
  159. goto out_cleanup_inode;
  160. }
  161. if (acl) {
  162. error = xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  163. if (error)
  164. goto out_cleanup_inode;
  165. }
  166. #endif
  167. xfs_setup_iops(ip);
  168. if (tmpfile)
  169. d_tmpfile(dentry, inode);
  170. else
  171. d_instantiate(dentry, inode);
  172. xfs_finish_inode_setup(ip);
  173. out_free_acl:
  174. if (default_acl)
  175. posix_acl_release(default_acl);
  176. if (acl)
  177. posix_acl_release(acl);
  178. return error;
  179. out_cleanup_inode:
  180. xfs_finish_inode_setup(ip);
  181. if (!tmpfile)
  182. xfs_cleanup_inode(dir, inode, dentry);
  183. iput(inode);
  184. goto out_free_acl;
  185. }
  186. STATIC int
  187. xfs_vn_mknod(
  188. struct inode *dir,
  189. struct dentry *dentry,
  190. umode_t mode,
  191. dev_t rdev)
  192. {
  193. return xfs_generic_create(dir, dentry, mode, rdev, false);
  194. }
  195. STATIC int
  196. xfs_vn_create(
  197. struct inode *dir,
  198. struct dentry *dentry,
  199. umode_t mode,
  200. bool flags)
  201. {
  202. return xfs_vn_mknod(dir, dentry, mode, 0);
  203. }
  204. STATIC int
  205. xfs_vn_mkdir(
  206. struct inode *dir,
  207. struct dentry *dentry,
  208. umode_t mode)
  209. {
  210. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  211. }
  212. STATIC struct dentry *
  213. xfs_vn_lookup(
  214. struct inode *dir,
  215. struct dentry *dentry,
  216. unsigned int flags)
  217. {
  218. struct xfs_inode *cip;
  219. struct xfs_name name;
  220. int error;
  221. if (dentry->d_name.len >= MAXNAMELEN)
  222. return ERR_PTR(-ENAMETOOLONG);
  223. xfs_dentry_to_name(&name, dentry, 0);
  224. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  225. if (unlikely(error)) {
  226. if (unlikely(error != -ENOENT))
  227. return ERR_PTR(error);
  228. d_add(dentry, NULL);
  229. return NULL;
  230. }
  231. return d_splice_alias(VFS_I(cip), dentry);
  232. }
  233. STATIC struct dentry *
  234. xfs_vn_ci_lookup(
  235. struct inode *dir,
  236. struct dentry *dentry,
  237. unsigned int flags)
  238. {
  239. struct xfs_inode *ip;
  240. struct xfs_name xname;
  241. struct xfs_name ci_name;
  242. struct qstr dname;
  243. int error;
  244. if (dentry->d_name.len >= MAXNAMELEN)
  245. return ERR_PTR(-ENAMETOOLONG);
  246. xfs_dentry_to_name(&xname, dentry, 0);
  247. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  248. if (unlikely(error)) {
  249. if (unlikely(error != -ENOENT))
  250. return ERR_PTR(error);
  251. /*
  252. * call d_add(dentry, NULL) here when d_drop_negative_children
  253. * is called in xfs_vn_mknod (ie. allow negative dentries
  254. * with CI filesystems).
  255. */
  256. return NULL;
  257. }
  258. /* if exact match, just splice and exit */
  259. if (!ci_name.name)
  260. return d_splice_alias(VFS_I(ip), dentry);
  261. /* else case-insensitive match... */
  262. dname.name = ci_name.name;
  263. dname.len = ci_name.len;
  264. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  265. kmem_free(ci_name.name);
  266. return dentry;
  267. }
  268. STATIC int
  269. xfs_vn_link(
  270. struct dentry *old_dentry,
  271. struct inode *dir,
  272. struct dentry *dentry)
  273. {
  274. struct inode *inode = d_inode(old_dentry);
  275. struct xfs_name name;
  276. int error;
  277. xfs_dentry_to_name(&name, dentry, inode->i_mode);
  278. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  279. if (unlikely(error))
  280. return error;
  281. ihold(inode);
  282. d_instantiate(dentry, inode);
  283. return 0;
  284. }
  285. STATIC int
  286. xfs_vn_unlink(
  287. struct inode *dir,
  288. struct dentry *dentry)
  289. {
  290. struct xfs_name name;
  291. int error;
  292. xfs_dentry_to_name(&name, dentry, 0);
  293. error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
  294. if (error)
  295. return error;
  296. /*
  297. * With unlink, the VFS makes the dentry "negative": no inode,
  298. * but still hashed. This is incompatible with case-insensitive
  299. * mode, so invalidate (unhash) the dentry in CI-mode.
  300. */
  301. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  302. d_invalidate(dentry);
  303. return 0;
  304. }
  305. STATIC int
  306. xfs_vn_symlink(
  307. struct inode *dir,
  308. struct dentry *dentry,
  309. const char *symname)
  310. {
  311. struct inode *inode;
  312. struct xfs_inode *cip = NULL;
  313. struct xfs_name name;
  314. int error;
  315. umode_t mode;
  316. mode = S_IFLNK |
  317. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  318. xfs_dentry_to_name(&name, dentry, mode);
  319. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
  320. if (unlikely(error))
  321. goto out;
  322. inode = VFS_I(cip);
  323. error = xfs_init_security(inode, dir, &dentry->d_name);
  324. if (unlikely(error))
  325. goto out_cleanup_inode;
  326. xfs_setup_iops(cip);
  327. d_instantiate(dentry, inode);
  328. xfs_finish_inode_setup(cip);
  329. return 0;
  330. out_cleanup_inode:
  331. xfs_finish_inode_setup(cip);
  332. xfs_cleanup_inode(dir, inode, dentry);
  333. iput(inode);
  334. out:
  335. return error;
  336. }
  337. STATIC int
  338. xfs_vn_rename(
  339. struct inode *odir,
  340. struct dentry *odentry,
  341. struct inode *ndir,
  342. struct dentry *ndentry,
  343. unsigned int flags)
  344. {
  345. struct inode *new_inode = d_inode(ndentry);
  346. int omode = 0;
  347. struct xfs_name oname;
  348. struct xfs_name nname;
  349. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  350. return -EINVAL;
  351. /* if we are exchanging files, we need to set i_mode of both files */
  352. if (flags & RENAME_EXCHANGE)
  353. omode = d_inode(ndentry)->i_mode;
  354. xfs_dentry_to_name(&oname, odentry, omode);
  355. xfs_dentry_to_name(&nname, ndentry, d_inode(odentry)->i_mode);
  356. return xfs_rename(XFS_I(odir), &oname, XFS_I(d_inode(odentry)),
  357. XFS_I(ndir), &nname,
  358. new_inode ? XFS_I(new_inode) : NULL, flags);
  359. }
  360. /*
  361. * careful here - this function can get called recursively, so
  362. * we need to be very careful about how much stack we use.
  363. * uio is kmalloced for this reason...
  364. */
  365. STATIC const char *
  366. xfs_vn_get_link(
  367. struct dentry *dentry,
  368. struct inode *inode,
  369. struct delayed_call *done)
  370. {
  371. char *link;
  372. int error = -ENOMEM;
  373. if (!dentry)
  374. return ERR_PTR(-ECHILD);
  375. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  376. if (!link)
  377. goto out_err;
  378. error = xfs_readlink(XFS_I(d_inode(dentry)), link);
  379. if (unlikely(error))
  380. goto out_kfree;
  381. set_delayed_call(done, kfree_link, link);
  382. return link;
  383. out_kfree:
  384. kfree(link);
  385. out_err:
  386. return ERR_PTR(error);
  387. }
  388. STATIC const char *
  389. xfs_vn_get_link_inline(
  390. struct dentry *dentry,
  391. struct inode *inode,
  392. struct delayed_call *done)
  393. {
  394. ASSERT(XFS_I(inode)->i_df.if_flags & XFS_IFINLINE);
  395. return XFS_I(inode)->i_df.if_u1.if_data;
  396. }
  397. STATIC int
  398. xfs_vn_getattr(
  399. struct vfsmount *mnt,
  400. struct dentry *dentry,
  401. struct kstat *stat)
  402. {
  403. struct inode *inode = d_inode(dentry);
  404. struct xfs_inode *ip = XFS_I(inode);
  405. struct xfs_mount *mp = ip->i_mount;
  406. trace_xfs_getattr(ip);
  407. if (XFS_FORCED_SHUTDOWN(mp))
  408. return -EIO;
  409. stat->size = XFS_ISIZE(ip);
  410. stat->dev = inode->i_sb->s_dev;
  411. stat->mode = inode->i_mode;
  412. stat->nlink = inode->i_nlink;
  413. stat->uid = inode->i_uid;
  414. stat->gid = inode->i_gid;
  415. stat->ino = ip->i_ino;
  416. stat->atime = inode->i_atime;
  417. stat->mtime = inode->i_mtime;
  418. stat->ctime = inode->i_ctime;
  419. stat->blocks =
  420. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  421. switch (inode->i_mode & S_IFMT) {
  422. case S_IFBLK:
  423. case S_IFCHR:
  424. stat->blksize = BLKDEV_IOSIZE;
  425. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  426. sysv_minor(ip->i_df.if_u2.if_rdev));
  427. break;
  428. default:
  429. if (XFS_IS_REALTIME_INODE(ip)) {
  430. /*
  431. * If the file blocks are being allocated from a
  432. * realtime volume, then return the inode's realtime
  433. * extent size or the realtime volume's extent size.
  434. */
  435. stat->blksize =
  436. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  437. } else
  438. stat->blksize = xfs_preferred_iosize(mp);
  439. stat->rdev = 0;
  440. break;
  441. }
  442. return 0;
  443. }
  444. static void
  445. xfs_setattr_mode(
  446. struct xfs_inode *ip,
  447. struct iattr *iattr)
  448. {
  449. struct inode *inode = VFS_I(ip);
  450. umode_t mode = iattr->ia_mode;
  451. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  452. inode->i_mode &= S_IFMT;
  453. inode->i_mode |= mode & ~S_IFMT;
  454. }
  455. void
  456. xfs_setattr_time(
  457. struct xfs_inode *ip,
  458. struct iattr *iattr)
  459. {
  460. struct inode *inode = VFS_I(ip);
  461. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  462. if (iattr->ia_valid & ATTR_ATIME)
  463. inode->i_atime = iattr->ia_atime;
  464. if (iattr->ia_valid & ATTR_CTIME)
  465. inode->i_ctime = iattr->ia_ctime;
  466. if (iattr->ia_valid & ATTR_MTIME)
  467. inode->i_mtime = iattr->ia_mtime;
  468. }
  469. int
  470. xfs_setattr_nonsize(
  471. struct xfs_inode *ip,
  472. struct iattr *iattr,
  473. int flags)
  474. {
  475. xfs_mount_t *mp = ip->i_mount;
  476. struct inode *inode = VFS_I(ip);
  477. int mask = iattr->ia_valid;
  478. xfs_trans_t *tp;
  479. int error;
  480. kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
  481. kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
  482. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  483. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  484. trace_xfs_setattr(ip);
  485. /* If acls are being inherited, we already have this checked */
  486. if (!(flags & XFS_ATTR_NOACL)) {
  487. if (mp->m_flags & XFS_MOUNT_RDONLY)
  488. return -EROFS;
  489. if (XFS_FORCED_SHUTDOWN(mp))
  490. return -EIO;
  491. error = inode_change_ok(inode, iattr);
  492. if (error)
  493. return error;
  494. }
  495. ASSERT((mask & ATTR_SIZE) == 0);
  496. /*
  497. * If disk quotas is on, we make sure that the dquots do exist on disk,
  498. * before we start any other transactions. Trying to do this later
  499. * is messy. We don't care to take a readlock to look at the ids
  500. * in inode here, because we can't hold it across the trans_reserve.
  501. * If the IDs do change before we take the ilock, we're covered
  502. * because the i_*dquot fields will get updated anyway.
  503. */
  504. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  505. uint qflags = 0;
  506. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  507. uid = iattr->ia_uid;
  508. qflags |= XFS_QMOPT_UQUOTA;
  509. } else {
  510. uid = inode->i_uid;
  511. }
  512. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  513. gid = iattr->ia_gid;
  514. qflags |= XFS_QMOPT_GQUOTA;
  515. } else {
  516. gid = inode->i_gid;
  517. }
  518. /*
  519. * We take a reference when we initialize udqp and gdqp,
  520. * so it is important that we never blindly double trip on
  521. * the same variable. See xfs_create() for an example.
  522. */
  523. ASSERT(udqp == NULL);
  524. ASSERT(gdqp == NULL);
  525. error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
  526. xfs_kgid_to_gid(gid),
  527. xfs_get_projid(ip),
  528. qflags, &udqp, &gdqp, NULL);
  529. if (error)
  530. return error;
  531. }
  532. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  533. if (error)
  534. goto out_dqrele;
  535. xfs_ilock(ip, XFS_ILOCK_EXCL);
  536. xfs_trans_ijoin(tp, ip, 0);
  537. /*
  538. * Change file ownership. Must be the owner or privileged.
  539. */
  540. if (mask & (ATTR_UID|ATTR_GID)) {
  541. /*
  542. * These IDs could have changed since we last looked at them.
  543. * But, we're assured that if the ownership did change
  544. * while we didn't have the inode locked, inode's dquot(s)
  545. * would have changed also.
  546. */
  547. iuid = inode->i_uid;
  548. igid = inode->i_gid;
  549. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  550. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  551. /*
  552. * Do a quota reservation only if uid/gid is actually
  553. * going to change.
  554. */
  555. if (XFS_IS_QUOTA_RUNNING(mp) &&
  556. ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
  557. (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
  558. ASSERT(tp);
  559. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  560. NULL, capable(CAP_FOWNER) ?
  561. XFS_QMOPT_FORCE_RES : 0);
  562. if (error) /* out of quota */
  563. goto out_cancel;
  564. }
  565. }
  566. /*
  567. * Change file ownership. Must be the owner or privileged.
  568. */
  569. if (mask & (ATTR_UID|ATTR_GID)) {
  570. /*
  571. * CAP_FSETID overrides the following restrictions:
  572. *
  573. * The set-user-ID and set-group-ID bits of a file will be
  574. * cleared upon successful return from chown()
  575. */
  576. if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
  577. !capable(CAP_FSETID))
  578. inode->i_mode &= ~(S_ISUID|S_ISGID);
  579. /*
  580. * Change the ownerships and register quota modifications
  581. * in the transaction.
  582. */
  583. if (!uid_eq(iuid, uid)) {
  584. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  585. ASSERT(mask & ATTR_UID);
  586. ASSERT(udqp);
  587. olddquot1 = xfs_qm_vop_chown(tp, ip,
  588. &ip->i_udquot, udqp);
  589. }
  590. ip->i_d.di_uid = xfs_kuid_to_uid(uid);
  591. inode->i_uid = uid;
  592. }
  593. if (!gid_eq(igid, gid)) {
  594. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  595. ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
  596. !XFS_IS_PQUOTA_ON(mp));
  597. ASSERT(mask & ATTR_GID);
  598. ASSERT(gdqp);
  599. olddquot2 = xfs_qm_vop_chown(tp, ip,
  600. &ip->i_gdquot, gdqp);
  601. }
  602. ip->i_d.di_gid = xfs_kgid_to_gid(gid);
  603. inode->i_gid = gid;
  604. }
  605. }
  606. if (mask & ATTR_MODE)
  607. xfs_setattr_mode(ip, iattr);
  608. if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  609. xfs_setattr_time(ip, iattr);
  610. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  611. XFS_STATS_INC(mp, xs_ig_attrchg);
  612. if (mp->m_flags & XFS_MOUNT_WSYNC)
  613. xfs_trans_set_sync(tp);
  614. error = xfs_trans_commit(tp);
  615. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  616. /*
  617. * Release any dquot(s) the inode had kept before chown.
  618. */
  619. xfs_qm_dqrele(olddquot1);
  620. xfs_qm_dqrele(olddquot2);
  621. xfs_qm_dqrele(udqp);
  622. xfs_qm_dqrele(gdqp);
  623. if (error)
  624. return error;
  625. /*
  626. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  627. * update. We could avoid this with linked transactions
  628. * and passing down the transaction pointer all the way
  629. * to attr_set. No previous user of the generic
  630. * Posix ACL code seems to care about this issue either.
  631. */
  632. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  633. error = posix_acl_chmod(inode, inode->i_mode);
  634. if (error)
  635. return error;
  636. }
  637. return 0;
  638. out_cancel:
  639. xfs_trans_cancel(tp);
  640. out_dqrele:
  641. xfs_qm_dqrele(udqp);
  642. xfs_qm_dqrele(gdqp);
  643. return error;
  644. }
  645. /*
  646. * Truncate file. Must have write permission and not be a directory.
  647. */
  648. int
  649. xfs_setattr_size(
  650. struct xfs_inode *ip,
  651. struct iattr *iattr)
  652. {
  653. struct xfs_mount *mp = ip->i_mount;
  654. struct inode *inode = VFS_I(ip);
  655. xfs_off_t oldsize, newsize;
  656. struct xfs_trans *tp;
  657. int error;
  658. uint lock_flags = 0;
  659. bool did_zeroing = false;
  660. trace_xfs_setattr(ip);
  661. if (mp->m_flags & XFS_MOUNT_RDONLY)
  662. return -EROFS;
  663. if (XFS_FORCED_SHUTDOWN(mp))
  664. return -EIO;
  665. error = inode_change_ok(inode, iattr);
  666. if (error)
  667. return error;
  668. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  669. ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
  670. ASSERT(S_ISREG(inode->i_mode));
  671. ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  672. ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  673. oldsize = inode->i_size;
  674. newsize = iattr->ia_size;
  675. /*
  676. * Short circuit the truncate case for zero length files.
  677. */
  678. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  679. if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
  680. return 0;
  681. /*
  682. * Use the regular setattr path to update the timestamps.
  683. */
  684. iattr->ia_valid &= ~ATTR_SIZE;
  685. return xfs_setattr_nonsize(ip, iattr, 0);
  686. }
  687. /*
  688. * Make sure that the dquots are attached to the inode.
  689. */
  690. error = xfs_qm_dqattach(ip, 0);
  691. if (error)
  692. return error;
  693. /*
  694. * Wait for all direct I/O to complete.
  695. */
  696. inode_dio_wait(inode);
  697. /*
  698. * File data changes must be complete before we start the transaction to
  699. * modify the inode. This needs to be done before joining the inode to
  700. * the transaction because the inode cannot be unlocked once it is a
  701. * part of the transaction.
  702. *
  703. * Start with zeroing any data beyond EOF that we may expose on file
  704. * extension, or zeroing out the rest of the block on a downward
  705. * truncate.
  706. */
  707. if (newsize > oldsize) {
  708. error = xfs_zero_eof(ip, newsize, oldsize, &did_zeroing);
  709. } else {
  710. if (IS_DAX(inode)) {
  711. error = dax_truncate_page(inode, newsize,
  712. xfs_get_blocks_direct);
  713. } else {
  714. error = iomap_truncate_page(inode, newsize,
  715. &did_zeroing, &xfs_iomap_ops);
  716. }
  717. }
  718. if (error)
  719. return error;
  720. /*
  721. * We are going to log the inode size change in this transaction so
  722. * any previous writes that are beyond the on disk EOF and the new
  723. * EOF that have not been written out need to be written here. If we
  724. * do not write the data out, we expose ourselves to the null files
  725. * problem. Note that this includes any block zeroing we did above;
  726. * otherwise those blocks may not be zeroed after a crash.
  727. */
  728. if (did_zeroing ||
  729. (newsize > ip->i_d.di_size && oldsize != ip->i_d.di_size)) {
  730. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  731. ip->i_d.di_size, newsize);
  732. if (error)
  733. return error;
  734. }
  735. /*
  736. * We've already locked out new page faults, so now we can safely remove
  737. * pages from the page cache knowing they won't get refaulted until we
  738. * drop the XFS_MMAP_EXCL lock after the extent manipulations are
  739. * complete. The truncate_setsize() call also cleans partial EOF page
  740. * PTEs on extending truncates and hence ensures sub-page block size
  741. * filesystems are correctly handled, too.
  742. *
  743. * We have to do all the page cache truncate work outside the
  744. * transaction context as the "lock" order is page lock->log space
  745. * reservation as defined by extent allocation in the writeback path.
  746. * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
  747. * having already truncated the in-memory version of the file (i.e. made
  748. * user visible changes). There's not much we can do about this, except
  749. * to hope that the caller sees ENOMEM and retries the truncate
  750. * operation.
  751. */
  752. truncate_setsize(inode, newsize);
  753. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  754. if (error)
  755. return error;
  756. lock_flags |= XFS_ILOCK_EXCL;
  757. xfs_ilock(ip, XFS_ILOCK_EXCL);
  758. xfs_trans_ijoin(tp, ip, 0);
  759. /*
  760. * Only change the c/mtime if we are changing the size or we are
  761. * explicitly asked to change it. This handles the semantic difference
  762. * between truncate() and ftruncate() as implemented in the VFS.
  763. *
  764. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  765. * special case where we need to update the times despite not having
  766. * these flags set. For all other operations the VFS set these flags
  767. * explicitly if it wants a timestamp update.
  768. */
  769. if (newsize != oldsize &&
  770. !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
  771. iattr->ia_ctime = iattr->ia_mtime =
  772. current_fs_time(inode->i_sb);
  773. iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  774. }
  775. /*
  776. * The first thing we do is set the size to new_size permanently on
  777. * disk. This way we don't have to worry about anyone ever being able
  778. * to look at the data being freed even in the face of a crash.
  779. * What we're getting around here is the case where we free a block, it
  780. * is allocated to another file, it is written to, and then we crash.
  781. * If the new data gets written to the file but the log buffers
  782. * containing the free and reallocation don't, then we'd end up with
  783. * garbage in the blocks being freed. As long as we make the new size
  784. * permanent before actually freeing any blocks it doesn't matter if
  785. * they get written to.
  786. */
  787. ip->i_d.di_size = newsize;
  788. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  789. if (newsize <= oldsize) {
  790. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  791. if (error)
  792. goto out_trans_cancel;
  793. /*
  794. * Truncated "down", so we're removing references to old data
  795. * here - if we delay flushing for a long time, we expose
  796. * ourselves unduly to the notorious NULL files problem. So,
  797. * we mark this inode and flush it when the file is closed,
  798. * and do not wait the usual (long) time for writeout.
  799. */
  800. xfs_iflags_set(ip, XFS_ITRUNCATED);
  801. /* A truncate down always removes post-EOF blocks. */
  802. xfs_inode_clear_eofblocks_tag(ip);
  803. }
  804. if (iattr->ia_valid & ATTR_MODE)
  805. xfs_setattr_mode(ip, iattr);
  806. if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  807. xfs_setattr_time(ip, iattr);
  808. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  809. XFS_STATS_INC(mp, xs_ig_attrchg);
  810. if (mp->m_flags & XFS_MOUNT_WSYNC)
  811. xfs_trans_set_sync(tp);
  812. error = xfs_trans_commit(tp);
  813. out_unlock:
  814. if (lock_flags)
  815. xfs_iunlock(ip, lock_flags);
  816. return error;
  817. out_trans_cancel:
  818. xfs_trans_cancel(tp);
  819. goto out_unlock;
  820. }
  821. STATIC int
  822. xfs_vn_setattr(
  823. struct dentry *dentry,
  824. struct iattr *iattr)
  825. {
  826. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  827. int error;
  828. if (iattr->ia_valid & ATTR_SIZE) {
  829. uint iolock = XFS_IOLOCK_EXCL;
  830. xfs_ilock(ip, iolock);
  831. error = xfs_break_layouts(d_inode(dentry), &iolock, true);
  832. if (!error) {
  833. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  834. iolock |= XFS_MMAPLOCK_EXCL;
  835. error = xfs_setattr_size(ip, iattr);
  836. }
  837. xfs_iunlock(ip, iolock);
  838. } else {
  839. error = xfs_setattr_nonsize(ip, iattr, 0);
  840. }
  841. return error;
  842. }
  843. STATIC int
  844. xfs_vn_update_time(
  845. struct inode *inode,
  846. struct timespec *now,
  847. int flags)
  848. {
  849. struct xfs_inode *ip = XFS_I(inode);
  850. struct xfs_mount *mp = ip->i_mount;
  851. struct xfs_trans *tp;
  852. int error;
  853. trace_xfs_update_time(ip);
  854. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  855. if (error)
  856. return error;
  857. xfs_ilock(ip, XFS_ILOCK_EXCL);
  858. if (flags & S_CTIME)
  859. inode->i_ctime = *now;
  860. if (flags & S_MTIME)
  861. inode->i_mtime = *now;
  862. if (flags & S_ATIME)
  863. inode->i_atime = *now;
  864. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  865. xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
  866. return xfs_trans_commit(tp);
  867. }
  868. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  869. /*
  870. * Call fiemap helper to fill in user data.
  871. * Returns positive errors to xfs_getbmap.
  872. */
  873. STATIC int
  874. xfs_fiemap_format(
  875. void **arg,
  876. struct getbmapx *bmv,
  877. int *full)
  878. {
  879. int error;
  880. struct fiemap_extent_info *fieinfo = *arg;
  881. u32 fiemap_flags = 0;
  882. u64 logical, physical, length;
  883. /* Do nothing for a hole */
  884. if (bmv->bmv_block == -1LL)
  885. return 0;
  886. logical = BBTOB(bmv->bmv_offset);
  887. physical = BBTOB(bmv->bmv_block);
  888. length = BBTOB(bmv->bmv_length);
  889. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  890. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  891. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  892. fiemap_flags |= (FIEMAP_EXTENT_DELALLOC |
  893. FIEMAP_EXTENT_UNKNOWN);
  894. physical = 0; /* no block yet */
  895. }
  896. if (bmv->bmv_oflags & BMV_OF_LAST)
  897. fiemap_flags |= FIEMAP_EXTENT_LAST;
  898. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  899. length, fiemap_flags);
  900. if (error > 0) {
  901. error = 0;
  902. *full = 1; /* user array now full */
  903. }
  904. return error;
  905. }
  906. STATIC int
  907. xfs_vn_fiemap(
  908. struct inode *inode,
  909. struct fiemap_extent_info *fieinfo,
  910. u64 start,
  911. u64 length)
  912. {
  913. xfs_inode_t *ip = XFS_I(inode);
  914. struct getbmapx bm;
  915. int error;
  916. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  917. if (error)
  918. return error;
  919. /* Set up bmap header for xfs internal routine */
  920. bm.bmv_offset = BTOBBT(start);
  921. /* Special case for whole file */
  922. if (length == FIEMAP_MAX_OFFSET)
  923. bm.bmv_length = -1LL;
  924. else
  925. bm.bmv_length = BTOBB(start + length) - bm.bmv_offset;
  926. /* We add one because in getbmap world count includes the header */
  927. bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
  928. fieinfo->fi_extents_max + 1;
  929. bm.bmv_count = min_t(__s32, bm.bmv_count,
  930. (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
  931. bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
  932. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  933. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  934. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  935. bm.bmv_iflags |= BMV_IF_DELALLOC;
  936. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  937. if (error)
  938. return error;
  939. return 0;
  940. }
  941. STATIC int
  942. xfs_vn_tmpfile(
  943. struct inode *dir,
  944. struct dentry *dentry,
  945. umode_t mode)
  946. {
  947. return xfs_generic_create(dir, dentry, mode, 0, true);
  948. }
  949. static const struct inode_operations xfs_inode_operations = {
  950. .get_acl = xfs_get_acl,
  951. .set_acl = xfs_set_acl,
  952. .getattr = xfs_vn_getattr,
  953. .setattr = xfs_vn_setattr,
  954. .setxattr = generic_setxattr,
  955. .getxattr = generic_getxattr,
  956. .removexattr = generic_removexattr,
  957. .listxattr = xfs_vn_listxattr,
  958. .fiemap = xfs_vn_fiemap,
  959. .update_time = xfs_vn_update_time,
  960. };
  961. static const struct inode_operations xfs_dir_inode_operations = {
  962. .create = xfs_vn_create,
  963. .lookup = xfs_vn_lookup,
  964. .link = xfs_vn_link,
  965. .unlink = xfs_vn_unlink,
  966. .symlink = xfs_vn_symlink,
  967. .mkdir = xfs_vn_mkdir,
  968. /*
  969. * Yes, XFS uses the same method for rmdir and unlink.
  970. *
  971. * There are some subtile differences deeper in the code,
  972. * but we use S_ISDIR to check for those.
  973. */
  974. .rmdir = xfs_vn_unlink,
  975. .mknod = xfs_vn_mknod,
  976. .rename2 = xfs_vn_rename,
  977. .get_acl = xfs_get_acl,
  978. .set_acl = xfs_set_acl,
  979. .getattr = xfs_vn_getattr,
  980. .setattr = xfs_vn_setattr,
  981. .setxattr = generic_setxattr,
  982. .getxattr = generic_getxattr,
  983. .removexattr = generic_removexattr,
  984. .listxattr = xfs_vn_listxattr,
  985. .update_time = xfs_vn_update_time,
  986. .tmpfile = xfs_vn_tmpfile,
  987. };
  988. static const struct inode_operations xfs_dir_ci_inode_operations = {
  989. .create = xfs_vn_create,
  990. .lookup = xfs_vn_ci_lookup,
  991. .link = xfs_vn_link,
  992. .unlink = xfs_vn_unlink,
  993. .symlink = xfs_vn_symlink,
  994. .mkdir = xfs_vn_mkdir,
  995. /*
  996. * Yes, XFS uses the same method for rmdir and unlink.
  997. *
  998. * There are some subtile differences deeper in the code,
  999. * but we use S_ISDIR to check for those.
  1000. */
  1001. .rmdir = xfs_vn_unlink,
  1002. .mknod = xfs_vn_mknod,
  1003. .rename2 = xfs_vn_rename,
  1004. .get_acl = xfs_get_acl,
  1005. .set_acl = xfs_set_acl,
  1006. .getattr = xfs_vn_getattr,
  1007. .setattr = xfs_vn_setattr,
  1008. .setxattr = generic_setxattr,
  1009. .getxattr = generic_getxattr,
  1010. .removexattr = generic_removexattr,
  1011. .listxattr = xfs_vn_listxattr,
  1012. .update_time = xfs_vn_update_time,
  1013. .tmpfile = xfs_vn_tmpfile,
  1014. };
  1015. static const struct inode_operations xfs_symlink_inode_operations = {
  1016. .readlink = generic_readlink,
  1017. .get_link = xfs_vn_get_link,
  1018. .getattr = xfs_vn_getattr,
  1019. .setattr = xfs_vn_setattr,
  1020. .setxattr = generic_setxattr,
  1021. .getxattr = generic_getxattr,
  1022. .removexattr = generic_removexattr,
  1023. .listxattr = xfs_vn_listxattr,
  1024. .update_time = xfs_vn_update_time,
  1025. };
  1026. static const struct inode_operations xfs_inline_symlink_inode_operations = {
  1027. .readlink = generic_readlink,
  1028. .get_link = xfs_vn_get_link_inline,
  1029. .getattr = xfs_vn_getattr,
  1030. .setattr = xfs_vn_setattr,
  1031. .setxattr = generic_setxattr,
  1032. .getxattr = generic_getxattr,
  1033. .removexattr = generic_removexattr,
  1034. .listxattr = xfs_vn_listxattr,
  1035. .update_time = xfs_vn_update_time,
  1036. };
  1037. STATIC void
  1038. xfs_diflags_to_iflags(
  1039. struct inode *inode,
  1040. struct xfs_inode *ip)
  1041. {
  1042. uint16_t flags = ip->i_d.di_flags;
  1043. inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC |
  1044. S_NOATIME | S_DAX);
  1045. if (flags & XFS_DIFLAG_IMMUTABLE)
  1046. inode->i_flags |= S_IMMUTABLE;
  1047. if (flags & XFS_DIFLAG_APPEND)
  1048. inode->i_flags |= S_APPEND;
  1049. if (flags & XFS_DIFLAG_SYNC)
  1050. inode->i_flags |= S_SYNC;
  1051. if (flags & XFS_DIFLAG_NOATIME)
  1052. inode->i_flags |= S_NOATIME;
  1053. if (S_ISREG(inode->i_mode) &&
  1054. ip->i_mount->m_sb.sb_blocksize == PAGE_SIZE &&
  1055. (ip->i_mount->m_flags & XFS_MOUNT_DAX ||
  1056. ip->i_d.di_flags2 & XFS_DIFLAG2_DAX))
  1057. inode->i_flags |= S_DAX;
  1058. }
  1059. /*
  1060. * Initialize the Linux inode.
  1061. *
  1062. * When reading existing inodes from disk this is called directly from xfs_iget,
  1063. * when creating a new inode it is called from xfs_ialloc after setting up the
  1064. * inode. These callers have different criteria for clearing XFS_INEW, so leave
  1065. * it up to the caller to deal with unlocking the inode appropriately.
  1066. */
  1067. void
  1068. xfs_setup_inode(
  1069. struct xfs_inode *ip)
  1070. {
  1071. struct inode *inode = &ip->i_vnode;
  1072. gfp_t gfp_mask;
  1073. inode->i_ino = ip->i_ino;
  1074. inode->i_state = I_NEW;
  1075. inode_sb_list_add(inode);
  1076. /* make the inode look hashed for the writeback code */
  1077. hlist_add_fake(&inode->i_hash);
  1078. inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
  1079. inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
  1080. switch (inode->i_mode & S_IFMT) {
  1081. case S_IFBLK:
  1082. case S_IFCHR:
  1083. inode->i_rdev =
  1084. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  1085. sysv_minor(ip->i_df.if_u2.if_rdev));
  1086. break;
  1087. default:
  1088. inode->i_rdev = 0;
  1089. break;
  1090. }
  1091. i_size_write(inode, ip->i_d.di_size);
  1092. xfs_diflags_to_iflags(inode, ip);
  1093. if (S_ISDIR(inode->i_mode)) {
  1094. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
  1095. ip->d_ops = ip->i_mount->m_dir_inode_ops;
  1096. } else {
  1097. ip->d_ops = ip->i_mount->m_nondir_inode_ops;
  1098. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
  1099. }
  1100. /*
  1101. * Ensure all page cache allocations are done from GFP_NOFS context to
  1102. * prevent direct reclaim recursion back into the filesystem and blowing
  1103. * stacks or deadlocking.
  1104. */
  1105. gfp_mask = mapping_gfp_mask(inode->i_mapping);
  1106. mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
  1107. /*
  1108. * If there is no attribute fork no ACL can exist on this inode,
  1109. * and it can't have any file capabilities attached to it either.
  1110. */
  1111. if (!XFS_IFORK_Q(ip)) {
  1112. inode_has_no_xattr(inode);
  1113. cache_no_acl(inode);
  1114. }
  1115. }
  1116. void
  1117. xfs_setup_iops(
  1118. struct xfs_inode *ip)
  1119. {
  1120. struct inode *inode = &ip->i_vnode;
  1121. switch (inode->i_mode & S_IFMT) {
  1122. case S_IFREG:
  1123. inode->i_op = &xfs_inode_operations;
  1124. inode->i_fop = &xfs_file_operations;
  1125. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1126. break;
  1127. case S_IFDIR:
  1128. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  1129. inode->i_op = &xfs_dir_ci_inode_operations;
  1130. else
  1131. inode->i_op = &xfs_dir_inode_operations;
  1132. inode->i_fop = &xfs_dir_file_operations;
  1133. break;
  1134. case S_IFLNK:
  1135. if (ip->i_df.if_flags & XFS_IFINLINE)
  1136. inode->i_op = &xfs_inline_symlink_inode_operations;
  1137. else
  1138. inode->i_op = &xfs_symlink_inode_operations;
  1139. break;
  1140. default:
  1141. inode->i_op = &xfs_inode_operations;
  1142. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1143. break;
  1144. }
  1145. }