inode.c 48 KB

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