inode.c 49 KB

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