inode.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/completion.h>
  9. #include <linux/buffer_head.h>
  10. #include <linux/namei.h>
  11. #include <linux/mm.h>
  12. #include <linux/cred.h>
  13. #include <linux/xattr.h>
  14. #include <linux/posix_acl.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <linux/crc32.h>
  17. #include <linux/iomap.h>
  18. #include <linux/security.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/uaccess.h>
  21. #include "gfs2.h"
  22. #include "incore.h"
  23. #include "acl.h"
  24. #include "bmap.h"
  25. #include "dir.h"
  26. #include "xattr.h"
  27. #include "glock.h"
  28. #include "inode.h"
  29. #include "meta_io.h"
  30. #include "quota.h"
  31. #include "rgrp.h"
  32. #include "trans.h"
  33. #include "util.h"
  34. #include "super.h"
  35. #include "glops.h"
  36. static const struct inode_operations gfs2_file_iops;
  37. static const struct inode_operations gfs2_dir_iops;
  38. static const struct inode_operations gfs2_symlink_iops;
  39. /**
  40. * gfs2_set_iop - Sets inode operations
  41. * @inode: The inode with correct i_mode filled in
  42. *
  43. * GFS2 lookup code fills in vfs inode contents based on info obtained
  44. * from directory entry inside gfs2_inode_lookup().
  45. */
  46. static void gfs2_set_iop(struct inode *inode)
  47. {
  48. struct gfs2_sbd *sdp = GFS2_SB(inode);
  49. umode_t mode = inode->i_mode;
  50. if (S_ISREG(mode)) {
  51. inode->i_op = &gfs2_file_iops;
  52. if (gfs2_localflocks(sdp))
  53. inode->i_fop = &gfs2_file_fops_nolock;
  54. else
  55. inode->i_fop = &gfs2_file_fops;
  56. } else if (S_ISDIR(mode)) {
  57. inode->i_op = &gfs2_dir_iops;
  58. if (gfs2_localflocks(sdp))
  59. inode->i_fop = &gfs2_dir_fops_nolock;
  60. else
  61. inode->i_fop = &gfs2_dir_fops;
  62. } else if (S_ISLNK(mode)) {
  63. inode->i_op = &gfs2_symlink_iops;
  64. } else {
  65. inode->i_op = &gfs2_file_iops;
  66. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  67. }
  68. }
  69. static int iget_test(struct inode *inode, void *opaque)
  70. {
  71. u64 no_addr = *(u64 *)opaque;
  72. return GFS2_I(inode)->i_no_addr == no_addr;
  73. }
  74. static int iget_set(struct inode *inode, void *opaque)
  75. {
  76. u64 no_addr = *(u64 *)opaque;
  77. GFS2_I(inode)->i_no_addr = no_addr;
  78. inode->i_ino = no_addr;
  79. return 0;
  80. }
  81. /**
  82. * gfs2_inode_lookup - Lookup an inode
  83. * @sb: The super block
  84. * @type: The type of the inode
  85. * @no_addr: The inode number
  86. * @no_formal_ino: The inode generation number
  87. * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
  88. * GFS2_BLKST_FREE to indicate not to verify)
  89. *
  90. * If @type is DT_UNKNOWN, the inode type is fetched from disk.
  91. *
  92. * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
  93. * placeholder because it doesn't otherwise make sense), the on-disk block type
  94. * is verified to be @blktype.
  95. *
  96. * When @no_formal_ino is non-zero, this function will return ERR_PTR(-ESTALE)
  97. * if it detects that @no_formal_ino doesn't match the actual inode generation
  98. * number. However, it doesn't always know unless @type is DT_UNKNOWN.
  99. *
  100. * Returns: A VFS inode, or an error
  101. */
  102. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  103. u64 no_addr, u64 no_formal_ino,
  104. unsigned int blktype)
  105. {
  106. struct inode *inode;
  107. struct gfs2_inode *ip;
  108. struct gfs2_holder i_gh;
  109. int error;
  110. gfs2_holder_mark_uninitialized(&i_gh);
  111. inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
  112. if (!inode)
  113. return ERR_PTR(-ENOMEM);
  114. ip = GFS2_I(inode);
  115. if (inode->i_state & I_NEW) {
  116. struct gfs2_sbd *sdp = GFS2_SB(inode);
  117. struct gfs2_glock *io_gl;
  118. int extra_flags = 0;
  119. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE,
  120. &ip->i_gl);
  121. if (unlikely(error))
  122. goto fail;
  123. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE,
  124. &io_gl);
  125. if (unlikely(error))
  126. goto fail;
  127. if (blktype == GFS2_BLKST_UNLINKED)
  128. extra_flags |= LM_FLAG_TRY;
  129. else
  130. gfs2_cancel_delete_work(io_gl);
  131. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED,
  132. GL_EXACT | GL_NOPID | extra_flags,
  133. &ip->i_iopen_gh);
  134. gfs2_glock_put(io_gl);
  135. if (unlikely(error))
  136. goto fail;
  137. if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
  138. /*
  139. * The GL_SKIP flag indicates to skip reading the inode
  140. * block. We read the inode when instantiating it
  141. * after possibly checking the block type.
  142. */
  143. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
  144. GL_SKIP, &i_gh);
  145. if (error)
  146. goto fail;
  147. error = -ESTALE;
  148. if (no_formal_ino &&
  149. gfs2_inode_already_deleted(ip->i_gl, no_formal_ino))
  150. goto fail;
  151. if (blktype != GFS2_BLKST_FREE) {
  152. error = gfs2_check_blk_type(sdp, no_addr,
  153. blktype);
  154. if (error)
  155. goto fail;
  156. }
  157. }
  158. set_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags);
  159. /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
  160. inode->i_atime.tv_sec = 1LL << (8 * sizeof(inode->i_atime.tv_sec) - 1);
  161. inode->i_atime.tv_nsec = 0;
  162. glock_set_object(ip->i_gl, ip);
  163. if (type == DT_UNKNOWN) {
  164. /* Inode glock must be locked already */
  165. error = gfs2_instantiate(&i_gh);
  166. if (error) {
  167. glock_clear_object(ip->i_gl, ip);
  168. goto fail;
  169. }
  170. } else {
  171. ip->i_no_formal_ino = no_formal_ino;
  172. inode->i_mode = DT2IF(type);
  173. }
  174. if (gfs2_holder_initialized(&i_gh))
  175. gfs2_glock_dq_uninit(&i_gh);
  176. glock_set_object(ip->i_iopen_gh.gh_gl, ip);
  177. gfs2_set_iop(inode);
  178. unlock_new_inode(inode);
  179. }
  180. if (no_formal_ino && ip->i_no_formal_ino &&
  181. no_formal_ino != ip->i_no_formal_ino) {
  182. iput(inode);
  183. return ERR_PTR(-ESTALE);
  184. }
  185. return inode;
  186. fail:
  187. if (error == GLR_TRYFAILED)
  188. error = -EAGAIN;
  189. if (gfs2_holder_initialized(&ip->i_iopen_gh))
  190. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  191. if (gfs2_holder_initialized(&i_gh))
  192. gfs2_glock_dq_uninit(&i_gh);
  193. iget_failed(inode);
  194. return ERR_PTR(error);
  195. }
  196. /**
  197. * gfs2_lookup_by_inum - look up an inode by inode number
  198. * @sdp: The super block
  199. * @no_addr: The inode number
  200. * @no_formal_ino: The inode generation number (0 for any)
  201. * @blktype: Requested block type (see gfs2_inode_lookup)
  202. */
  203. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  204. u64 no_formal_ino, unsigned int blktype)
  205. {
  206. struct super_block *sb = sdp->sd_vfs;
  207. struct inode *inode;
  208. int error;
  209. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino,
  210. blktype);
  211. if (IS_ERR(inode))
  212. return inode;
  213. if (no_formal_ino) {
  214. error = -EIO;
  215. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  216. goto fail_iput;
  217. }
  218. return inode;
  219. fail_iput:
  220. iput(inode);
  221. return ERR_PTR(error);
  222. }
  223. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  224. {
  225. struct qstr qstr;
  226. struct inode *inode;
  227. gfs2_str2qstr(&qstr, name);
  228. inode = gfs2_lookupi(dip, &qstr, 1);
  229. /* gfs2_lookupi has inconsistent callers: vfs
  230. * related routines expect NULL for no entry found,
  231. * gfs2_lookup_simple callers expect ENOENT
  232. * and do not check for NULL.
  233. */
  234. if (inode == NULL)
  235. return ERR_PTR(-ENOENT);
  236. else
  237. return inode;
  238. }
  239. /**
  240. * gfs2_lookupi - Look up a filename in a directory and return its inode
  241. * @dir: The inode of the directory containing the inode to look-up
  242. * @name: The name of the inode to look for
  243. * @is_root: If 1, ignore the caller's permissions
  244. *
  245. * This can be called via the VFS filldir function when NFS is doing
  246. * a readdirplus and the inode which its intending to stat isn't
  247. * already in cache. In this case we must not take the directory glock
  248. * again, since the readdir call will have already taken that lock.
  249. *
  250. * Returns: errno
  251. */
  252. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  253. int is_root)
  254. {
  255. struct super_block *sb = dir->i_sb;
  256. struct gfs2_inode *dip = GFS2_I(dir);
  257. struct gfs2_holder d_gh;
  258. int error = 0;
  259. struct inode *inode = NULL;
  260. gfs2_holder_mark_uninitialized(&d_gh);
  261. if (!name->len || name->len > GFS2_FNAMESIZE)
  262. return ERR_PTR(-ENAMETOOLONG);
  263. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  264. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  265. dir == d_inode(sb->s_root))) {
  266. igrab(dir);
  267. return dir;
  268. }
  269. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  270. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  271. if (error)
  272. return ERR_PTR(error);
  273. }
  274. if (!is_root) {
  275. error = gfs2_permission(&init_user_ns, dir, MAY_EXEC);
  276. if (error)
  277. goto out;
  278. }
  279. inode = gfs2_dir_search(dir, name, false);
  280. if (IS_ERR(inode))
  281. error = PTR_ERR(inode);
  282. out:
  283. if (gfs2_holder_initialized(&d_gh))
  284. gfs2_glock_dq_uninit(&d_gh);
  285. if (error == -ENOENT)
  286. return NULL;
  287. return inode ? inode : ERR_PTR(error);
  288. }
  289. /**
  290. * create_ok - OK to create a new on-disk inode here?
  291. * @dip: Directory in which dinode is to be created
  292. * @name: Name of new dinode
  293. * @mode:
  294. *
  295. * Returns: errno
  296. */
  297. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  298. umode_t mode)
  299. {
  300. int error;
  301. error = gfs2_permission(&init_user_ns, &dip->i_inode,
  302. MAY_WRITE | MAY_EXEC);
  303. if (error)
  304. return error;
  305. /* Don't create entries in an unlinked directory */
  306. if (!dip->i_inode.i_nlink)
  307. return -ENOENT;
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  315. struct inode *inode)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) &&
  319. !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
  320. if (S_ISDIR(inode->i_mode))
  321. inode->i_mode |= S_ISUID;
  322. else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
  323. inode->i_mode &= ~07111;
  324. inode->i_uid = dip->i_inode.i_uid;
  325. } else
  326. inode->i_uid = current_fsuid();
  327. if (dip->i_inode.i_mode & S_ISGID) {
  328. if (S_ISDIR(inode->i_mode))
  329. inode->i_mode |= S_ISGID;
  330. inode->i_gid = dip->i_inode.i_gid;
  331. } else
  332. inode->i_gid = current_fsgid();
  333. }
  334. static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
  335. {
  336. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  337. struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
  338. int error;
  339. error = gfs2_quota_lock_check(ip, &ap);
  340. if (error)
  341. goto out;
  342. error = gfs2_inplace_reserve(ip, &ap);
  343. if (error)
  344. goto out_quota;
  345. error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
  346. if (error)
  347. goto out_ipreserv;
  348. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
  349. ip->i_no_formal_ino = ip->i_generation;
  350. ip->i_inode.i_ino = ip->i_no_addr;
  351. ip->i_goal = ip->i_no_addr;
  352. gfs2_trans_end(sdp);
  353. out_ipreserv:
  354. gfs2_inplace_release(ip);
  355. out_quota:
  356. gfs2_quota_unlock(ip);
  357. out:
  358. return error;
  359. }
  360. static void gfs2_init_dir(struct buffer_head *dibh,
  361. const struct gfs2_inode *parent)
  362. {
  363. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  364. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  365. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  366. dent->de_inum = di->di_num; /* already GFS2 endian */
  367. dent->de_type = cpu_to_be16(DT_DIR);
  368. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  369. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  370. gfs2_inum_out(parent, dent);
  371. dent->de_type = cpu_to_be16(DT_DIR);
  372. }
  373. /**
  374. * gfs2_init_xattr - Initialise an xattr block for a new inode
  375. * @ip: The inode in question
  376. *
  377. * This sets up an empty xattr block for a new inode, ready to
  378. * take any ACLs, LSM xattrs, etc.
  379. */
  380. static void gfs2_init_xattr(struct gfs2_inode *ip)
  381. {
  382. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  383. struct buffer_head *bh;
  384. struct gfs2_ea_header *ea;
  385. bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
  386. gfs2_trans_add_meta(ip->i_gl, bh);
  387. gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  388. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  389. ea = GFS2_EA_BH2FIRST(bh);
  390. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  391. ea->ea_type = GFS2_EATYPE_UNUSED;
  392. ea->ea_flags = GFS2_EAFLAG_LAST;
  393. brelse(bh);
  394. }
  395. /**
  396. * init_dinode - Fill in a new dinode structure
  397. * @dip: The directory this inode is being created in
  398. * @ip: The inode
  399. * @symname: The symlink destination (if a symlink)
  400. *
  401. */
  402. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  403. const char *symname)
  404. {
  405. struct gfs2_dinode *di;
  406. struct buffer_head *dibh;
  407. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  408. gfs2_trans_add_meta(ip->i_gl, dibh);
  409. di = (struct gfs2_dinode *)dibh->b_data;
  410. gfs2_dinode_out(ip, di);
  411. di->di_major = cpu_to_be32(imajor(&ip->i_inode));
  412. di->di_minor = cpu_to_be32(iminor(&ip->i_inode));
  413. di->__pad1 = 0;
  414. di->__pad2 = 0;
  415. di->__pad3 = 0;
  416. memset(&di->__pad4, 0, sizeof(di->__pad4));
  417. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  418. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  419. switch(ip->i_inode.i_mode & S_IFMT) {
  420. case S_IFDIR:
  421. gfs2_init_dir(dibh, dip);
  422. break;
  423. case S_IFLNK:
  424. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  425. break;
  426. }
  427. set_buffer_uptodate(dibh);
  428. brelse(dibh);
  429. }
  430. /**
  431. * gfs2_trans_da_blks - Calculate number of blocks to link inode
  432. * @dip: The directory we are linking into
  433. * @da: The dir add information
  434. * @nr_inodes: The number of inodes involved
  435. *
  436. * This calculate the number of blocks we need to reserve in a
  437. * transaction to link @nr_inodes into a directory. In most cases
  438. * @nr_inodes will be 2 (the directory plus the inode being linked in)
  439. * but in case of rename, 4 may be required.
  440. *
  441. * Returns: Number of blocks
  442. */
  443. static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
  444. const struct gfs2_diradd *da,
  445. unsigned nr_inodes)
  446. {
  447. return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
  448. (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
  449. }
  450. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  451. struct gfs2_inode *ip, struct gfs2_diradd *da)
  452. {
  453. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  454. struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
  455. int error;
  456. if (da->nr_blocks) {
  457. error = gfs2_quota_lock_check(dip, &ap);
  458. if (error)
  459. goto fail_quota_locks;
  460. error = gfs2_inplace_reserve(dip, &ap);
  461. if (error)
  462. goto fail_quota_locks;
  463. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
  464. if (error)
  465. goto fail_ipreserv;
  466. } else {
  467. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  468. if (error)
  469. goto fail_quota_locks;
  470. }
  471. error = gfs2_dir_add(&dip->i_inode, name, ip, da);
  472. gfs2_trans_end(sdp);
  473. fail_ipreserv:
  474. gfs2_inplace_release(dip);
  475. fail_quota_locks:
  476. gfs2_quota_unlock(dip);
  477. return error;
  478. }
  479. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  480. void *fs_info)
  481. {
  482. const struct xattr *xattr;
  483. int err = 0;
  484. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  485. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  486. xattr->value_len, 0,
  487. GFS2_EATYPE_SECURITY);
  488. if (err < 0)
  489. break;
  490. }
  491. return err;
  492. }
  493. /**
  494. * gfs2_create_inode - Create a new inode
  495. * @dir: The parent directory
  496. * @dentry: The new dentry
  497. * @file: If non-NULL, the file which is being opened
  498. * @mode: The permissions on the new inode
  499. * @dev: For device nodes, this is the device number
  500. * @symname: For symlinks, this is the link destination
  501. * @size: The initial size of the inode (ignored for directories)
  502. * @excl: Force fail if inode exists
  503. *
  504. * Returns: 0 on success, or error code
  505. */
  506. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  507. struct file *file,
  508. umode_t mode, dev_t dev, const char *symname,
  509. unsigned int size, int excl)
  510. {
  511. const struct qstr *name = &dentry->d_name;
  512. struct posix_acl *default_acl, *acl;
  513. struct gfs2_holder ghs[2];
  514. struct inode *inode = NULL;
  515. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  516. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  517. struct gfs2_glock *io_gl;
  518. int error, free_vfs_inode = 1;
  519. u32 aflags = 0;
  520. unsigned blocks = 1;
  521. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  522. if (!name->len || name->len > GFS2_FNAMESIZE)
  523. return -ENAMETOOLONG;
  524. error = gfs2_qa_get(dip);
  525. if (error)
  526. return error;
  527. error = gfs2_rindex_update(sdp);
  528. if (error)
  529. goto fail;
  530. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  531. if (error)
  532. goto fail;
  533. gfs2_holder_mark_uninitialized(ghs + 1);
  534. error = create_ok(dip, name, mode);
  535. if (error)
  536. goto fail_gunlock;
  537. inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
  538. error = PTR_ERR(inode);
  539. if (!IS_ERR(inode)) {
  540. if (S_ISDIR(inode->i_mode)) {
  541. iput(inode);
  542. inode = ERR_PTR(-EISDIR);
  543. goto fail_gunlock;
  544. }
  545. d_instantiate(dentry, inode);
  546. error = 0;
  547. if (file) {
  548. if (S_ISREG(inode->i_mode))
  549. error = finish_open(file, dentry, gfs2_open_common);
  550. else
  551. error = finish_no_open(file, NULL);
  552. }
  553. gfs2_glock_dq_uninit(ghs);
  554. goto fail;
  555. } else if (error != -ENOENT) {
  556. goto fail_gunlock;
  557. }
  558. error = gfs2_diradd_alloc_required(dir, name, &da);
  559. if (error < 0)
  560. goto fail_gunlock;
  561. inode = new_inode(sdp->sd_vfs);
  562. error = -ENOMEM;
  563. if (!inode)
  564. goto fail_gunlock;
  565. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  566. if (error)
  567. goto fail_gunlock;
  568. ip = GFS2_I(inode);
  569. error = gfs2_qa_get(ip);
  570. if (error)
  571. goto fail_free_acls;
  572. inode->i_mode = mode;
  573. set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
  574. inode->i_rdev = dev;
  575. inode->i_size = size;
  576. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  577. munge_mode_uid_gid(dip, inode);
  578. check_and_update_goal(dip);
  579. ip->i_goal = dip->i_goal;
  580. ip->i_diskflags = 0;
  581. ip->i_eattr = 0;
  582. ip->i_height = 0;
  583. ip->i_depth = 0;
  584. ip->i_entries = 0;
  585. ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
  586. switch(mode & S_IFMT) {
  587. case S_IFREG:
  588. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  589. gfs2_tune_get(sdp, gt_new_files_jdata))
  590. ip->i_diskflags |= GFS2_DIF_JDATA;
  591. gfs2_set_aops(inode);
  592. break;
  593. case S_IFDIR:
  594. ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
  595. ip->i_diskflags |= GFS2_DIF_JDATA;
  596. ip->i_entries = 2;
  597. break;
  598. }
  599. /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
  600. if (dip->i_diskflags & GFS2_DIF_SYSTEM)
  601. ip->i_diskflags |= GFS2_DIF_SYSTEM;
  602. gfs2_set_inode_flags(inode);
  603. if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
  604. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  605. aflags |= GFS2_AF_ORLOV;
  606. if (default_acl || acl)
  607. blocks++;
  608. error = alloc_dinode(ip, aflags, &blocks);
  609. if (error)
  610. goto fail_free_inode;
  611. gfs2_set_inode_blocks(inode, blocks);
  612. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  613. if (error)
  614. goto fail_free_inode;
  615. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  616. if (error)
  617. goto fail_free_inode;
  618. gfs2_cancel_delete_work(io_gl);
  619. error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr);
  620. BUG_ON(error);
  621. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT | GL_NOPID,
  622. &ip->i_iopen_gh);
  623. if (error)
  624. goto fail_gunlock2;
  625. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  626. if (error)
  627. goto fail_gunlock3;
  628. error = gfs2_trans_begin(sdp, blocks, 0);
  629. if (error)
  630. goto fail_gunlock3;
  631. if (blocks > 1) {
  632. ip->i_eattr = ip->i_no_addr + 1;
  633. gfs2_init_xattr(ip);
  634. }
  635. init_dinode(dip, ip, symname);
  636. gfs2_trans_end(sdp);
  637. glock_set_object(ip->i_gl, ip);
  638. glock_set_object(io_gl, ip);
  639. gfs2_set_iop(inode);
  640. free_vfs_inode = 0; /* After this point, the inode is no longer
  641. considered free. Any failures need to undo
  642. the gfs2 structures. */
  643. if (default_acl) {
  644. error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  645. if (error)
  646. goto fail_gunlock4;
  647. posix_acl_release(default_acl);
  648. default_acl = NULL;
  649. }
  650. if (acl) {
  651. error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
  652. if (error)
  653. goto fail_gunlock4;
  654. posix_acl_release(acl);
  655. acl = NULL;
  656. }
  657. error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
  658. &gfs2_initxattrs, NULL);
  659. if (error)
  660. goto fail_gunlock4;
  661. error = link_dinode(dip, name, ip, &da);
  662. if (error)
  663. goto fail_gunlock4;
  664. mark_inode_dirty(inode);
  665. d_instantiate(dentry, inode);
  666. /* After instantiate, errors should result in evict which will destroy
  667. * both inode and iopen glocks properly. */
  668. if (file) {
  669. file->f_mode |= FMODE_CREATED;
  670. error = finish_open(file, dentry, gfs2_open_common);
  671. }
  672. gfs2_glock_dq_uninit(ghs);
  673. gfs2_qa_put(ip);
  674. gfs2_glock_dq_uninit(ghs + 1);
  675. gfs2_glock_put(io_gl);
  676. gfs2_qa_put(dip);
  677. unlock_new_inode(inode);
  678. return error;
  679. fail_gunlock4:
  680. glock_clear_object(ip->i_gl, ip);
  681. glock_clear_object(io_gl, ip);
  682. fail_gunlock3:
  683. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  684. fail_gunlock2:
  685. gfs2_glock_put(io_gl);
  686. fail_free_inode:
  687. if (ip->i_gl) {
  688. if (free_vfs_inode) /* else evict will do the put for us */
  689. gfs2_glock_put(ip->i_gl);
  690. }
  691. gfs2_rs_deltree(&ip->i_res);
  692. gfs2_qa_put(ip);
  693. fail_free_acls:
  694. posix_acl_release(default_acl);
  695. posix_acl_release(acl);
  696. fail_gunlock:
  697. gfs2_dir_no_add(&da);
  698. gfs2_glock_dq_uninit(ghs);
  699. if (!IS_ERR_OR_NULL(inode)) {
  700. clear_nlink(inode);
  701. if (!free_vfs_inode)
  702. mark_inode_dirty(inode);
  703. set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
  704. &GFS2_I(inode)->i_flags);
  705. if (inode->i_state & I_NEW)
  706. iget_failed(inode);
  707. else
  708. iput(inode);
  709. }
  710. if (gfs2_holder_initialized(ghs + 1))
  711. gfs2_glock_dq_uninit(ghs + 1);
  712. fail:
  713. gfs2_qa_put(dip);
  714. return error;
  715. }
  716. /**
  717. * gfs2_create - Create a file
  718. * @mnt_userns: User namespace of the mount the inode was found from
  719. * @dir: The directory in which to create the file
  720. * @dentry: The dentry of the new file
  721. * @mode: The mode of the new file
  722. * @excl: Force fail if inode exists
  723. *
  724. * Returns: errno
  725. */
  726. static int gfs2_create(struct user_namespace *mnt_userns, struct inode *dir,
  727. struct dentry *dentry, umode_t mode, bool excl)
  728. {
  729. return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl);
  730. }
  731. /**
  732. * __gfs2_lookup - Look up a filename in a directory and return its inode
  733. * @dir: The directory inode
  734. * @dentry: The dentry of the new inode
  735. * @file: File to be opened
  736. *
  737. *
  738. * Returns: errno
  739. */
  740. static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
  741. struct file *file)
  742. {
  743. struct inode *inode;
  744. struct dentry *d;
  745. struct gfs2_holder gh;
  746. struct gfs2_glock *gl;
  747. int error;
  748. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  749. if (inode == NULL) {
  750. d_add(dentry, NULL);
  751. return NULL;
  752. }
  753. if (IS_ERR(inode))
  754. return ERR_CAST(inode);
  755. gl = GFS2_I(inode)->i_gl;
  756. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  757. if (error) {
  758. iput(inode);
  759. return ERR_PTR(error);
  760. }
  761. d = d_splice_alias(inode, dentry);
  762. if (IS_ERR(d)) {
  763. gfs2_glock_dq_uninit(&gh);
  764. return d;
  765. }
  766. if (file && S_ISREG(inode->i_mode))
  767. error = finish_open(file, dentry, gfs2_open_common);
  768. gfs2_glock_dq_uninit(&gh);
  769. if (error) {
  770. dput(d);
  771. return ERR_PTR(error);
  772. }
  773. return d;
  774. }
  775. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  776. unsigned flags)
  777. {
  778. return __gfs2_lookup(dir, dentry, NULL);
  779. }
  780. /**
  781. * gfs2_link - Link to a file
  782. * @old_dentry: The inode to link
  783. * @dir: Add link to this directory
  784. * @dentry: The name of the link
  785. *
  786. * Link the inode in "old_dentry" into the directory "dir" with the
  787. * name in "dentry".
  788. *
  789. * Returns: errno
  790. */
  791. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  792. struct dentry *dentry)
  793. {
  794. struct gfs2_inode *dip = GFS2_I(dir);
  795. struct gfs2_sbd *sdp = GFS2_SB(dir);
  796. struct inode *inode = d_inode(old_dentry);
  797. struct gfs2_inode *ip = GFS2_I(inode);
  798. struct gfs2_holder ghs[2];
  799. struct buffer_head *dibh;
  800. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  801. int error;
  802. if (S_ISDIR(inode->i_mode))
  803. return -EPERM;
  804. error = gfs2_qa_get(dip);
  805. if (error)
  806. return error;
  807. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  808. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  809. error = gfs2_glock_nq(ghs); /* parent */
  810. if (error)
  811. goto out_parent;
  812. error = gfs2_glock_nq(ghs + 1); /* child */
  813. if (error)
  814. goto out_child;
  815. error = -ENOENT;
  816. if (inode->i_nlink == 0)
  817. goto out_gunlock;
  818. error = gfs2_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
  819. if (error)
  820. goto out_gunlock;
  821. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  822. switch (error) {
  823. case -ENOENT:
  824. break;
  825. case 0:
  826. error = -EEXIST;
  827. goto out_gunlock;
  828. default:
  829. goto out_gunlock;
  830. }
  831. error = -EINVAL;
  832. if (!dip->i_inode.i_nlink)
  833. goto out_gunlock;
  834. error = -EFBIG;
  835. if (dip->i_entries == (u32)-1)
  836. goto out_gunlock;
  837. error = -EPERM;
  838. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  839. goto out_gunlock;
  840. error = -EINVAL;
  841. if (!ip->i_inode.i_nlink)
  842. goto out_gunlock;
  843. error = -EMLINK;
  844. if (ip->i_inode.i_nlink == (u32)-1)
  845. goto out_gunlock;
  846. error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
  847. if (error < 0)
  848. goto out_gunlock;
  849. if (da.nr_blocks) {
  850. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  851. error = gfs2_quota_lock_check(dip, &ap);
  852. if (error)
  853. goto out_gunlock;
  854. error = gfs2_inplace_reserve(dip, &ap);
  855. if (error)
  856. goto out_gunlock_q;
  857. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
  858. if (error)
  859. goto out_ipres;
  860. } else {
  861. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  862. if (error)
  863. goto out_ipres;
  864. }
  865. error = gfs2_meta_inode_buffer(ip, &dibh);
  866. if (error)
  867. goto out_end_trans;
  868. error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
  869. if (error)
  870. goto out_brelse;
  871. gfs2_trans_add_meta(ip->i_gl, dibh);
  872. inc_nlink(&ip->i_inode);
  873. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  874. ihold(inode);
  875. d_instantiate(dentry, inode);
  876. mark_inode_dirty(inode);
  877. out_brelse:
  878. brelse(dibh);
  879. out_end_trans:
  880. gfs2_trans_end(sdp);
  881. out_ipres:
  882. if (da.nr_blocks)
  883. gfs2_inplace_release(dip);
  884. out_gunlock_q:
  885. if (da.nr_blocks)
  886. gfs2_quota_unlock(dip);
  887. out_gunlock:
  888. gfs2_dir_no_add(&da);
  889. gfs2_glock_dq(ghs + 1);
  890. out_child:
  891. gfs2_glock_dq(ghs);
  892. out_parent:
  893. gfs2_qa_put(dip);
  894. gfs2_holder_uninit(ghs);
  895. gfs2_holder_uninit(ghs + 1);
  896. return error;
  897. }
  898. /*
  899. * gfs2_unlink_ok - check to see that a inode is still in a directory
  900. * @dip: the directory
  901. * @name: the name of the file
  902. * @ip: the inode
  903. *
  904. * Assumes that the lock on (at least) @dip is held.
  905. *
  906. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  907. */
  908. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  909. const struct gfs2_inode *ip)
  910. {
  911. int error;
  912. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  913. return -EPERM;
  914. if ((dip->i_inode.i_mode & S_ISVTX) &&
  915. !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
  916. !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
  917. return -EPERM;
  918. if (IS_APPEND(&dip->i_inode))
  919. return -EPERM;
  920. error = gfs2_permission(&init_user_ns, &dip->i_inode,
  921. MAY_WRITE | MAY_EXEC);
  922. if (error)
  923. return error;
  924. return gfs2_dir_check(&dip->i_inode, name, ip);
  925. }
  926. /**
  927. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  928. * @dip: The parent directory
  929. * @dentry: The dentry to unlink
  930. *
  931. * Called with all the locks and in a transaction. This will only be
  932. * called for a directory after it has been checked to ensure it is empty.
  933. *
  934. * Returns: 0 on success, or an error
  935. */
  936. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  937. const struct dentry *dentry)
  938. {
  939. struct inode *inode = d_inode(dentry);
  940. struct gfs2_inode *ip = GFS2_I(inode);
  941. int error;
  942. error = gfs2_dir_del(dip, dentry);
  943. if (error)
  944. return error;
  945. ip->i_entries = 0;
  946. inode->i_ctime = current_time(inode);
  947. if (S_ISDIR(inode->i_mode))
  948. clear_nlink(inode);
  949. else
  950. drop_nlink(inode);
  951. mark_inode_dirty(inode);
  952. if (inode->i_nlink == 0)
  953. gfs2_unlink_di(inode);
  954. return 0;
  955. }
  956. /**
  957. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  958. * @dir: The inode of the directory containing the inode to unlink
  959. * @dentry: The file itself
  960. *
  961. * This routine uses the type of the inode as a flag to figure out
  962. * whether this is an unlink or an rmdir.
  963. *
  964. * Returns: errno
  965. */
  966. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  967. {
  968. struct gfs2_inode *dip = GFS2_I(dir);
  969. struct gfs2_sbd *sdp = GFS2_SB(dir);
  970. struct inode *inode = d_inode(dentry);
  971. struct gfs2_inode *ip = GFS2_I(inode);
  972. struct gfs2_holder ghs[3];
  973. struct gfs2_rgrpd *rgd;
  974. int error;
  975. error = gfs2_rindex_update(sdp);
  976. if (error)
  977. return error;
  978. error = -EROFS;
  979. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  980. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  981. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  982. if (!rgd)
  983. goto out_inodes;
  984. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, ghs + 2);
  985. error = gfs2_glock_nq(ghs); /* parent */
  986. if (error)
  987. goto out_parent;
  988. error = gfs2_glock_nq(ghs + 1); /* child */
  989. if (error)
  990. goto out_child;
  991. error = -ENOENT;
  992. if (inode->i_nlink == 0)
  993. goto out_rgrp;
  994. if (S_ISDIR(inode->i_mode)) {
  995. error = -ENOTEMPTY;
  996. if (ip->i_entries > 2 || inode->i_nlink > 2)
  997. goto out_rgrp;
  998. }
  999. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  1000. if (error)
  1001. goto out_rgrp;
  1002. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  1003. if (error)
  1004. goto out_gunlock;
  1005. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  1006. if (error)
  1007. goto out_gunlock;
  1008. error = gfs2_unlink_inode(dip, dentry);
  1009. gfs2_trans_end(sdp);
  1010. out_gunlock:
  1011. gfs2_glock_dq(ghs + 2);
  1012. out_rgrp:
  1013. gfs2_glock_dq(ghs + 1);
  1014. out_child:
  1015. gfs2_glock_dq(ghs);
  1016. out_parent:
  1017. gfs2_holder_uninit(ghs + 2);
  1018. out_inodes:
  1019. gfs2_holder_uninit(ghs + 1);
  1020. gfs2_holder_uninit(ghs);
  1021. return error;
  1022. }
  1023. /**
  1024. * gfs2_symlink - Create a symlink
  1025. * @mnt_userns: User namespace of the mount the inode was found from
  1026. * @dir: The directory to create the symlink in
  1027. * @dentry: The dentry to put the symlink in
  1028. * @symname: The thing which the link points to
  1029. *
  1030. * Returns: errno
  1031. */
  1032. static int gfs2_symlink(struct user_namespace *mnt_userns, struct inode *dir,
  1033. struct dentry *dentry, const char *symname)
  1034. {
  1035. unsigned int size;
  1036. size = strlen(symname);
  1037. if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
  1038. return -ENAMETOOLONG;
  1039. return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
  1040. }
  1041. /**
  1042. * gfs2_mkdir - Make a directory
  1043. * @mnt_userns: User namespace of the mount the inode was found from
  1044. * @dir: The parent directory of the new one
  1045. * @dentry: The dentry of the new directory
  1046. * @mode: The mode of the new directory
  1047. *
  1048. * Returns: errno
  1049. */
  1050. static int gfs2_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
  1051. struct dentry *dentry, umode_t mode)
  1052. {
  1053. unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
  1054. return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0);
  1055. }
  1056. /**
  1057. * gfs2_mknod - Make a special file
  1058. * @mnt_userns: User namespace of the mount the inode was found from
  1059. * @dir: The directory in which the special file will reside
  1060. * @dentry: The dentry of the special file
  1061. * @mode: The mode of the special file
  1062. * @dev: The device specification of the special file
  1063. *
  1064. */
  1065. static int gfs2_mknod(struct user_namespace *mnt_userns, struct inode *dir,
  1066. struct dentry *dentry, umode_t mode, dev_t dev)
  1067. {
  1068. return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
  1069. }
  1070. /**
  1071. * gfs2_atomic_open - Atomically open a file
  1072. * @dir: The directory
  1073. * @dentry: The proposed new entry
  1074. * @file: The proposed new struct file
  1075. * @flags: open flags
  1076. * @mode: File mode
  1077. *
  1078. * Returns: error code or 0 for success
  1079. */
  1080. static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
  1081. struct file *file, unsigned flags,
  1082. umode_t mode)
  1083. {
  1084. struct dentry *d;
  1085. bool excl = !!(flags & O_EXCL);
  1086. if (!d_in_lookup(dentry))
  1087. goto skip_lookup;
  1088. d = __gfs2_lookup(dir, dentry, file);
  1089. if (IS_ERR(d))
  1090. return PTR_ERR(d);
  1091. if (d != NULL)
  1092. dentry = d;
  1093. if (d_really_is_positive(dentry)) {
  1094. if (!(file->f_mode & FMODE_OPENED))
  1095. return finish_no_open(file, d);
  1096. dput(d);
  1097. return excl && (flags & O_CREAT) ? -EEXIST : 0;
  1098. }
  1099. BUG_ON(d != NULL);
  1100. skip_lookup:
  1101. if (!(flags & O_CREAT))
  1102. return -ENOENT;
  1103. return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
  1104. }
  1105. /*
  1106. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  1107. * @this: move this
  1108. * @to: to here
  1109. *
  1110. * Follow @to back to the root and make sure we don't encounter @this
  1111. * Assumes we already hold the rename lock.
  1112. *
  1113. * Returns: errno
  1114. */
  1115. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1116. {
  1117. struct inode *dir = &to->i_inode;
  1118. struct super_block *sb = dir->i_sb;
  1119. struct inode *tmp;
  1120. int error = 0;
  1121. igrab(dir);
  1122. for (;;) {
  1123. if (dir == &this->i_inode) {
  1124. error = -EINVAL;
  1125. break;
  1126. }
  1127. if (dir == d_inode(sb->s_root)) {
  1128. error = 0;
  1129. break;
  1130. }
  1131. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1132. if (!tmp) {
  1133. error = -ENOENT;
  1134. break;
  1135. }
  1136. if (IS_ERR(tmp)) {
  1137. error = PTR_ERR(tmp);
  1138. break;
  1139. }
  1140. iput(dir);
  1141. dir = tmp;
  1142. }
  1143. iput(dir);
  1144. return error;
  1145. }
  1146. /**
  1147. * update_moved_ino - Update an inode that's being moved
  1148. * @ip: The inode being moved
  1149. * @ndip: The parent directory of the new filename
  1150. * @dir_rename: True of ip is a directory
  1151. *
  1152. * Returns: errno
  1153. */
  1154. static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
  1155. int dir_rename)
  1156. {
  1157. if (dir_rename)
  1158. return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1159. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1160. mark_inode_dirty_sync(&ip->i_inode);
  1161. return 0;
  1162. }
  1163. /**
  1164. * gfs2_rename - Rename a file
  1165. * @odir: Parent directory of old file name
  1166. * @odentry: The old dentry of the file
  1167. * @ndir: Parent directory of new file name
  1168. * @ndentry: The new dentry of the file
  1169. *
  1170. * Returns: errno
  1171. */
  1172. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1173. struct inode *ndir, struct dentry *ndentry)
  1174. {
  1175. struct gfs2_inode *odip = GFS2_I(odir);
  1176. struct gfs2_inode *ndip = GFS2_I(ndir);
  1177. struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
  1178. struct gfs2_inode *nip = NULL;
  1179. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1180. struct gfs2_holder ghs[4], r_gh, rd_gh;
  1181. struct gfs2_rgrpd *nrgd;
  1182. unsigned int num_gh;
  1183. int dir_rename = 0;
  1184. struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
  1185. unsigned int x;
  1186. int error;
  1187. gfs2_holder_mark_uninitialized(&r_gh);
  1188. gfs2_holder_mark_uninitialized(&rd_gh);
  1189. if (d_really_is_positive(ndentry)) {
  1190. nip = GFS2_I(d_inode(ndentry));
  1191. if (ip == nip)
  1192. return 0;
  1193. }
  1194. error = gfs2_rindex_update(sdp);
  1195. if (error)
  1196. return error;
  1197. error = gfs2_qa_get(ndip);
  1198. if (error)
  1199. return error;
  1200. if (odip != ndip) {
  1201. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1202. 0, &r_gh);
  1203. if (error)
  1204. goto out;
  1205. if (S_ISDIR(ip->i_inode.i_mode)) {
  1206. dir_rename = 1;
  1207. /* don't move a directory into its subdir */
  1208. error = gfs2_ok_to_move(ip, ndip);
  1209. if (error)
  1210. goto out_gunlock_r;
  1211. }
  1212. }
  1213. num_gh = 1;
  1214. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
  1215. if (odip != ndip) {
  1216. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
  1217. ghs + num_gh);
  1218. num_gh++;
  1219. }
  1220. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
  1221. num_gh++;
  1222. if (nip) {
  1223. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
  1224. ghs + num_gh);
  1225. num_gh++;
  1226. }
  1227. for (x = 0; x < num_gh; x++) {
  1228. error = gfs2_glock_nq(ghs + x);
  1229. if (error)
  1230. goto out_gunlock;
  1231. }
  1232. error = gfs2_glock_async_wait(num_gh, ghs);
  1233. if (error)
  1234. goto out_gunlock;
  1235. if (nip) {
  1236. /* Grab the resource group glock for unlink flag twiddling.
  1237. * This is the case where the target dinode already exists
  1238. * so we unlink before doing the rename.
  1239. */
  1240. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1241. if (!nrgd) {
  1242. error = -ENOENT;
  1243. goto out_gunlock;
  1244. }
  1245. error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE,
  1246. LM_FLAG_NODE_SCOPE, &rd_gh);
  1247. if (error)
  1248. goto out_gunlock;
  1249. }
  1250. error = -ENOENT;
  1251. if (ip->i_inode.i_nlink == 0)
  1252. goto out_gunlock;
  1253. /* Check out the old directory */
  1254. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1255. if (error)
  1256. goto out_gunlock;
  1257. /* Check out the new directory */
  1258. if (nip) {
  1259. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1260. if (error)
  1261. goto out_gunlock;
  1262. if (nip->i_inode.i_nlink == 0) {
  1263. error = -EAGAIN;
  1264. goto out_gunlock;
  1265. }
  1266. if (S_ISDIR(nip->i_inode.i_mode)) {
  1267. if (nip->i_entries < 2) {
  1268. gfs2_consist_inode(nip);
  1269. error = -EIO;
  1270. goto out_gunlock;
  1271. }
  1272. if (nip->i_entries > 2) {
  1273. error = -ENOTEMPTY;
  1274. goto out_gunlock;
  1275. }
  1276. }
  1277. } else {
  1278. error = gfs2_permission(&init_user_ns, ndir,
  1279. MAY_WRITE | MAY_EXEC);
  1280. if (error)
  1281. goto out_gunlock;
  1282. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1283. switch (error) {
  1284. case -ENOENT:
  1285. error = 0;
  1286. break;
  1287. case 0:
  1288. error = -EEXIST;
  1289. goto out_gunlock;
  1290. default:
  1291. goto out_gunlock;
  1292. }
  1293. if (odip != ndip) {
  1294. if (!ndip->i_inode.i_nlink) {
  1295. error = -ENOENT;
  1296. goto out_gunlock;
  1297. }
  1298. if (ndip->i_entries == (u32)-1) {
  1299. error = -EFBIG;
  1300. goto out_gunlock;
  1301. }
  1302. if (S_ISDIR(ip->i_inode.i_mode) &&
  1303. ndip->i_inode.i_nlink == (u32)-1) {
  1304. error = -EMLINK;
  1305. goto out_gunlock;
  1306. }
  1307. }
  1308. }
  1309. /* Check out the dir to be renamed */
  1310. if (dir_rename) {
  1311. error = gfs2_permission(&init_user_ns, d_inode(odentry),
  1312. MAY_WRITE);
  1313. if (error)
  1314. goto out_gunlock;
  1315. }
  1316. if (nip == NULL) {
  1317. error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
  1318. if (error)
  1319. goto out_gunlock;
  1320. }
  1321. if (da.nr_blocks) {
  1322. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  1323. error = gfs2_quota_lock_check(ndip, &ap);
  1324. if (error)
  1325. goto out_gunlock;
  1326. error = gfs2_inplace_reserve(ndip, &ap);
  1327. if (error)
  1328. goto out_gunlock_q;
  1329. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
  1330. 4 * RES_LEAF + 4, 0);
  1331. if (error)
  1332. goto out_ipreserv;
  1333. } else {
  1334. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1335. 5 * RES_LEAF + 4, 0);
  1336. if (error)
  1337. goto out_gunlock;
  1338. }
  1339. /* Remove the target file, if it exists */
  1340. if (nip)
  1341. error = gfs2_unlink_inode(ndip, ndentry);
  1342. error = update_moved_ino(ip, ndip, dir_rename);
  1343. if (error)
  1344. goto out_end_trans;
  1345. error = gfs2_dir_del(odip, odentry);
  1346. if (error)
  1347. goto out_end_trans;
  1348. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
  1349. if (error)
  1350. goto out_end_trans;
  1351. out_end_trans:
  1352. gfs2_trans_end(sdp);
  1353. out_ipreserv:
  1354. if (da.nr_blocks)
  1355. gfs2_inplace_release(ndip);
  1356. out_gunlock_q:
  1357. if (da.nr_blocks)
  1358. gfs2_quota_unlock(ndip);
  1359. out_gunlock:
  1360. gfs2_dir_no_add(&da);
  1361. if (gfs2_holder_initialized(&rd_gh))
  1362. gfs2_glock_dq_uninit(&rd_gh);
  1363. while (x--) {
  1364. if (gfs2_holder_queued(ghs + x))
  1365. gfs2_glock_dq(ghs + x);
  1366. gfs2_holder_uninit(ghs + x);
  1367. }
  1368. out_gunlock_r:
  1369. if (gfs2_holder_initialized(&r_gh))
  1370. gfs2_glock_dq_uninit(&r_gh);
  1371. out:
  1372. gfs2_qa_put(ndip);
  1373. return error;
  1374. }
  1375. /**
  1376. * gfs2_exchange - exchange two files
  1377. * @odir: Parent directory of old file name
  1378. * @odentry: The old dentry of the file
  1379. * @ndir: Parent directory of new file name
  1380. * @ndentry: The new dentry of the file
  1381. * @flags: The rename flags
  1382. *
  1383. * Returns: errno
  1384. */
  1385. static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
  1386. struct inode *ndir, struct dentry *ndentry,
  1387. unsigned int flags)
  1388. {
  1389. struct gfs2_inode *odip = GFS2_I(odir);
  1390. struct gfs2_inode *ndip = GFS2_I(ndir);
  1391. struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
  1392. struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
  1393. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1394. struct gfs2_holder ghs[4], r_gh;
  1395. unsigned int num_gh;
  1396. unsigned int x;
  1397. umode_t old_mode = oip->i_inode.i_mode;
  1398. umode_t new_mode = nip->i_inode.i_mode;
  1399. int error;
  1400. gfs2_holder_mark_uninitialized(&r_gh);
  1401. error = gfs2_rindex_update(sdp);
  1402. if (error)
  1403. return error;
  1404. if (odip != ndip) {
  1405. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1406. 0, &r_gh);
  1407. if (error)
  1408. goto out;
  1409. if (S_ISDIR(old_mode)) {
  1410. /* don't move a directory into its subdir */
  1411. error = gfs2_ok_to_move(oip, ndip);
  1412. if (error)
  1413. goto out_gunlock_r;
  1414. }
  1415. if (S_ISDIR(new_mode)) {
  1416. /* don't move a directory into its subdir */
  1417. error = gfs2_ok_to_move(nip, odip);
  1418. if (error)
  1419. goto out_gunlock_r;
  1420. }
  1421. }
  1422. num_gh = 1;
  1423. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
  1424. if (odip != ndip) {
  1425. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
  1426. ghs + num_gh);
  1427. num_gh++;
  1428. }
  1429. gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
  1430. num_gh++;
  1431. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
  1432. num_gh++;
  1433. for (x = 0; x < num_gh; x++) {
  1434. error = gfs2_glock_nq(ghs + x);
  1435. if (error)
  1436. goto out_gunlock;
  1437. }
  1438. error = gfs2_glock_async_wait(num_gh, ghs);
  1439. if (error)
  1440. goto out_gunlock;
  1441. error = -ENOENT;
  1442. if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
  1443. goto out_gunlock;
  1444. error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
  1445. if (error)
  1446. goto out_gunlock;
  1447. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1448. if (error)
  1449. goto out_gunlock;
  1450. if (S_ISDIR(old_mode)) {
  1451. error = gfs2_permission(&init_user_ns, odentry->d_inode,
  1452. MAY_WRITE);
  1453. if (error)
  1454. goto out_gunlock;
  1455. }
  1456. if (S_ISDIR(new_mode)) {
  1457. error = gfs2_permission(&init_user_ns, ndentry->d_inode,
  1458. MAY_WRITE);
  1459. if (error)
  1460. goto out_gunlock;
  1461. }
  1462. error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
  1463. if (error)
  1464. goto out_gunlock;
  1465. error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
  1466. if (error)
  1467. goto out_end_trans;
  1468. error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
  1469. if (error)
  1470. goto out_end_trans;
  1471. error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
  1472. IF2DT(old_mode));
  1473. if (error)
  1474. goto out_end_trans;
  1475. error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
  1476. IF2DT(new_mode));
  1477. if (error)
  1478. goto out_end_trans;
  1479. if (odip != ndip) {
  1480. if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
  1481. inc_nlink(&odip->i_inode);
  1482. drop_nlink(&ndip->i_inode);
  1483. } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
  1484. inc_nlink(&ndip->i_inode);
  1485. drop_nlink(&odip->i_inode);
  1486. }
  1487. }
  1488. mark_inode_dirty(&ndip->i_inode);
  1489. if (odip != ndip)
  1490. mark_inode_dirty(&odip->i_inode);
  1491. out_end_trans:
  1492. gfs2_trans_end(sdp);
  1493. out_gunlock:
  1494. while (x--) {
  1495. if (gfs2_holder_queued(ghs + x))
  1496. gfs2_glock_dq(ghs + x);
  1497. gfs2_holder_uninit(ghs + x);
  1498. }
  1499. out_gunlock_r:
  1500. if (gfs2_holder_initialized(&r_gh))
  1501. gfs2_glock_dq_uninit(&r_gh);
  1502. out:
  1503. return error;
  1504. }
  1505. static int gfs2_rename2(struct user_namespace *mnt_userns, struct inode *odir,
  1506. struct dentry *odentry, struct inode *ndir,
  1507. struct dentry *ndentry, unsigned int flags)
  1508. {
  1509. flags &= ~RENAME_NOREPLACE;
  1510. if (flags & ~RENAME_EXCHANGE)
  1511. return -EINVAL;
  1512. if (flags & RENAME_EXCHANGE)
  1513. return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
  1514. return gfs2_rename(odir, odentry, ndir, ndentry);
  1515. }
  1516. /**
  1517. * gfs2_get_link - Follow a symbolic link
  1518. * @dentry: The dentry of the link
  1519. * @inode: The inode of the link
  1520. * @done: destructor for return value
  1521. *
  1522. * This can handle symlinks of any size.
  1523. *
  1524. * Returns: 0 on success or error code
  1525. */
  1526. static const char *gfs2_get_link(struct dentry *dentry,
  1527. struct inode *inode,
  1528. struct delayed_call *done)
  1529. {
  1530. struct gfs2_inode *ip = GFS2_I(inode);
  1531. struct gfs2_holder i_gh;
  1532. struct buffer_head *dibh;
  1533. unsigned int size;
  1534. char *buf;
  1535. int error;
  1536. if (!dentry)
  1537. return ERR_PTR(-ECHILD);
  1538. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1539. error = gfs2_glock_nq(&i_gh);
  1540. if (error) {
  1541. gfs2_holder_uninit(&i_gh);
  1542. return ERR_PTR(error);
  1543. }
  1544. size = (unsigned int)i_size_read(&ip->i_inode);
  1545. if (size == 0) {
  1546. gfs2_consist_inode(ip);
  1547. buf = ERR_PTR(-EIO);
  1548. goto out;
  1549. }
  1550. error = gfs2_meta_inode_buffer(ip, &dibh);
  1551. if (error) {
  1552. buf = ERR_PTR(error);
  1553. goto out;
  1554. }
  1555. buf = kzalloc(size + 1, GFP_NOFS);
  1556. if (!buf)
  1557. buf = ERR_PTR(-ENOMEM);
  1558. else
  1559. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1560. brelse(dibh);
  1561. out:
  1562. gfs2_glock_dq_uninit(&i_gh);
  1563. if (!IS_ERR(buf))
  1564. set_delayed_call(done, kfree_link, buf);
  1565. return buf;
  1566. }
  1567. /**
  1568. * gfs2_permission
  1569. * @mnt_userns: User namespace of the mount the inode was found from
  1570. * @inode: The inode
  1571. * @mask: The mask to be tested
  1572. *
  1573. * This may be called from the VFS directly, or from within GFS2 with the
  1574. * inode locked, so we look to see if the glock is already locked and only
  1575. * lock the glock if its not already been done.
  1576. *
  1577. * Returns: errno
  1578. */
  1579. int gfs2_permission(struct user_namespace *mnt_userns, struct inode *inode,
  1580. int mask)
  1581. {
  1582. int may_not_block = mask & MAY_NOT_BLOCK;
  1583. struct gfs2_inode *ip;
  1584. struct gfs2_holder i_gh;
  1585. struct gfs2_glock *gl;
  1586. int error;
  1587. gfs2_holder_mark_uninitialized(&i_gh);
  1588. ip = GFS2_I(inode);
  1589. gl = rcu_dereference_check(ip->i_gl, !may_not_block);
  1590. if (unlikely(!gl)) {
  1591. /* inode is getting torn down, must be RCU mode */
  1592. WARN_ON_ONCE(!may_not_block);
  1593. return -ECHILD;
  1594. }
  1595. if (gfs2_glock_is_locked_by_me(gl) == NULL) {
  1596. if (may_not_block)
  1597. return -ECHILD;
  1598. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1599. if (error)
  1600. return error;
  1601. }
  1602. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1603. error = -EPERM;
  1604. else
  1605. error = generic_permission(&init_user_ns, inode, mask);
  1606. if (gfs2_holder_initialized(&i_gh))
  1607. gfs2_glock_dq_uninit(&i_gh);
  1608. return error;
  1609. }
  1610. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1611. {
  1612. setattr_copy(&init_user_ns, inode, attr);
  1613. mark_inode_dirty(inode);
  1614. return 0;
  1615. }
  1616. static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1617. {
  1618. int error;
  1619. if (current->journal_info)
  1620. return __gfs2_setattr_simple(inode, attr);
  1621. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1622. if (error)
  1623. return error;
  1624. error = __gfs2_setattr_simple(inode, attr);
  1625. gfs2_trans_end(GFS2_SB(inode));
  1626. return error;
  1627. }
  1628. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1629. {
  1630. struct gfs2_inode *ip = GFS2_I(inode);
  1631. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1632. kuid_t ouid, nuid;
  1633. kgid_t ogid, ngid;
  1634. int error;
  1635. struct gfs2_alloc_parms ap;
  1636. ouid = inode->i_uid;
  1637. ogid = inode->i_gid;
  1638. nuid = attr->ia_uid;
  1639. ngid = attr->ia_gid;
  1640. if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
  1641. ouid = nuid = NO_UID_QUOTA_CHANGE;
  1642. if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
  1643. ogid = ngid = NO_GID_QUOTA_CHANGE;
  1644. error = gfs2_qa_get(ip);
  1645. if (error)
  1646. return error;
  1647. error = gfs2_rindex_update(sdp);
  1648. if (error)
  1649. goto out;
  1650. error = gfs2_quota_lock(ip, nuid, ngid);
  1651. if (error)
  1652. goto out;
  1653. ap.target = gfs2_get_inode_blocks(&ip->i_inode);
  1654. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1655. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1656. error = gfs2_quota_check(ip, nuid, ngid, &ap);
  1657. if (error)
  1658. goto out_gunlock_q;
  1659. }
  1660. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1661. if (error)
  1662. goto out_gunlock_q;
  1663. error = gfs2_setattr_simple(inode, attr);
  1664. if (error)
  1665. goto out_end_trans;
  1666. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1667. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1668. gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
  1669. gfs2_quota_change(ip, ap.target, nuid, ngid);
  1670. }
  1671. out_end_trans:
  1672. gfs2_trans_end(sdp);
  1673. out_gunlock_q:
  1674. gfs2_quota_unlock(ip);
  1675. out:
  1676. gfs2_qa_put(ip);
  1677. return error;
  1678. }
  1679. /**
  1680. * gfs2_setattr - Change attributes on an inode
  1681. * @mnt_userns: User namespace of the mount the inode was found from
  1682. * @dentry: The dentry which is changing
  1683. * @attr: The structure describing the change
  1684. *
  1685. * The VFS layer wants to change one or more of an inodes attributes. Write
  1686. * that change out to disk.
  1687. *
  1688. * Returns: errno
  1689. */
  1690. static int gfs2_setattr(struct user_namespace *mnt_userns,
  1691. struct dentry *dentry, struct iattr *attr)
  1692. {
  1693. struct inode *inode = d_inode(dentry);
  1694. struct gfs2_inode *ip = GFS2_I(inode);
  1695. struct gfs2_holder i_gh;
  1696. int error;
  1697. error = gfs2_qa_get(ip);
  1698. if (error)
  1699. return error;
  1700. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1701. if (error)
  1702. goto out;
  1703. error = may_setattr(&init_user_ns, inode, attr->ia_valid);
  1704. if (error)
  1705. goto error;
  1706. error = setattr_prepare(&init_user_ns, dentry, attr);
  1707. if (error)
  1708. goto error;
  1709. if (attr->ia_valid & ATTR_SIZE)
  1710. error = gfs2_setattr_size(inode, attr->ia_size);
  1711. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1712. error = setattr_chown(inode, attr);
  1713. else {
  1714. error = gfs2_setattr_simple(inode, attr);
  1715. if (!error && attr->ia_valid & ATTR_MODE)
  1716. error = posix_acl_chmod(&init_user_ns, inode,
  1717. inode->i_mode);
  1718. }
  1719. error:
  1720. if (!error)
  1721. mark_inode_dirty(inode);
  1722. gfs2_glock_dq_uninit(&i_gh);
  1723. out:
  1724. gfs2_qa_put(ip);
  1725. return error;
  1726. }
  1727. /**
  1728. * gfs2_getattr - Read out an inode's attributes
  1729. * @mnt_userns: user namespace of the mount the inode was found from
  1730. * @path: Object to query
  1731. * @stat: The inode's stats
  1732. * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
  1733. * @flags: AT_STATX_xxx setting
  1734. *
  1735. * This may be called from the VFS directly, or from within GFS2 with the
  1736. * inode locked, so we look to see if the glock is already locked and only
  1737. * lock the glock if its not already been done. Note that its the NFS
  1738. * readdirplus operation which causes this to be called (from filldir)
  1739. * with the glock already held.
  1740. *
  1741. * Returns: errno
  1742. */
  1743. static int gfs2_getattr(struct user_namespace *mnt_userns,
  1744. const struct path *path, struct kstat *stat,
  1745. u32 request_mask, unsigned int flags)
  1746. {
  1747. struct inode *inode = d_inode(path->dentry);
  1748. struct gfs2_inode *ip = GFS2_I(inode);
  1749. struct gfs2_holder gh;
  1750. u32 gfsflags;
  1751. int error;
  1752. gfs2_holder_mark_uninitialized(&gh);
  1753. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1754. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1755. if (error)
  1756. return error;
  1757. }
  1758. gfsflags = ip->i_diskflags;
  1759. if (gfsflags & GFS2_DIF_APPENDONLY)
  1760. stat->attributes |= STATX_ATTR_APPEND;
  1761. if (gfsflags & GFS2_DIF_IMMUTABLE)
  1762. stat->attributes |= STATX_ATTR_IMMUTABLE;
  1763. stat->attributes_mask |= (STATX_ATTR_APPEND |
  1764. STATX_ATTR_COMPRESSED |
  1765. STATX_ATTR_ENCRYPTED |
  1766. STATX_ATTR_IMMUTABLE |
  1767. STATX_ATTR_NODUMP);
  1768. generic_fillattr(&init_user_ns, inode, stat);
  1769. if (gfs2_holder_initialized(&gh))
  1770. gfs2_glock_dq_uninit(&gh);
  1771. return 0;
  1772. }
  1773. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1774. u64 start, u64 len)
  1775. {
  1776. struct gfs2_inode *ip = GFS2_I(inode);
  1777. struct gfs2_holder gh;
  1778. int ret;
  1779. inode_lock_shared(inode);
  1780. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1781. if (ret)
  1782. goto out;
  1783. ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
  1784. gfs2_glock_dq_uninit(&gh);
  1785. out:
  1786. inode_unlock_shared(inode);
  1787. return ret;
  1788. }
  1789. loff_t gfs2_seek_data(struct file *file, loff_t offset)
  1790. {
  1791. struct inode *inode = file->f_mapping->host;
  1792. struct gfs2_inode *ip = GFS2_I(inode);
  1793. struct gfs2_holder gh;
  1794. loff_t ret;
  1795. inode_lock_shared(inode);
  1796. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1797. if (!ret)
  1798. ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
  1799. gfs2_glock_dq_uninit(&gh);
  1800. inode_unlock_shared(inode);
  1801. if (ret < 0)
  1802. return ret;
  1803. return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
  1804. }
  1805. loff_t gfs2_seek_hole(struct file *file, loff_t offset)
  1806. {
  1807. struct inode *inode = file->f_mapping->host;
  1808. struct gfs2_inode *ip = GFS2_I(inode);
  1809. struct gfs2_holder gh;
  1810. loff_t ret;
  1811. inode_lock_shared(inode);
  1812. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1813. if (!ret)
  1814. ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
  1815. gfs2_glock_dq_uninit(&gh);
  1816. inode_unlock_shared(inode);
  1817. if (ret < 0)
  1818. return ret;
  1819. return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
  1820. }
  1821. static int gfs2_update_time(struct inode *inode, struct timespec64 *time,
  1822. int flags)
  1823. {
  1824. struct gfs2_inode *ip = GFS2_I(inode);
  1825. struct gfs2_glock *gl = ip->i_gl;
  1826. struct gfs2_holder *gh;
  1827. int error;
  1828. gh = gfs2_glock_is_locked_by_me(gl);
  1829. if (gh && !gfs2_glock_is_held_excl(gl)) {
  1830. gfs2_glock_dq(gh);
  1831. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh);
  1832. error = gfs2_glock_nq(gh);
  1833. if (error)
  1834. return error;
  1835. }
  1836. return generic_update_time(inode, time, flags);
  1837. }
  1838. static const struct inode_operations gfs2_file_iops = {
  1839. .permission = gfs2_permission,
  1840. .setattr = gfs2_setattr,
  1841. .getattr = gfs2_getattr,
  1842. .listxattr = gfs2_listxattr,
  1843. .fiemap = gfs2_fiemap,
  1844. .get_acl = gfs2_get_acl,
  1845. .set_acl = gfs2_set_acl,
  1846. .update_time = gfs2_update_time,
  1847. .fileattr_get = gfs2_fileattr_get,
  1848. .fileattr_set = gfs2_fileattr_set,
  1849. };
  1850. static const struct inode_operations gfs2_dir_iops = {
  1851. .create = gfs2_create,
  1852. .lookup = gfs2_lookup,
  1853. .link = gfs2_link,
  1854. .unlink = gfs2_unlink,
  1855. .symlink = gfs2_symlink,
  1856. .mkdir = gfs2_mkdir,
  1857. .rmdir = gfs2_unlink,
  1858. .mknod = gfs2_mknod,
  1859. .rename = gfs2_rename2,
  1860. .permission = gfs2_permission,
  1861. .setattr = gfs2_setattr,
  1862. .getattr = gfs2_getattr,
  1863. .listxattr = gfs2_listxattr,
  1864. .fiemap = gfs2_fiemap,
  1865. .get_acl = gfs2_get_acl,
  1866. .set_acl = gfs2_set_acl,
  1867. .update_time = gfs2_update_time,
  1868. .atomic_open = gfs2_atomic_open,
  1869. .fileattr_get = gfs2_fileattr_get,
  1870. .fileattr_set = gfs2_fileattr_set,
  1871. };
  1872. static const struct inode_operations gfs2_symlink_iops = {
  1873. .get_link = gfs2_get_link,
  1874. .permission = gfs2_permission,
  1875. .setattr = gfs2_setattr,
  1876. .getattr = gfs2_getattr,
  1877. .listxattr = gfs2_listxattr,
  1878. .fiemap = gfs2_fiemap,
  1879. };