xfs_iops.c 34 KB

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