inode.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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(-ENOBUFS);
  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_gl->gl_object = NULL;
  166. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  167. fail_iopen:
  168. if (io_gl)
  169. gfs2_glock_put(io_gl);
  170. fail_put:
  171. ip->i_gl->gl_object = NULL;
  172. gfs2_glock_put(ip->i_gl);
  173. fail:
  174. iget_failed(inode);
  175. return ERR_PTR(error);
  176. }
  177. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  178. u64 *no_formal_ino, unsigned int blktype)
  179. {
  180. struct super_block *sb = sdp->sd_vfs;
  181. struct gfs2_holder i_gh;
  182. struct inode *inode = NULL;
  183. int error;
  184. /* Must not read in block until block type is verified */
  185. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  186. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  187. if (error)
  188. return ERR_PTR(error);
  189. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  190. if (error)
  191. goto fail;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  193. if (IS_ERR(inode))
  194. goto fail;
  195. /* Two extra checks for NFS only */
  196. if (no_formal_ino) {
  197. error = -ESTALE;
  198. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  199. goto fail_iput;
  200. error = -EIO;
  201. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  202. goto fail_iput;
  203. error = 0;
  204. }
  205. fail:
  206. gfs2_glock_dq_uninit(&i_gh);
  207. return error ? ERR_PTR(error) : inode;
  208. fail_iput:
  209. iput(inode);
  210. goto fail;
  211. }
  212. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  213. {
  214. struct qstr qstr;
  215. struct inode *inode;
  216. gfs2_str2qstr(&qstr, name);
  217. inode = gfs2_lookupi(dip, &qstr, 1);
  218. /* gfs2_lookupi has inconsistent callers: vfs
  219. * related routines expect NULL for no entry found,
  220. * gfs2_lookup_simple callers expect ENOENT
  221. * and do not check for NULL.
  222. */
  223. if (inode == NULL)
  224. return ERR_PTR(-ENOENT);
  225. else
  226. return inode;
  227. }
  228. /**
  229. * gfs2_lookupi - Look up a filename in a directory and return its inode
  230. * @d_gh: An initialized holder for the directory glock
  231. * @name: The name of the inode to look for
  232. * @is_root: If 1, ignore the caller's permissions
  233. * @i_gh: An uninitialized holder for the new inode glock
  234. *
  235. * This can be called via the VFS filldir function when NFS is doing
  236. * a readdirplus and the inode which its intending to stat isn't
  237. * already in cache. In this case we must not take the directory glock
  238. * again, since the readdir call will have already taken that lock.
  239. *
  240. * Returns: errno
  241. */
  242. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  243. int is_root)
  244. {
  245. struct super_block *sb = dir->i_sb;
  246. struct gfs2_inode *dip = GFS2_I(dir);
  247. struct gfs2_holder d_gh;
  248. int error = 0;
  249. struct inode *inode = NULL;
  250. int unlock = 0;
  251. if (!name->len || name->len > GFS2_FNAMESIZE)
  252. return ERR_PTR(-ENAMETOOLONG);
  253. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  254. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  255. dir == sb->s_root->d_inode)) {
  256. igrab(dir);
  257. return dir;
  258. }
  259. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  260. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  261. if (error)
  262. return ERR_PTR(error);
  263. unlock = 1;
  264. }
  265. if (!is_root) {
  266. error = gfs2_permission(dir, MAY_EXEC);
  267. if (error)
  268. goto out;
  269. }
  270. inode = gfs2_dir_search(dir, name);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (unlock)
  275. gfs2_glock_dq_uninit(&d_gh);
  276. if (error == -ENOENT)
  277. return NULL;
  278. return inode ? inode : ERR_PTR(error);
  279. }
  280. /**
  281. * create_ok - OK to create a new on-disk inode here?
  282. * @dip: Directory in which dinode is to be created
  283. * @name: Name of new dinode
  284. * @mode:
  285. *
  286. * Returns: errno
  287. */
  288. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  289. umode_t mode)
  290. {
  291. int error;
  292. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  293. if (error)
  294. return error;
  295. /* Don't create entries in an unlinked directory */
  296. if (!dip->i_inode.i_nlink)
  297. return -ENOENT;
  298. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  299. switch (error) {
  300. case -ENOENT:
  301. error = 0;
  302. break;
  303. case 0:
  304. return -EEXIST;
  305. default:
  306. return error;
  307. }
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  315. struct inode *inode)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) &&
  319. !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
  320. if (S_ISDIR(inode->i_mode))
  321. inode->i_mode |= S_ISUID;
  322. else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
  323. inode->i_mode &= ~07111;
  324. inode->i_uid = dip->i_inode.i_uid;
  325. } else
  326. inode->i_uid = current_fsuid();
  327. if (dip->i_inode.i_mode & S_ISGID) {
  328. if (S_ISDIR(inode->i_mode))
  329. inode->i_mode |= S_ISGID;
  330. inode->i_gid = dip->i_inode.i_gid;
  331. } else
  332. inode->i_gid = current_fsgid();
  333. }
  334. static int alloc_dinode(struct gfs2_inode *ip, u32 flags)
  335. {
  336. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  337. int error;
  338. int dblocks = 1;
  339. error = gfs2_inplace_reserve(ip, RES_DINODE, flags);
  340. if (error)
  341. goto out;
  342. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  343. if (error)
  344. goto out_ipreserv;
  345. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, &dblocks, 1, &ip->i_generation);
  346. ip->i_no_formal_ino = ip->i_generation;
  347. ip->i_inode.i_ino = ip->i_no_addr;
  348. ip->i_goal = ip->i_no_addr;
  349. gfs2_trans_end(sdp);
  350. out_ipreserv:
  351. gfs2_inplace_release(ip);
  352. out:
  353. return error;
  354. }
  355. static void gfs2_init_dir(struct buffer_head *dibh,
  356. const struct gfs2_inode *parent)
  357. {
  358. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  359. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  360. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  361. dent->de_inum = di->di_num; /* already GFS2 endian */
  362. dent->de_type = cpu_to_be16(DT_DIR);
  363. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  364. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  365. gfs2_inum_out(parent, dent);
  366. dent->de_type = cpu_to_be16(DT_DIR);
  367. }
  368. /**
  369. * init_dinode - Fill in a new dinode structure
  370. * @dip: The directory this inode is being created in
  371. * @ip: The inode
  372. * @symname: The symlink destination (if a symlink)
  373. * @bhp: The buffer head (returned to caller)
  374. *
  375. */
  376. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  377. const char *symname, struct buffer_head **bhp)
  378. {
  379. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  380. struct gfs2_dinode *di;
  381. struct buffer_head *dibh;
  382. struct timespec tv = CURRENT_TIME;
  383. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  384. gfs2_trans_add_meta(ip->i_gl, dibh);
  385. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  386. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  387. di = (struct gfs2_dinode *)dibh->b_data;
  388. di->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  389. di->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  390. di->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  391. di->di_uid = cpu_to_be32(i_uid_read(&ip->i_inode));
  392. di->di_gid = cpu_to_be32(i_gid_read(&ip->i_inode));
  393. di->di_nlink = 0;
  394. di->di_size = cpu_to_be64(ip->i_inode.i_size);
  395. di->di_blocks = cpu_to_be64(1);
  396. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  397. di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
  398. di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
  399. di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_no_addr);
  400. di->di_generation = cpu_to_be64(ip->i_generation);
  401. di->di_flags = 0;
  402. di->__pad1 = 0;
  403. di->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) ? GFS2_FORMAT_DE : 0);
  404. di->di_height = 0;
  405. di->__pad2 = 0;
  406. di->__pad3 = 0;
  407. di->di_depth = 0;
  408. di->di_entries = 0;
  409. memset(&di->__pad4, 0, sizeof(di->__pad4));
  410. di->di_eattr = 0;
  411. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  412. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  413. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  414. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  415. switch(ip->i_inode.i_mode & S_IFMT) {
  416. case S_IFREG:
  417. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  418. gfs2_tune_get(sdp, gt_new_files_jdata))
  419. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  420. break;
  421. case S_IFDIR:
  422. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  423. GFS2_DIF_INHERIT_JDATA);
  424. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  425. di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  426. di->di_entries = cpu_to_be32(2);
  427. gfs2_init_dir(dibh, dip);
  428. break;
  429. case S_IFLNK:
  430. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  431. break;
  432. }
  433. set_buffer_uptodate(dibh);
  434. *bhp = dibh;
  435. }
  436. static int make_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  437. const char *symname, struct buffer_head **bhp)
  438. {
  439. struct inode *inode = &ip->i_inode;
  440. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  441. int error;
  442. error = gfs2_rindex_update(sdp);
  443. if (error)
  444. return error;
  445. error = gfs2_quota_lock(dip, inode->i_uid, inode->i_gid);
  446. if (error)
  447. return error;
  448. error = gfs2_quota_check(dip, inode->i_uid, inode->i_gid);
  449. if (error)
  450. goto out_quota;
  451. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  452. if (error)
  453. goto out_quota;
  454. init_dinode(dip, ip, symname, bhp);
  455. gfs2_quota_change(dip, +1, inode->i_uid, inode->i_gid);
  456. gfs2_trans_end(sdp);
  457. out_quota:
  458. gfs2_quota_unlock(dip);
  459. return error;
  460. }
  461. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  462. struct gfs2_inode *ip)
  463. {
  464. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  465. int alloc_required;
  466. struct buffer_head *dibh;
  467. int error;
  468. error = gfs2_rindex_update(sdp);
  469. if (error)
  470. return error;
  471. error = gfs2_quota_lock(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  472. if (error)
  473. goto fail;
  474. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  475. if (alloc_required < 0)
  476. goto fail_quota_locks;
  477. if (alloc_required) {
  478. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  479. if (error)
  480. goto fail_quota_locks;
  481. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres, 0);
  482. if (error)
  483. goto fail_quota_locks;
  484. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  485. dip->i_rgd->rd_length +
  486. 2 * RES_DINODE +
  487. RES_STATFS + RES_QUOTA, 0);
  488. if (error)
  489. goto fail_ipreserv;
  490. } else {
  491. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  492. if (error)
  493. goto fail_quota_locks;
  494. }
  495. error = gfs2_dir_add(&dip->i_inode, name, ip);
  496. if (error)
  497. goto fail_end_trans;
  498. error = gfs2_meta_inode_buffer(ip, &dibh);
  499. if (error)
  500. goto fail_end_trans;
  501. set_nlink(&ip->i_inode, S_ISDIR(ip->i_inode.i_mode) ? 2 : 1);
  502. gfs2_trans_add_meta(ip->i_gl, dibh);
  503. gfs2_dinode_out(ip, dibh->b_data);
  504. brelse(dibh);
  505. return 0;
  506. fail_end_trans:
  507. gfs2_trans_end(sdp);
  508. fail_ipreserv:
  509. if (alloc_required)
  510. gfs2_inplace_release(dip);
  511. fail_quota_locks:
  512. gfs2_quota_unlock(dip);
  513. fail:
  514. return error;
  515. }
  516. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  517. void *fs_info)
  518. {
  519. const struct xattr *xattr;
  520. int err = 0;
  521. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  522. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  523. xattr->value_len, 0,
  524. GFS2_EATYPE_SECURITY);
  525. if (err < 0)
  526. break;
  527. }
  528. return err;
  529. }
  530. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  531. const struct qstr *qstr)
  532. {
  533. return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  534. &gfs2_initxattrs, NULL);
  535. }
  536. /**
  537. * gfs2_create_inode - Create a new inode
  538. * @dir: The parent directory
  539. * @dentry: The new dentry
  540. * @mode: The permissions on the new inode
  541. * @dev: For device nodes, this is the device number
  542. * @symname: For symlinks, this is the link destination
  543. * @size: The initial size of the inode (ignored for directories)
  544. *
  545. * Returns: 0 on success, or error code
  546. */
  547. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  548. umode_t mode, dev_t dev, const char *symname,
  549. unsigned int size, int excl)
  550. {
  551. const struct qstr *name = &dentry->d_name;
  552. struct gfs2_holder ghs[2];
  553. struct inode *inode = NULL;
  554. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  555. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  556. struct gfs2_glock *io_gl;
  557. int error;
  558. struct buffer_head *bh = NULL;
  559. u32 aflags = 0;
  560. if (!name->len || name->len > GFS2_FNAMESIZE)
  561. return -ENAMETOOLONG;
  562. error = gfs2_rs_alloc(dip);
  563. if (error)
  564. return error;
  565. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  566. if (error)
  567. goto fail;
  568. error = create_ok(dip, name, mode);
  569. if ((error == -EEXIST) && S_ISREG(mode) && !excl) {
  570. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  571. gfs2_glock_dq_uninit(ghs);
  572. d_instantiate(dentry, inode);
  573. return IS_ERR(inode) ? PTR_ERR(inode) : 0;
  574. }
  575. if (error)
  576. goto fail_gunlock;
  577. inode = new_inode(sdp->sd_vfs);
  578. if (!inode) {
  579. gfs2_glock_dq_uninit(ghs);
  580. return -ENOMEM;
  581. }
  582. ip = GFS2_I(inode);
  583. error = gfs2_rs_alloc(ip);
  584. if (error)
  585. goto fail_free_inode;
  586. set_bit(GIF_INVALID, &ip->i_flags);
  587. inode->i_mode = mode;
  588. inode->i_rdev = dev;
  589. inode->i_size = size;
  590. munge_mode_uid_gid(dip, inode);
  591. ip->i_goal = dip->i_goal;
  592. if ((GFS2_I(sdp->sd_root_dir->d_inode) == dip) ||
  593. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  594. aflags |= GFS2_AF_ORLOV;
  595. error = alloc_dinode(ip, aflags);
  596. if (error)
  597. goto fail_free_inode;
  598. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  599. if (error)
  600. goto fail_free_inode;
  601. ip->i_gl->gl_object = ip;
  602. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  603. if (error)
  604. goto fail_free_inode;
  605. error = make_dinode(dip, ip, symname, &bh);
  606. if (error)
  607. goto fail_gunlock2;
  608. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  609. if (error)
  610. goto fail_gunlock2;
  611. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  612. if (error)
  613. goto fail_gunlock2;
  614. ip->i_iopen_gh.gh_gl->gl_object = ip;
  615. gfs2_glock_put(io_gl);
  616. gfs2_set_iop(inode);
  617. insert_inode_hash(inode);
  618. error = gfs2_inode_refresh(ip);
  619. if (error)
  620. goto fail_gunlock3;
  621. error = gfs2_acl_create(dip, inode);
  622. if (error)
  623. goto fail_gunlock3;
  624. error = gfs2_security_init(dip, ip, name);
  625. if (error)
  626. goto fail_gunlock3;
  627. error = link_dinode(dip, name, ip);
  628. if (error)
  629. goto fail_gunlock3;
  630. if (bh)
  631. brelse(bh);
  632. gfs2_trans_end(sdp);
  633. gfs2_inplace_release(dip);
  634. gfs2_quota_unlock(dip);
  635. mark_inode_dirty(inode);
  636. gfs2_glock_dq_uninit_m(2, ghs);
  637. d_instantiate(dentry, inode);
  638. return 0;
  639. fail_gunlock3:
  640. gfs2_glock_dq_uninit(ghs + 1);
  641. if (ip->i_gl)
  642. gfs2_glock_put(ip->i_gl);
  643. goto fail_gunlock;
  644. fail_gunlock2:
  645. gfs2_glock_dq_uninit(ghs + 1);
  646. fail_free_inode:
  647. if (ip->i_gl)
  648. gfs2_glock_put(ip->i_gl);
  649. gfs2_rs_delete(ip);
  650. free_inode_nonrcu(inode);
  651. inode = NULL;
  652. fail_gunlock:
  653. gfs2_glock_dq_uninit(ghs);
  654. if (inode && !IS_ERR(inode)) {
  655. set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
  656. iput(inode);
  657. }
  658. fail:
  659. if (bh)
  660. brelse(bh);
  661. return error;
  662. }
  663. /**
  664. * gfs2_create - Create a file
  665. * @dir: The directory in which to create the file
  666. * @dentry: The dentry of the new file
  667. * @mode: The mode of the new file
  668. *
  669. * Returns: errno
  670. */
  671. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  672. umode_t mode, bool excl)
  673. {
  674. return gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0, excl);
  675. }
  676. /**
  677. * gfs2_lookup - Look up a filename in a directory and return its inode
  678. * @dir: The directory inode
  679. * @dentry: The dentry of the new inode
  680. * @nd: passed from Linux VFS, ignored by us
  681. *
  682. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  683. *
  684. * Returns: errno
  685. */
  686. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  687. unsigned int flags)
  688. {
  689. struct inode *inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  690. if (inode && !IS_ERR(inode)) {
  691. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  692. struct gfs2_holder gh;
  693. int error;
  694. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  695. if (error) {
  696. iput(inode);
  697. return ERR_PTR(error);
  698. }
  699. gfs2_glock_dq_uninit(&gh);
  700. }
  701. return d_splice_alias(inode, dentry);
  702. }
  703. /**
  704. * gfs2_link - Link to a file
  705. * @old_dentry: The inode to link
  706. * @dir: Add link to this directory
  707. * @dentry: The name of the link
  708. *
  709. * Link the inode in "old_dentry" into the directory "dir" with the
  710. * name in "dentry".
  711. *
  712. * Returns: errno
  713. */
  714. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  715. struct dentry *dentry)
  716. {
  717. struct gfs2_inode *dip = GFS2_I(dir);
  718. struct gfs2_sbd *sdp = GFS2_SB(dir);
  719. struct inode *inode = old_dentry->d_inode;
  720. struct gfs2_inode *ip = GFS2_I(inode);
  721. struct gfs2_holder ghs[2];
  722. struct buffer_head *dibh;
  723. int alloc_required;
  724. int error;
  725. if (S_ISDIR(inode->i_mode))
  726. return -EPERM;
  727. error = gfs2_rs_alloc(dip);
  728. if (error)
  729. return error;
  730. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  731. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  732. error = gfs2_glock_nq(ghs); /* parent */
  733. if (error)
  734. goto out_parent;
  735. error = gfs2_glock_nq(ghs + 1); /* child */
  736. if (error)
  737. goto out_child;
  738. error = -ENOENT;
  739. if (inode->i_nlink == 0)
  740. goto out_gunlock;
  741. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  742. if (error)
  743. goto out_gunlock;
  744. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  745. switch (error) {
  746. case -ENOENT:
  747. break;
  748. case 0:
  749. error = -EEXIST;
  750. default:
  751. goto out_gunlock;
  752. }
  753. error = -EINVAL;
  754. if (!dip->i_inode.i_nlink)
  755. goto out_gunlock;
  756. error = -EFBIG;
  757. if (dip->i_entries == (u32)-1)
  758. goto out_gunlock;
  759. error = -EPERM;
  760. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  761. goto out_gunlock;
  762. error = -EINVAL;
  763. if (!ip->i_inode.i_nlink)
  764. goto out_gunlock;
  765. error = -EMLINK;
  766. if (ip->i_inode.i_nlink == (u32)-1)
  767. goto out_gunlock;
  768. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  769. if (error < 0)
  770. goto out_gunlock;
  771. error = 0;
  772. if (alloc_required) {
  773. error = gfs2_quota_lock_check(dip);
  774. if (error)
  775. goto out_gunlock;
  776. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres, 0);
  777. if (error)
  778. goto out_gunlock_q;
  779. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  780. gfs2_rg_blocks(dip, sdp->sd_max_dirres) +
  781. 2 * RES_DINODE + RES_STATFS +
  782. RES_QUOTA, 0);
  783. if (error)
  784. goto out_ipres;
  785. } else {
  786. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  787. if (error)
  788. goto out_ipres;
  789. }
  790. error = gfs2_meta_inode_buffer(ip, &dibh);
  791. if (error)
  792. goto out_end_trans;
  793. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  794. if (error)
  795. goto out_brelse;
  796. gfs2_trans_add_meta(ip->i_gl, dibh);
  797. inc_nlink(&ip->i_inode);
  798. ip->i_inode.i_ctime = CURRENT_TIME;
  799. ihold(inode);
  800. d_instantiate(dentry, inode);
  801. mark_inode_dirty(inode);
  802. out_brelse:
  803. brelse(dibh);
  804. out_end_trans:
  805. gfs2_trans_end(sdp);
  806. out_ipres:
  807. if (alloc_required)
  808. gfs2_inplace_release(dip);
  809. out_gunlock_q:
  810. if (alloc_required)
  811. gfs2_quota_unlock(dip);
  812. out_gunlock:
  813. gfs2_glock_dq(ghs + 1);
  814. out_child:
  815. gfs2_glock_dq(ghs);
  816. out_parent:
  817. gfs2_holder_uninit(ghs);
  818. gfs2_holder_uninit(ghs + 1);
  819. return error;
  820. }
  821. /*
  822. * gfs2_unlink_ok - check to see that a inode is still in a directory
  823. * @dip: the directory
  824. * @name: the name of the file
  825. * @ip: the inode
  826. *
  827. * Assumes that the lock on (at least) @dip is held.
  828. *
  829. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  830. */
  831. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  832. const struct gfs2_inode *ip)
  833. {
  834. int error;
  835. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  836. return -EPERM;
  837. if ((dip->i_inode.i_mode & S_ISVTX) &&
  838. !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
  839. !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
  840. return -EPERM;
  841. if (IS_APPEND(&dip->i_inode))
  842. return -EPERM;
  843. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  844. if (error)
  845. return error;
  846. error = gfs2_dir_check(&dip->i_inode, name, ip);
  847. if (error)
  848. return error;
  849. return 0;
  850. }
  851. /**
  852. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  853. * @dip: The parent directory
  854. * @name: The name of the entry in the parent directory
  855. * @inode: The inode to be removed
  856. *
  857. * Called with all the locks and in a transaction. This will only be
  858. * called for a directory after it has been checked to ensure it is empty.
  859. *
  860. * Returns: 0 on success, or an error
  861. */
  862. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  863. const struct dentry *dentry)
  864. {
  865. struct inode *inode = dentry->d_inode;
  866. struct gfs2_inode *ip = GFS2_I(inode);
  867. int error;
  868. error = gfs2_dir_del(dip, dentry);
  869. if (error)
  870. return error;
  871. ip->i_entries = 0;
  872. inode->i_ctime = CURRENT_TIME;
  873. if (S_ISDIR(inode->i_mode))
  874. clear_nlink(inode);
  875. else
  876. drop_nlink(inode);
  877. mark_inode_dirty(inode);
  878. if (inode->i_nlink == 0)
  879. gfs2_unlink_di(inode);
  880. return 0;
  881. }
  882. /**
  883. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  884. * @dir: The inode of the directory containing the inode to unlink
  885. * @dentry: The file itself
  886. *
  887. * This routine uses the type of the inode as a flag to figure out
  888. * whether this is an unlink or an rmdir.
  889. *
  890. * Returns: errno
  891. */
  892. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  893. {
  894. struct gfs2_inode *dip = GFS2_I(dir);
  895. struct gfs2_sbd *sdp = GFS2_SB(dir);
  896. struct inode *inode = dentry->d_inode;
  897. struct gfs2_inode *ip = GFS2_I(inode);
  898. struct gfs2_holder ghs[3];
  899. struct gfs2_rgrpd *rgd;
  900. int error;
  901. error = gfs2_rindex_update(sdp);
  902. if (error)
  903. return error;
  904. error = -EROFS;
  905. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  906. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  907. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  908. if (!rgd)
  909. goto out_inodes;
  910. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  911. error = gfs2_glock_nq(ghs); /* parent */
  912. if (error)
  913. goto out_parent;
  914. error = gfs2_glock_nq(ghs + 1); /* child */
  915. if (error)
  916. goto out_child;
  917. error = -ENOENT;
  918. if (inode->i_nlink == 0)
  919. goto out_rgrp;
  920. if (S_ISDIR(inode->i_mode)) {
  921. error = -ENOTEMPTY;
  922. if (ip->i_entries > 2 || inode->i_nlink > 2)
  923. goto out_rgrp;
  924. }
  925. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  926. if (error)
  927. goto out_rgrp;
  928. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  929. if (error)
  930. goto out_gunlock;
  931. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  932. if (error)
  933. goto out_end_trans;
  934. error = gfs2_unlink_inode(dip, dentry);
  935. out_end_trans:
  936. gfs2_trans_end(sdp);
  937. out_gunlock:
  938. gfs2_glock_dq(ghs + 2);
  939. out_rgrp:
  940. gfs2_glock_dq(ghs + 1);
  941. out_child:
  942. gfs2_glock_dq(ghs);
  943. out_parent:
  944. gfs2_holder_uninit(ghs + 2);
  945. out_inodes:
  946. gfs2_holder_uninit(ghs + 1);
  947. gfs2_holder_uninit(ghs);
  948. return error;
  949. }
  950. /**
  951. * gfs2_symlink - Create a symlink
  952. * @dir: The directory to create the symlink in
  953. * @dentry: The dentry to put the symlink in
  954. * @symname: The thing which the link points to
  955. *
  956. * Returns: errno
  957. */
  958. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  959. const char *symname)
  960. {
  961. struct gfs2_sbd *sdp = GFS2_SB(dir);
  962. unsigned int size;
  963. size = strlen(symname);
  964. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  965. return -ENAMETOOLONG;
  966. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
  967. }
  968. /**
  969. * gfs2_mkdir - Make a directory
  970. * @dir: The parent directory of the new one
  971. * @dentry: The dentry of the new directory
  972. * @mode: The mode of the new directory
  973. *
  974. * Returns: errno
  975. */
  976. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  977. {
  978. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0, 0);
  979. }
  980. /**
  981. * gfs2_mknod - Make a special file
  982. * @dir: The directory in which the special file will reside
  983. * @dentry: The dentry of the special file
  984. * @mode: The mode of the special file
  985. * @dev: The device specification of the special file
  986. *
  987. */
  988. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  989. dev_t dev)
  990. {
  991. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0, 0);
  992. }
  993. /*
  994. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  995. * @this: move this
  996. * @to: to here
  997. *
  998. * Follow @to back to the root and make sure we don't encounter @this
  999. * Assumes we already hold the rename lock.
  1000. *
  1001. * Returns: errno
  1002. */
  1003. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1004. {
  1005. struct inode *dir = &to->i_inode;
  1006. struct super_block *sb = dir->i_sb;
  1007. struct inode *tmp;
  1008. int error = 0;
  1009. igrab(dir);
  1010. for (;;) {
  1011. if (dir == &this->i_inode) {
  1012. error = -EINVAL;
  1013. break;
  1014. }
  1015. if (dir == sb->s_root->d_inode) {
  1016. error = 0;
  1017. break;
  1018. }
  1019. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1020. if (IS_ERR(tmp)) {
  1021. error = PTR_ERR(tmp);
  1022. break;
  1023. }
  1024. iput(dir);
  1025. dir = tmp;
  1026. }
  1027. iput(dir);
  1028. return error;
  1029. }
  1030. /**
  1031. * gfs2_rename - Rename a file
  1032. * @odir: Parent directory of old file name
  1033. * @odentry: The old dentry of the file
  1034. * @ndir: Parent directory of new file name
  1035. * @ndentry: The new dentry of the file
  1036. *
  1037. * Returns: errno
  1038. */
  1039. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1040. struct inode *ndir, struct dentry *ndentry)
  1041. {
  1042. struct gfs2_inode *odip = GFS2_I(odir);
  1043. struct gfs2_inode *ndip = GFS2_I(ndir);
  1044. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1045. struct gfs2_inode *nip = NULL;
  1046. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1047. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  1048. struct gfs2_rgrpd *nrgd;
  1049. unsigned int num_gh;
  1050. int dir_rename = 0;
  1051. int alloc_required = 0;
  1052. unsigned int x;
  1053. int error;
  1054. if (ndentry->d_inode) {
  1055. nip = GFS2_I(ndentry->d_inode);
  1056. if (ip == nip)
  1057. return 0;
  1058. }
  1059. error = gfs2_rindex_update(sdp);
  1060. if (error)
  1061. return error;
  1062. error = gfs2_rs_alloc(ndip);
  1063. if (error)
  1064. return error;
  1065. if (odip != ndip) {
  1066. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1067. 0, &r_gh);
  1068. if (error)
  1069. goto out;
  1070. if (S_ISDIR(ip->i_inode.i_mode)) {
  1071. dir_rename = 1;
  1072. /* don't move a dirctory into it's subdir */
  1073. error = gfs2_ok_to_move(ip, ndip);
  1074. if (error)
  1075. goto out_gunlock_r;
  1076. }
  1077. }
  1078. num_gh = 1;
  1079. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1080. if (odip != ndip) {
  1081. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1082. num_gh++;
  1083. }
  1084. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1085. num_gh++;
  1086. if (nip) {
  1087. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1088. num_gh++;
  1089. /* grab the resource lock for unlink flag twiddling
  1090. * this is the case of the target file already existing
  1091. * so we unlink before doing the rename
  1092. */
  1093. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1094. if (nrgd)
  1095. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1096. }
  1097. for (x = 0; x < num_gh; x++) {
  1098. error = gfs2_glock_nq(ghs + x);
  1099. if (error)
  1100. goto out_gunlock;
  1101. }
  1102. error = -ENOENT;
  1103. if (ip->i_inode.i_nlink == 0)
  1104. goto out_gunlock;
  1105. /* Check out the old directory */
  1106. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1107. if (error)
  1108. goto out_gunlock;
  1109. /* Check out the new directory */
  1110. if (nip) {
  1111. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1112. if (error)
  1113. goto out_gunlock;
  1114. if (nip->i_inode.i_nlink == 0) {
  1115. error = -EAGAIN;
  1116. goto out_gunlock;
  1117. }
  1118. if (S_ISDIR(nip->i_inode.i_mode)) {
  1119. if (nip->i_entries < 2) {
  1120. gfs2_consist_inode(nip);
  1121. error = -EIO;
  1122. goto out_gunlock;
  1123. }
  1124. if (nip->i_entries > 2) {
  1125. error = -ENOTEMPTY;
  1126. goto out_gunlock;
  1127. }
  1128. }
  1129. } else {
  1130. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1131. if (error)
  1132. goto out_gunlock;
  1133. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1134. switch (error) {
  1135. case -ENOENT:
  1136. error = 0;
  1137. break;
  1138. case 0:
  1139. error = -EEXIST;
  1140. default:
  1141. goto out_gunlock;
  1142. };
  1143. if (odip != ndip) {
  1144. if (!ndip->i_inode.i_nlink) {
  1145. error = -ENOENT;
  1146. goto out_gunlock;
  1147. }
  1148. if (ndip->i_entries == (u32)-1) {
  1149. error = -EFBIG;
  1150. goto out_gunlock;
  1151. }
  1152. if (S_ISDIR(ip->i_inode.i_mode) &&
  1153. ndip->i_inode.i_nlink == (u32)-1) {
  1154. error = -EMLINK;
  1155. goto out_gunlock;
  1156. }
  1157. }
  1158. }
  1159. /* Check out the dir to be renamed */
  1160. if (dir_rename) {
  1161. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1162. if (error)
  1163. goto out_gunlock;
  1164. }
  1165. if (nip == NULL)
  1166. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1167. error = alloc_required;
  1168. if (error < 0)
  1169. goto out_gunlock;
  1170. if (alloc_required) {
  1171. error = gfs2_quota_lock_check(ndip);
  1172. if (error)
  1173. goto out_gunlock;
  1174. error = gfs2_inplace_reserve(ndip, sdp->sd_max_dirres, 0);
  1175. if (error)
  1176. goto out_gunlock_q;
  1177. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1178. gfs2_rg_blocks(ndip, sdp->sd_max_dirres) +
  1179. 4 * RES_DINODE + 4 * RES_LEAF +
  1180. RES_STATFS + RES_QUOTA + 4, 0);
  1181. if (error)
  1182. goto out_ipreserv;
  1183. } else {
  1184. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1185. 5 * RES_LEAF + 4, 0);
  1186. if (error)
  1187. goto out_gunlock;
  1188. }
  1189. /* Remove the target file, if it exists */
  1190. if (nip)
  1191. error = gfs2_unlink_inode(ndip, ndentry);
  1192. if (dir_rename) {
  1193. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1194. if (error)
  1195. goto out_end_trans;
  1196. } else {
  1197. struct buffer_head *dibh;
  1198. error = gfs2_meta_inode_buffer(ip, &dibh);
  1199. if (error)
  1200. goto out_end_trans;
  1201. ip->i_inode.i_ctime = CURRENT_TIME;
  1202. gfs2_trans_add_meta(ip->i_gl, dibh);
  1203. gfs2_dinode_out(ip, dibh->b_data);
  1204. brelse(dibh);
  1205. }
  1206. error = gfs2_dir_del(odip, odentry);
  1207. if (error)
  1208. goto out_end_trans;
  1209. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1210. if (error)
  1211. goto out_end_trans;
  1212. out_end_trans:
  1213. gfs2_trans_end(sdp);
  1214. out_ipreserv:
  1215. if (alloc_required)
  1216. gfs2_inplace_release(ndip);
  1217. out_gunlock_q:
  1218. if (alloc_required)
  1219. gfs2_quota_unlock(ndip);
  1220. out_gunlock:
  1221. while (x--) {
  1222. gfs2_glock_dq(ghs + x);
  1223. gfs2_holder_uninit(ghs + x);
  1224. }
  1225. out_gunlock_r:
  1226. if (r_gh.gh_gl)
  1227. gfs2_glock_dq_uninit(&r_gh);
  1228. out:
  1229. return error;
  1230. }
  1231. /**
  1232. * gfs2_follow_link - Follow a symbolic link
  1233. * @dentry: The dentry of the link
  1234. * @nd: Data that we pass to vfs_follow_link()
  1235. *
  1236. * This can handle symlinks of any size.
  1237. *
  1238. * Returns: 0 on success or error code
  1239. */
  1240. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1241. {
  1242. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1243. struct gfs2_holder i_gh;
  1244. struct buffer_head *dibh;
  1245. unsigned int size;
  1246. char *buf;
  1247. int error;
  1248. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1249. error = gfs2_glock_nq(&i_gh);
  1250. if (error) {
  1251. gfs2_holder_uninit(&i_gh);
  1252. nd_set_link(nd, ERR_PTR(error));
  1253. return NULL;
  1254. }
  1255. size = (unsigned int)i_size_read(&ip->i_inode);
  1256. if (size == 0) {
  1257. gfs2_consist_inode(ip);
  1258. buf = ERR_PTR(-EIO);
  1259. goto out;
  1260. }
  1261. error = gfs2_meta_inode_buffer(ip, &dibh);
  1262. if (error) {
  1263. buf = ERR_PTR(error);
  1264. goto out;
  1265. }
  1266. buf = kzalloc(size + 1, GFP_NOFS);
  1267. if (!buf)
  1268. buf = ERR_PTR(-ENOMEM);
  1269. else
  1270. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1271. brelse(dibh);
  1272. out:
  1273. gfs2_glock_dq_uninit(&i_gh);
  1274. nd_set_link(nd, buf);
  1275. return NULL;
  1276. }
  1277. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1278. {
  1279. char *s = nd_get_link(nd);
  1280. if (!IS_ERR(s))
  1281. kfree(s);
  1282. }
  1283. /**
  1284. * gfs2_permission -
  1285. * @inode: The inode
  1286. * @mask: The mask to be tested
  1287. * @flags: Indicates whether this is an RCU path walk or not
  1288. *
  1289. * This may be called from the VFS directly, or from within GFS2 with the
  1290. * inode locked, so we look to see if the glock is already locked and only
  1291. * lock the glock if its not already been done.
  1292. *
  1293. * Returns: errno
  1294. */
  1295. int gfs2_permission(struct inode *inode, int mask)
  1296. {
  1297. struct gfs2_inode *ip;
  1298. struct gfs2_holder i_gh;
  1299. int error;
  1300. int unlock = 0;
  1301. ip = GFS2_I(inode);
  1302. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1303. if (mask & MAY_NOT_BLOCK)
  1304. return -ECHILD;
  1305. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1306. if (error)
  1307. return error;
  1308. unlock = 1;
  1309. }
  1310. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1311. error = -EACCES;
  1312. else
  1313. error = generic_permission(inode, mask);
  1314. if (unlock)
  1315. gfs2_glock_dq_uninit(&i_gh);
  1316. return error;
  1317. }
  1318. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1319. {
  1320. setattr_copy(inode, attr);
  1321. mark_inode_dirty(inode);
  1322. return 0;
  1323. }
  1324. /**
  1325. * gfs2_setattr_simple -
  1326. * @ip:
  1327. * @attr:
  1328. *
  1329. * Returns: errno
  1330. */
  1331. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1332. {
  1333. int error;
  1334. if (current->journal_info)
  1335. return __gfs2_setattr_simple(inode, attr);
  1336. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1337. if (error)
  1338. return error;
  1339. error = __gfs2_setattr_simple(inode, attr);
  1340. gfs2_trans_end(GFS2_SB(inode));
  1341. return error;
  1342. }
  1343. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1344. {
  1345. struct gfs2_inode *ip = GFS2_I(inode);
  1346. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1347. kuid_t ouid, nuid;
  1348. kgid_t ogid, ngid;
  1349. int error;
  1350. ouid = inode->i_uid;
  1351. ogid = inode->i_gid;
  1352. nuid = attr->ia_uid;
  1353. ngid = attr->ia_gid;
  1354. if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
  1355. ouid = nuid = NO_UID_QUOTA_CHANGE;
  1356. if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
  1357. ogid = ngid = NO_GID_QUOTA_CHANGE;
  1358. error = gfs2_quota_lock(ip, nuid, ngid);
  1359. if (error)
  1360. return error;
  1361. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1362. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1363. error = gfs2_quota_check(ip, nuid, ngid);
  1364. if (error)
  1365. goto out_gunlock_q;
  1366. }
  1367. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1368. if (error)
  1369. goto out_gunlock_q;
  1370. error = gfs2_setattr_simple(inode, attr);
  1371. if (error)
  1372. goto out_end_trans;
  1373. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1374. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1375. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1376. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1377. gfs2_quota_change(ip, blocks, nuid, ngid);
  1378. }
  1379. out_end_trans:
  1380. gfs2_trans_end(sdp);
  1381. out_gunlock_q:
  1382. gfs2_quota_unlock(ip);
  1383. return error;
  1384. }
  1385. /**
  1386. * gfs2_setattr - Change attributes on an inode
  1387. * @dentry: The dentry which is changing
  1388. * @attr: The structure describing the change
  1389. *
  1390. * The VFS layer wants to change one or more of an inodes attributes. Write
  1391. * that change out to disk.
  1392. *
  1393. * Returns: errno
  1394. */
  1395. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1396. {
  1397. struct inode *inode = dentry->d_inode;
  1398. struct gfs2_inode *ip = GFS2_I(inode);
  1399. struct gfs2_holder i_gh;
  1400. int error;
  1401. error = gfs2_rs_alloc(ip);
  1402. if (error)
  1403. return error;
  1404. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1405. if (error)
  1406. return error;
  1407. error = -EPERM;
  1408. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1409. goto out;
  1410. error = inode_change_ok(inode, attr);
  1411. if (error)
  1412. goto out;
  1413. if (attr->ia_valid & ATTR_SIZE)
  1414. error = gfs2_setattr_size(inode, attr->ia_size);
  1415. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1416. error = setattr_chown(inode, attr);
  1417. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1418. error = gfs2_acl_chmod(ip, attr);
  1419. else
  1420. error = gfs2_setattr_simple(inode, attr);
  1421. out:
  1422. if (!error)
  1423. mark_inode_dirty(inode);
  1424. gfs2_glock_dq_uninit(&i_gh);
  1425. return error;
  1426. }
  1427. /**
  1428. * gfs2_getattr - Read out an inode's attributes
  1429. * @mnt: The vfsmount the inode is being accessed from
  1430. * @dentry: The dentry to stat
  1431. * @stat: The inode's stats
  1432. *
  1433. * This may be called from the VFS directly, or from within GFS2 with the
  1434. * inode locked, so we look to see if the glock is already locked and only
  1435. * lock the glock if its not already been done. Note that its the NFS
  1436. * readdirplus operation which causes this to be called (from filldir)
  1437. * with the glock already held.
  1438. *
  1439. * Returns: errno
  1440. */
  1441. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1442. struct kstat *stat)
  1443. {
  1444. struct inode *inode = dentry->d_inode;
  1445. struct gfs2_inode *ip = GFS2_I(inode);
  1446. struct gfs2_holder gh;
  1447. int error;
  1448. int unlock = 0;
  1449. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1450. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1451. if (error)
  1452. return error;
  1453. unlock = 1;
  1454. }
  1455. generic_fillattr(inode, stat);
  1456. if (unlock)
  1457. gfs2_glock_dq_uninit(&gh);
  1458. return 0;
  1459. }
  1460. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1461. const void *data, size_t size, int flags)
  1462. {
  1463. struct inode *inode = dentry->d_inode;
  1464. struct gfs2_inode *ip = GFS2_I(inode);
  1465. struct gfs2_holder gh;
  1466. int ret;
  1467. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1468. ret = gfs2_glock_nq(&gh);
  1469. if (ret == 0) {
  1470. ret = gfs2_rs_alloc(ip);
  1471. if (ret == 0)
  1472. ret = generic_setxattr(dentry, name, data, size, flags);
  1473. gfs2_glock_dq(&gh);
  1474. }
  1475. gfs2_holder_uninit(&gh);
  1476. return ret;
  1477. }
  1478. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1479. void *data, size_t size)
  1480. {
  1481. struct inode *inode = dentry->d_inode;
  1482. struct gfs2_inode *ip = GFS2_I(inode);
  1483. struct gfs2_holder gh;
  1484. int ret;
  1485. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1486. ret = gfs2_glock_nq(&gh);
  1487. if (ret == 0) {
  1488. ret = generic_getxattr(dentry, name, data, size);
  1489. gfs2_glock_dq(&gh);
  1490. }
  1491. gfs2_holder_uninit(&gh);
  1492. return ret;
  1493. }
  1494. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1495. {
  1496. struct inode *inode = dentry->d_inode;
  1497. struct gfs2_inode *ip = GFS2_I(inode);
  1498. struct gfs2_holder gh;
  1499. int ret;
  1500. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1501. ret = gfs2_glock_nq(&gh);
  1502. if (ret == 0) {
  1503. ret = gfs2_rs_alloc(ip);
  1504. if (ret == 0)
  1505. ret = generic_removexattr(dentry, name);
  1506. gfs2_glock_dq(&gh);
  1507. }
  1508. gfs2_holder_uninit(&gh);
  1509. return ret;
  1510. }
  1511. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1512. u64 start, u64 len)
  1513. {
  1514. struct gfs2_inode *ip = GFS2_I(inode);
  1515. struct gfs2_holder gh;
  1516. int ret;
  1517. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1518. if (ret)
  1519. return ret;
  1520. mutex_lock(&inode->i_mutex);
  1521. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1522. if (ret)
  1523. goto out;
  1524. if (gfs2_is_stuffed(ip)) {
  1525. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1526. u64 size = i_size_read(inode);
  1527. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1528. FIEMAP_EXTENT_DATA_INLINE;
  1529. phys += sizeof(struct gfs2_dinode);
  1530. phys += start;
  1531. if (start + len > size)
  1532. len = size - start;
  1533. if (start < size)
  1534. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1535. len, flags);
  1536. if (ret == 1)
  1537. ret = 0;
  1538. } else {
  1539. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1540. gfs2_block_map);
  1541. }
  1542. gfs2_glock_dq_uninit(&gh);
  1543. out:
  1544. mutex_unlock(&inode->i_mutex);
  1545. return ret;
  1546. }
  1547. const struct inode_operations gfs2_file_iops = {
  1548. .permission = gfs2_permission,
  1549. .setattr = gfs2_setattr,
  1550. .getattr = gfs2_getattr,
  1551. .setxattr = gfs2_setxattr,
  1552. .getxattr = gfs2_getxattr,
  1553. .listxattr = gfs2_listxattr,
  1554. .removexattr = gfs2_removexattr,
  1555. .fiemap = gfs2_fiemap,
  1556. .get_acl = gfs2_get_acl,
  1557. };
  1558. const struct inode_operations gfs2_dir_iops = {
  1559. .create = gfs2_create,
  1560. .lookup = gfs2_lookup,
  1561. .link = gfs2_link,
  1562. .unlink = gfs2_unlink,
  1563. .symlink = gfs2_symlink,
  1564. .mkdir = gfs2_mkdir,
  1565. .rmdir = gfs2_unlink,
  1566. .mknod = gfs2_mknod,
  1567. .rename = gfs2_rename,
  1568. .permission = gfs2_permission,
  1569. .setattr = gfs2_setattr,
  1570. .getattr = gfs2_getattr,
  1571. .setxattr = gfs2_setxattr,
  1572. .getxattr = gfs2_getxattr,
  1573. .listxattr = gfs2_listxattr,
  1574. .removexattr = gfs2_removexattr,
  1575. .fiemap = gfs2_fiemap,
  1576. .get_acl = gfs2_get_acl,
  1577. };
  1578. const struct inode_operations gfs2_symlink_iops = {
  1579. .readlink = generic_readlink,
  1580. .follow_link = gfs2_follow_link,
  1581. .put_link = gfs2_put_link,
  1582. .permission = gfs2_permission,
  1583. .setattr = gfs2_setattr,
  1584. .getattr = gfs2_getattr,
  1585. .setxattr = gfs2_setxattr,
  1586. .getxattr = gfs2_getxattr,
  1587. .listxattr = gfs2_listxattr,
  1588. .removexattr = gfs2_removexattr,
  1589. .fiemap = gfs2_fiemap,
  1590. .get_acl = gfs2_get_acl,
  1591. };