xfs_iops.c 33 KB

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