inode.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/posix_acl.h>
  15. #include <linux/sort.h>
  16. #include <asm/semaphore.h>
  17. #include "gfs2.h"
  18. #include "acl.h"
  19. #include "bmap.h"
  20. #include "dir.h"
  21. #include "eattr.h"
  22. #include "glock.h"
  23. #include "glops.h"
  24. #include "inode.h"
  25. #include "log.h"
  26. #include "meta_io.h"
  27. #include "ops_address.h"
  28. #include "ops_file.h"
  29. #include "ops_inode.h"
  30. #include "quota.h"
  31. #include "rgrp.h"
  32. #include "trans.h"
  33. #include "unlinked.h"
  34. /**
  35. * inode_attr_in - Copy attributes from the dinode into the VFS inode
  36. * @ip: The GFS2 inode (with embedded disk inode data)
  37. * @inode: The Linux VFS inode
  38. *
  39. */
  40. static void inode_attr_in(struct gfs2_inode *ip, struct inode *inode)
  41. {
  42. inode->i_ino = ip->i_num.no_formal_ino;
  43. switch (ip->i_di.di_mode & S_IFMT) {
  44. case S_IFBLK:
  45. case S_IFCHR:
  46. inode->i_rdev = MKDEV(ip->i_di.di_major, ip->i_di.di_minor);
  47. break;
  48. default:
  49. inode->i_rdev = 0;
  50. break;
  51. };
  52. inode->i_mode = ip->i_di.di_mode;
  53. inode->i_nlink = ip->i_di.di_nlink;
  54. inode->i_uid = ip->i_di.di_uid;
  55. inode->i_gid = ip->i_di.di_gid;
  56. i_size_write(inode, ip->i_di.di_size);
  57. inode->i_atime.tv_sec = ip->i_di.di_atime;
  58. inode->i_mtime.tv_sec = ip->i_di.di_mtime;
  59. inode->i_ctime.tv_sec = ip->i_di.di_ctime;
  60. inode->i_atime.tv_nsec = 0;
  61. inode->i_mtime.tv_nsec = 0;
  62. inode->i_ctime.tv_nsec = 0;
  63. inode->i_blksize = PAGE_SIZE;
  64. inode->i_blocks = ip->i_di.di_blocks <<
  65. (ip->i_sbd->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
  66. if (ip->i_di.di_flags & GFS2_DIF_IMMUTABLE)
  67. inode->i_flags |= S_IMMUTABLE;
  68. else
  69. inode->i_flags &= ~S_IMMUTABLE;
  70. if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY)
  71. inode->i_flags |= S_APPEND;
  72. else
  73. inode->i_flags &= ~S_APPEND;
  74. }
  75. /**
  76. * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
  77. * @ip: The GFS2 inode (with embedded disk inode data)
  78. *
  79. */
  80. void gfs2_inode_attr_in(struct gfs2_inode *ip)
  81. {
  82. struct inode *inode;
  83. inode = gfs2_ip2v_lookup(ip);
  84. if (inode) {
  85. inode_attr_in(ip, inode);
  86. iput(inode);
  87. }
  88. }
  89. /**
  90. * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
  91. * @ip: The GFS2 inode
  92. *
  93. * Only copy out the attributes that we want the VFS layer
  94. * to be able to modify.
  95. */
  96. void gfs2_inode_attr_out(struct gfs2_inode *ip)
  97. {
  98. struct inode *inode = ip->i_vnode;
  99. gfs2_assert_withdraw(ip->i_sbd,
  100. (ip->i_di.di_mode & S_IFMT) == (inode->i_mode & S_IFMT));
  101. ip->i_di.di_mode = inode->i_mode;
  102. ip->i_di.di_uid = inode->i_uid;
  103. ip->i_di.di_gid = inode->i_gid;
  104. ip->i_di.di_atime = inode->i_atime.tv_sec;
  105. ip->i_di.di_mtime = inode->i_mtime.tv_sec;
  106. ip->i_di.di_ctime = inode->i_ctime.tv_sec;
  107. }
  108. /**
  109. * gfs2_ip2v_lookup - Get the struct inode for a struct gfs2_inode
  110. * @ip: the struct gfs2_inode to get the struct inode for
  111. *
  112. * Returns: A VFS inode, or NULL if none
  113. */
  114. struct inode *gfs2_ip2v_lookup(struct gfs2_inode *ip)
  115. {
  116. struct inode *inode = NULL;
  117. gfs2_assert_warn(ip->i_sbd, test_bit(GIF_MIN_INIT, &ip->i_flags));
  118. spin_lock(&ip->i_spin);
  119. if (ip->i_vnode)
  120. inode = igrab(ip->i_vnode);
  121. spin_unlock(&ip->i_spin);
  122. return inode;
  123. }
  124. /**
  125. * gfs2_ip2v - Get/Create a struct inode for a struct gfs2_inode
  126. * @ip: the struct gfs2_inode to get the struct inode for
  127. *
  128. * Returns: A VFS inode, or NULL if no mem
  129. */
  130. struct inode *gfs2_ip2v(struct gfs2_inode *ip)
  131. {
  132. struct inode *inode, *tmp;
  133. inode = gfs2_ip2v_lookup(ip);
  134. if (inode)
  135. return inode;
  136. tmp = new_inode(ip->i_sbd->sd_vfs);
  137. if (!tmp)
  138. return NULL;
  139. inode_attr_in(ip, tmp);
  140. if (S_ISREG(ip->i_di.di_mode)) {
  141. tmp->i_op = &gfs2_file_iops;
  142. tmp->i_fop = &gfs2_file_fops;
  143. tmp->i_mapping->a_ops = &gfs2_file_aops;
  144. } else if (S_ISDIR(ip->i_di.di_mode)) {
  145. tmp->i_op = &gfs2_dir_iops;
  146. tmp->i_fop = &gfs2_dir_fops;
  147. } else if (S_ISLNK(ip->i_di.di_mode)) {
  148. tmp->i_op = &gfs2_symlink_iops;
  149. } else {
  150. tmp->i_op = &gfs2_dev_iops;
  151. init_special_inode(tmp, tmp->i_mode, tmp->i_rdev);
  152. }
  153. set_v2ip(tmp, NULL);
  154. for (;;) {
  155. spin_lock(&ip->i_spin);
  156. if (!ip->i_vnode)
  157. break;
  158. inode = igrab(ip->i_vnode);
  159. spin_unlock(&ip->i_spin);
  160. if (inode) {
  161. iput(tmp);
  162. return inode;
  163. }
  164. yield();
  165. }
  166. inode = tmp;
  167. gfs2_inode_hold(ip);
  168. ip->i_vnode = inode;
  169. set_v2ip(inode, ip);
  170. spin_unlock(&ip->i_spin);
  171. insert_inode_hash(inode);
  172. return inode;
  173. }
  174. static int iget_test(struct inode *inode, void *opaque)
  175. {
  176. struct gfs2_inode *ip = get_v2ip(inode);
  177. struct gfs2_inum *inum = (struct gfs2_inum *)opaque;
  178. if (ip && ip->i_num.no_addr == inum->no_addr)
  179. return 1;
  180. return 0;
  181. }
  182. struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
  183. {
  184. return ilookup5(sb, (unsigned long)inum->no_formal_ino,
  185. iget_test, inum);
  186. }
  187. void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type)
  188. {
  189. spin_lock(&ip->i_spin);
  190. if (!test_and_set_bit(GIF_MIN_INIT, &ip->i_flags)) {
  191. ip->i_di.di_nlink = 1;
  192. ip->i_di.di_mode = DT2IF(type);
  193. }
  194. spin_unlock(&ip->i_spin);
  195. }
  196. /**
  197. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  198. * @ip: The GFS2 inode
  199. *
  200. * Returns: errno
  201. */
  202. int gfs2_inode_refresh(struct gfs2_inode *ip)
  203. {
  204. struct buffer_head *dibh;
  205. int error;
  206. error = gfs2_meta_inode_buffer(ip, &dibh);
  207. if (error)
  208. return error;
  209. if (gfs2_metatype_check(ip->i_sbd, dibh, GFS2_METATYPE_DI)) {
  210. brelse(dibh);
  211. return -EIO;
  212. }
  213. spin_lock(&ip->i_spin);
  214. gfs2_dinode_in(&ip->i_di, dibh->b_data);
  215. set_bit(GIF_MIN_INIT, &ip->i_flags);
  216. spin_unlock(&ip->i_spin);
  217. brelse(dibh);
  218. if (ip->i_num.no_addr != ip->i_di.di_num.no_addr) {
  219. if (gfs2_consist_inode(ip))
  220. gfs2_dinode_print(&ip->i_di);
  221. return -EIO;
  222. }
  223. if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino)
  224. return -ESTALE;
  225. ip->i_vn = ip->i_gl->gl_vn;
  226. return 0;
  227. }
  228. /**
  229. * inode_create - create a struct gfs2_inode
  230. * @i_gl: The glock covering the inode
  231. * @inum: The inode number
  232. * @io_gl: the iopen glock to acquire/hold (using holder in new gfs2_inode)
  233. * @io_state: the state the iopen glock should be acquired in
  234. * @ipp: pointer to put the returned inode in
  235. *
  236. * Returns: errno
  237. */
  238. static int inode_create(struct gfs2_glock *i_gl, struct gfs2_inum *inum,
  239. struct gfs2_glock *io_gl, unsigned int io_state,
  240. struct gfs2_inode **ipp)
  241. {
  242. struct gfs2_sbd *sdp = i_gl->gl_sbd;
  243. struct gfs2_inode *ip;
  244. int error = 0;
  245. ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
  246. if (!ip)
  247. return -ENOMEM;
  248. memset(ip, 0, sizeof(struct gfs2_inode));
  249. ip->i_num = *inum;
  250. atomic_set(&ip->i_count, 1);
  251. ip->i_vn = i_gl->gl_vn - 1;
  252. ip->i_gl = i_gl;
  253. ip->i_sbd = sdp;
  254. spin_lock_init(&ip->i_spin);
  255. init_rwsem(&ip->i_rw_mutex);
  256. ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
  257. error = gfs2_glock_nq_init(io_gl,
  258. io_state, GL_LOCAL_EXCL | GL_EXACT,
  259. &ip->i_iopen_gh);
  260. if (error)
  261. goto fail;
  262. ip->i_iopen_gh.gh_owner = NULL;
  263. spin_lock(&io_gl->gl_spin);
  264. gfs2_glock_hold(i_gl);
  265. set_gl2gl(io_gl, i_gl);
  266. spin_unlock(&io_gl->gl_spin);
  267. gfs2_glock_hold(i_gl);
  268. set_gl2ip(i_gl, ip);
  269. atomic_inc(&sdp->sd_inode_count);
  270. *ipp = ip;
  271. return 0;
  272. fail:
  273. gfs2_meta_cache_flush(ip);
  274. kmem_cache_free(gfs2_inode_cachep, ip);
  275. *ipp = NULL;
  276. return error;
  277. }
  278. /**
  279. * gfs2_inode_get - Create or get a reference on an inode
  280. * @i_gl: The glock covering the inode
  281. * @inum: The inode number
  282. * @create:
  283. * @ipp: pointer to put the returned inode in
  284. *
  285. * Returns: errno
  286. */
  287. int gfs2_inode_get(struct gfs2_glock *i_gl, struct gfs2_inum *inum, int create,
  288. struct gfs2_inode **ipp)
  289. {
  290. struct gfs2_sbd *sdp = i_gl->gl_sbd;
  291. struct gfs2_glock *io_gl;
  292. int error = 0;
  293. gfs2_glmutex_lock(i_gl);
  294. *ipp = get_gl2ip(i_gl);
  295. if (*ipp) {
  296. error = -ESTALE;
  297. if ((*ipp)->i_num.no_formal_ino != inum->no_formal_ino)
  298. goto out;
  299. atomic_inc(&(*ipp)->i_count);
  300. error = 0;
  301. goto out;
  302. }
  303. if (!create)
  304. goto out;
  305. error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops,
  306. CREATE, &io_gl);
  307. if (!error) {
  308. error = inode_create(i_gl, inum, io_gl, LM_ST_SHARED, ipp);
  309. gfs2_glock_put(io_gl);
  310. }
  311. out:
  312. gfs2_glmutex_unlock(i_gl);
  313. return error;
  314. }
  315. void gfs2_inode_hold(struct gfs2_inode *ip)
  316. {
  317. gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
  318. atomic_inc(&ip->i_count);
  319. }
  320. void gfs2_inode_put(struct gfs2_inode *ip)
  321. {
  322. gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
  323. atomic_dec(&ip->i_count);
  324. }
  325. void gfs2_inode_destroy(struct gfs2_inode *ip)
  326. {
  327. struct gfs2_sbd *sdp = ip->i_sbd;
  328. struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
  329. struct gfs2_glock *i_gl = ip->i_gl;
  330. gfs2_assert_warn(sdp, !atomic_read(&ip->i_count));
  331. gfs2_assert(sdp, get_gl2gl(io_gl) == i_gl);
  332. spin_lock(&io_gl->gl_spin);
  333. set_gl2gl(io_gl, NULL);
  334. gfs2_glock_put(i_gl);
  335. spin_unlock(&io_gl->gl_spin);
  336. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  337. gfs2_meta_cache_flush(ip);
  338. kmem_cache_free(gfs2_inode_cachep, ip);
  339. set_gl2ip(i_gl, NULL);
  340. gfs2_glock_put(i_gl);
  341. atomic_dec(&sdp->sd_inode_count);
  342. }
  343. static int dinode_dealloc(struct gfs2_inode *ip, struct gfs2_unlinked *ul)
  344. {
  345. struct gfs2_sbd *sdp = ip->i_sbd;
  346. struct gfs2_alloc *al;
  347. struct gfs2_rgrpd *rgd;
  348. int error;
  349. if (ip->i_di.di_blocks != 1) {
  350. if (gfs2_consist_inode(ip))
  351. gfs2_dinode_print(&ip->i_di);
  352. return -EIO;
  353. }
  354. al = gfs2_alloc_get(ip);
  355. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  356. if (error)
  357. goto out;
  358. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  359. if (error)
  360. goto out_qs;
  361. rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
  362. if (!rgd) {
  363. gfs2_consist_inode(ip);
  364. error = -EIO;
  365. goto out_rindex_relse;
  366. }
  367. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  368. &al->al_rgd_gh);
  369. if (error)
  370. goto out_rindex_relse;
  371. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
  372. RES_STATFS + RES_QUOTA, 1);
  373. if (error)
  374. goto out_rg_gunlock;
  375. gfs2_trans_add_gl(ip->i_gl);
  376. gfs2_free_di(rgd, ip);
  377. error = gfs2_unlinked_ondisk_rm(sdp, ul);
  378. gfs2_trans_end(sdp);
  379. clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  380. out_rg_gunlock:
  381. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  382. out_rindex_relse:
  383. gfs2_glock_dq_uninit(&al->al_ri_gh);
  384. out_qs:
  385. gfs2_quota_unhold(ip);
  386. out:
  387. gfs2_alloc_put(ip);
  388. return error;
  389. }
  390. /**
  391. * inode_dealloc - Deallocate all on-disk blocks for an inode (dinode)
  392. * @sdp: the filesystem
  393. * @inum: the inode number to deallocate
  394. * @io_gh: a holder for the iopen glock for this inode
  395. *
  396. * Returns: errno
  397. */
  398. static int inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul,
  399. struct gfs2_holder *io_gh)
  400. {
  401. struct gfs2_inode *ip;
  402. struct gfs2_holder i_gh;
  403. int error;
  404. error = gfs2_glock_nq_num(sdp,
  405. ul->ul_ut.ut_inum.no_addr, &gfs2_inode_glops,
  406. LM_ST_EXCLUSIVE, 0, &i_gh);
  407. if (error)
  408. return error;
  409. /* We reacquire the iopen lock here to avoid a race with the NFS server
  410. calling gfs2_read_inode() with the inode number of a inode we're in
  411. the process of deallocating. And we can't keep our hold on the lock
  412. from inode_dealloc_init() for deadlock reasons. */
  413. gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY, io_gh);
  414. error = gfs2_glock_nq(io_gh);
  415. switch (error) {
  416. case 0:
  417. break;
  418. case GLR_TRYFAILED:
  419. error = 1;
  420. default:
  421. goto out;
  422. }
  423. gfs2_assert_warn(sdp, !get_gl2ip(i_gh.gh_gl));
  424. error = inode_create(i_gh.gh_gl, &ul->ul_ut.ut_inum, io_gh->gh_gl,
  425. LM_ST_EXCLUSIVE, &ip);
  426. gfs2_glock_dq(io_gh);
  427. if (error)
  428. goto out;
  429. error = gfs2_inode_refresh(ip);
  430. if (error)
  431. goto out_iput;
  432. if (ip->i_di.di_nlink) {
  433. if (gfs2_consist_inode(ip))
  434. gfs2_dinode_print(&ip->i_di);
  435. error = -EIO;
  436. goto out_iput;
  437. }
  438. if (S_ISDIR(ip->i_di.di_mode) &&
  439. (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
  440. error = gfs2_dir_exhash_dealloc(ip);
  441. if (error)
  442. goto out_iput;
  443. }
  444. if (ip->i_di.di_eattr) {
  445. error = gfs2_ea_dealloc(ip);
  446. if (error)
  447. goto out_iput;
  448. }
  449. if (!gfs2_is_stuffed(ip)) {
  450. error = gfs2_file_dealloc(ip);
  451. if (error)
  452. goto out_iput;
  453. }
  454. error = dinode_dealloc(ip, ul);
  455. if (error)
  456. goto out_iput;
  457. out_iput:
  458. gfs2_glmutex_lock(i_gh.gh_gl);
  459. gfs2_inode_put(ip);
  460. gfs2_inode_destroy(ip);
  461. gfs2_glmutex_unlock(i_gh.gh_gl);
  462. out:
  463. gfs2_glock_dq_uninit(&i_gh);
  464. return error;
  465. }
  466. /**
  467. * try_inode_dealloc - Try to deallocate an inode and all its blocks
  468. * @sdp: the filesystem
  469. *
  470. * Returns: 0 on success, -errno on error, 1 on busy (inode open)
  471. */
  472. static int try_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
  473. {
  474. struct gfs2_holder io_gh;
  475. int error = 0;
  476. gfs2_try_toss_inode(sdp, &ul->ul_ut.ut_inum);
  477. error = gfs2_glock_nq_num(sdp,
  478. ul->ul_ut.ut_inum.no_addr, &gfs2_iopen_glops,
  479. LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB, &io_gh);
  480. switch (error) {
  481. case 0:
  482. break;
  483. case GLR_TRYFAILED:
  484. return 1;
  485. default:
  486. return error;
  487. }
  488. gfs2_glock_dq(&io_gh);
  489. error = inode_dealloc(sdp, ul, &io_gh);
  490. gfs2_holder_uninit(&io_gh);
  491. return error;
  492. }
  493. static int inode_dealloc_uninit(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
  494. {
  495. struct gfs2_rgrpd *rgd;
  496. struct gfs2_holder ri_gh, rgd_gh;
  497. int error;
  498. error = gfs2_rindex_hold(sdp, &ri_gh);
  499. if (error)
  500. return error;
  501. rgd = gfs2_blk2rgrpd(sdp, ul->ul_ut.ut_inum.no_addr);
  502. if (!rgd) {
  503. gfs2_consist(sdp);
  504. error = -EIO;
  505. goto out;
  506. }
  507. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rgd_gh);
  508. if (error)
  509. goto out;
  510. error = gfs2_trans_begin(sdp,
  511. RES_RG_BIT + RES_UNLINKED + RES_STATFS,
  512. 0);
  513. if (error)
  514. goto out_gunlock;
  515. gfs2_free_uninit_di(rgd, ul->ul_ut.ut_inum.no_addr);
  516. gfs2_unlinked_ondisk_rm(sdp, ul);
  517. gfs2_trans_end(sdp);
  518. out_gunlock:
  519. gfs2_glock_dq_uninit(&rgd_gh);
  520. out:
  521. gfs2_glock_dq_uninit(&ri_gh);
  522. return error;
  523. }
  524. int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
  525. {
  526. if (ul->ul_ut.ut_flags & GFS2_UTF_UNINIT)
  527. return inode_dealloc_uninit(sdp, ul);
  528. else
  529. return try_inode_dealloc(sdp, ul);
  530. }
  531. /**
  532. * gfs2_change_nlink - Change nlink count on inode
  533. * @ip: The GFS2 inode
  534. * @diff: The change in the nlink count required
  535. *
  536. * Returns: errno
  537. */
  538. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  539. {
  540. struct buffer_head *dibh;
  541. uint32_t nlink;
  542. int error;
  543. nlink = ip->i_di.di_nlink + diff;
  544. /* If we are reducing the nlink count, but the new value ends up being
  545. bigger than the old one, we must have underflowed. */
  546. if (diff < 0 && nlink > ip->i_di.di_nlink) {
  547. if (gfs2_consist_inode(ip))
  548. gfs2_dinode_print(&ip->i_di);
  549. return -EIO;
  550. }
  551. error = gfs2_meta_inode_buffer(ip, &dibh);
  552. if (error)
  553. return error;
  554. ip->i_di.di_nlink = nlink;
  555. ip->i_di.di_ctime = get_seconds();
  556. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  557. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  558. brelse(dibh);
  559. return 0;
  560. }
  561. /**
  562. * gfs2_lookupi - Look up a filename in a directory and return its inode
  563. * @d_gh: An initialized holder for the directory glock
  564. * @name: The name of the inode to look for
  565. * @is_root: If 1, ignore the caller's permissions
  566. * @i_gh: An uninitialized holder for the new inode glock
  567. *
  568. * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
  569. * @is_root is true.
  570. *
  571. * Returns: errno
  572. */
  573. int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
  574. struct inode **inodep)
  575. {
  576. struct gfs2_inode *ipp;
  577. struct gfs2_inode *dip = get_v2ip(dir);
  578. struct gfs2_sbd *sdp = dip->i_sbd;
  579. struct gfs2_holder d_gh;
  580. struct gfs2_inum inum;
  581. unsigned int type;
  582. struct gfs2_glock *gl;
  583. int error = 0;
  584. *inodep = NULL;
  585. if (!name->len || name->len > GFS2_FNAMESIZE)
  586. return -ENAMETOOLONG;
  587. if (gfs2_filecmp(name, ".", 1) ||
  588. (gfs2_filecmp(name, "..", 2) && dir == sdp->sd_root_dir)) {
  589. gfs2_inode_hold(dip);
  590. ipp = dip;
  591. goto done;
  592. }
  593. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  594. if (error)
  595. return error;
  596. if (!is_root) {
  597. error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL);
  598. if (error)
  599. goto out;
  600. }
  601. error = gfs2_dir_search(dip, name, &inum, &type);
  602. if (error)
  603. goto out;
  604. error = gfs2_glock_get(sdp, inum.no_addr, &gfs2_inode_glops,
  605. CREATE, &gl);
  606. if (error)
  607. goto out;
  608. error = gfs2_inode_get(gl, &inum, CREATE, &ipp);
  609. if (!error)
  610. gfs2_inode_min_init(ipp, type);
  611. gfs2_glock_put(gl);
  612. out:
  613. gfs2_glock_dq_uninit(&d_gh);
  614. done:
  615. if (error == 0) {
  616. *inodep = gfs2_ip2v(ipp);
  617. if (!*inodep)
  618. error = -ENOMEM;
  619. gfs2_inode_put(ipp);
  620. }
  621. return error;
  622. }
  623. static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
  624. {
  625. struct gfs2_inode *ip = get_v2ip(sdp->sd_ir_inode);
  626. struct buffer_head *bh;
  627. struct gfs2_inum_range ir;
  628. int error;
  629. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  630. if (error)
  631. return error;
  632. mutex_lock(&sdp->sd_inum_mutex);
  633. error = gfs2_meta_inode_buffer(ip, &bh);
  634. if (error) {
  635. mutex_unlock(&sdp->sd_inum_mutex);
  636. gfs2_trans_end(sdp);
  637. return error;
  638. }
  639. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  640. if (ir.ir_length) {
  641. *formal_ino = ir.ir_start++;
  642. ir.ir_length--;
  643. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  644. gfs2_inum_range_out(&ir,
  645. bh->b_data + sizeof(struct gfs2_dinode));
  646. brelse(bh);
  647. mutex_unlock(&sdp->sd_inum_mutex);
  648. gfs2_trans_end(sdp);
  649. return 0;
  650. }
  651. brelse(bh);
  652. mutex_unlock(&sdp->sd_inum_mutex);
  653. gfs2_trans_end(sdp);
  654. return 1;
  655. }
  656. static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino)
  657. {
  658. struct gfs2_inode *ip = get_v2ip(sdp->sd_ir_inode);
  659. struct gfs2_inode *m_ip = get_v2ip(sdp->sd_inum_inode);
  660. struct gfs2_holder gh;
  661. struct buffer_head *bh;
  662. struct gfs2_inum_range ir;
  663. int error;
  664. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  665. if (error)
  666. return error;
  667. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  668. if (error)
  669. goto out;
  670. mutex_lock(&sdp->sd_inum_mutex);
  671. error = gfs2_meta_inode_buffer(ip, &bh);
  672. if (error)
  673. goto out_end_trans;
  674. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  675. if (!ir.ir_length) {
  676. struct buffer_head *m_bh;
  677. uint64_t x, y;
  678. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  679. if (error)
  680. goto out_brelse;
  681. x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode));
  682. x = y = be64_to_cpu(x);
  683. ir.ir_start = x;
  684. ir.ir_length = GFS2_INUM_QUANTUM;
  685. x += GFS2_INUM_QUANTUM;
  686. if (x < y)
  687. gfs2_consist_inode(m_ip);
  688. x = cpu_to_be64(x);
  689. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  690. *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x;
  691. brelse(m_bh);
  692. }
  693. *formal_ino = ir.ir_start++;
  694. ir.ir_length--;
  695. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  696. gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  697. out_brelse:
  698. brelse(bh);
  699. out_end_trans:
  700. mutex_unlock(&sdp->sd_inum_mutex);
  701. gfs2_trans_end(sdp);
  702. out:
  703. gfs2_glock_dq_uninit(&gh);
  704. return error;
  705. }
  706. static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum)
  707. {
  708. int error;
  709. error = pick_formal_ino_1(sdp, inum);
  710. if (error <= 0)
  711. return error;
  712. error = pick_formal_ino_2(sdp, inum);
  713. return error;
  714. }
  715. /**
  716. * create_ok - OK to create a new on-disk inode here?
  717. * @dip: Directory in which dinode is to be created
  718. * @name: Name of new dinode
  719. * @mode:
  720. *
  721. * Returns: errno
  722. */
  723. static int create_ok(struct gfs2_inode *dip, struct qstr *name,
  724. unsigned int mode)
  725. {
  726. int error;
  727. error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
  728. if (error)
  729. return error;
  730. /* Don't create entries in an unlinked directory */
  731. if (!dip->i_di.di_nlink)
  732. return -EPERM;
  733. error = gfs2_dir_search(dip, name, NULL, NULL);
  734. switch (error) {
  735. case -ENOENT:
  736. error = 0;
  737. break;
  738. case 0:
  739. return -EEXIST;
  740. default:
  741. return error;
  742. }
  743. if (dip->i_di.di_entries == (uint32_t)-1)
  744. return -EFBIG;
  745. if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1)
  746. return -EMLINK;
  747. return 0;
  748. }
  749. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  750. unsigned int *uid, unsigned int *gid)
  751. {
  752. if (dip->i_sbd->sd_args.ar_suiddir &&
  753. (dip->i_di.di_mode & S_ISUID) &&
  754. dip->i_di.di_uid) {
  755. if (S_ISDIR(*mode))
  756. *mode |= S_ISUID;
  757. else if (dip->i_di.di_uid != current->fsuid)
  758. *mode &= ~07111;
  759. *uid = dip->i_di.di_uid;
  760. } else
  761. *uid = current->fsuid;
  762. if (dip->i_di.di_mode & S_ISGID) {
  763. if (S_ISDIR(*mode))
  764. *mode |= S_ISGID;
  765. *gid = dip->i_di.di_gid;
  766. } else
  767. *gid = current->fsgid;
  768. }
  769. static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_unlinked *ul)
  770. {
  771. struct gfs2_sbd *sdp = dip->i_sbd;
  772. int error;
  773. gfs2_alloc_get(dip);
  774. dip->i_alloc.al_requested = RES_DINODE;
  775. error = gfs2_inplace_reserve(dip);
  776. if (error)
  777. goto out;
  778. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
  779. RES_STATFS, 0);
  780. if (error)
  781. goto out_ipreserv;
  782. ul->ul_ut.ut_inum.no_addr = gfs2_alloc_di(dip);
  783. ul->ul_ut.ut_flags = GFS2_UTF_UNINIT;
  784. error = gfs2_unlinked_ondisk_add(sdp, ul);
  785. gfs2_trans_end(sdp);
  786. out_ipreserv:
  787. gfs2_inplace_release(dip);
  788. out:
  789. gfs2_alloc_put(dip);
  790. return error;
  791. }
  792. /**
  793. * init_dinode - Fill in a new dinode structure
  794. * @dip: the directory this inode is being created in
  795. * @gl: The glock covering the new inode
  796. * @inum: the inode number
  797. * @mode: the file permissions
  798. * @uid:
  799. * @gid:
  800. *
  801. */
  802. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  803. struct gfs2_inum *inum, unsigned int mode,
  804. unsigned int uid, unsigned int gid)
  805. {
  806. struct gfs2_sbd *sdp = dip->i_sbd;
  807. struct gfs2_dinode *di;
  808. struct buffer_head *dibh;
  809. dibh = gfs2_meta_new(gl, inum->no_addr);
  810. gfs2_trans_add_bh(gl, dibh, 1);
  811. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  812. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  813. di = (struct gfs2_dinode *)dibh->b_data;
  814. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  815. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  816. di->di_mode = cpu_to_be32(mode);
  817. di->di_uid = cpu_to_be32(uid);
  818. di->di_gid = cpu_to_be32(gid);
  819. di->di_nlink = cpu_to_be32(0);
  820. di->di_size = cpu_to_be64(0);
  821. di->di_blocks = cpu_to_be64(1);
  822. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
  823. di->di_major = di->di_minor = cpu_to_be32(0);
  824. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  825. di->__pad[0] = di->__pad[1] = 0;
  826. di->di_flags = cpu_to_be32(0);
  827. if (S_ISREG(mode)) {
  828. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
  829. gfs2_tune_get(sdp, gt_new_files_jdata))
  830. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  831. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
  832. gfs2_tune_get(sdp, gt_new_files_directio))
  833. di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
  834. } else if (S_ISDIR(mode)) {
  835. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  836. GFS2_DIF_INHERIT_DIRECTIO);
  837. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  838. GFS2_DIF_INHERIT_JDATA);
  839. }
  840. di->__pad1 = 0;
  841. di->di_height = cpu_to_be32(0);
  842. di->__pad2 = 0;
  843. di->__pad3 = 0;
  844. di->di_depth = cpu_to_be16(0);
  845. di->di_entries = cpu_to_be32(0);
  846. memset(&di->__pad4, 0, sizeof(di->__pad4));
  847. di->di_eattr = cpu_to_be64(0);
  848. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  849. brelse(dibh);
  850. }
  851. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  852. unsigned int mode, struct gfs2_unlinked *ul)
  853. {
  854. struct gfs2_sbd *sdp = dip->i_sbd;
  855. unsigned int uid, gid;
  856. int error;
  857. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  858. gfs2_alloc_get(dip);
  859. error = gfs2_quota_lock(dip, uid, gid);
  860. if (error)
  861. goto out;
  862. error = gfs2_quota_check(dip, uid, gid);
  863. if (error)
  864. goto out_quota;
  865. error = gfs2_trans_begin(sdp, RES_DINODE + RES_UNLINKED +
  866. RES_QUOTA, 0);
  867. if (error)
  868. goto out_quota;
  869. ul->ul_ut.ut_flags = 0;
  870. error = gfs2_unlinked_ondisk_munge(sdp, ul);
  871. init_dinode(dip, gl, &ul->ul_ut.ut_inum,
  872. mode, uid, gid);
  873. gfs2_quota_change(dip, +1, uid, gid);
  874. gfs2_trans_end(sdp);
  875. out_quota:
  876. gfs2_quota_unlock(dip);
  877. out:
  878. gfs2_alloc_put(dip);
  879. return error;
  880. }
  881. static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
  882. struct gfs2_inode *ip, struct gfs2_unlinked *ul)
  883. {
  884. struct gfs2_sbd *sdp = dip->i_sbd;
  885. struct gfs2_alloc *al;
  886. int alloc_required;
  887. struct buffer_head *dibh;
  888. int error;
  889. al = gfs2_alloc_get(dip);
  890. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  891. if (error)
  892. goto fail;
  893. error = gfs2_diradd_alloc_required(dip, name, &alloc_required);
  894. if (alloc_required) {
  895. error = gfs2_quota_check(dip, dip->i_di.di_uid,
  896. dip->i_di.di_gid);
  897. if (error)
  898. goto fail_quota_locks;
  899. al->al_requested = sdp->sd_max_dirres;
  900. error = gfs2_inplace_reserve(dip);
  901. if (error)
  902. goto fail_quota_locks;
  903. error = gfs2_trans_begin(sdp,
  904. sdp->sd_max_dirres +
  905. al->al_rgd->rd_ri.ri_length +
  906. 2 * RES_DINODE + RES_UNLINKED +
  907. RES_STATFS + RES_QUOTA, 0);
  908. if (error)
  909. goto fail_ipreserv;
  910. } else {
  911. error = gfs2_trans_begin(sdp,
  912. RES_LEAF +
  913. 2 * RES_DINODE +
  914. RES_UNLINKED, 0);
  915. if (error)
  916. goto fail_quota_locks;
  917. }
  918. error = gfs2_dir_add(dip, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
  919. if (error)
  920. goto fail_end_trans;
  921. error = gfs2_meta_inode_buffer(ip, &dibh);
  922. if (error)
  923. goto fail_end_trans;
  924. ip->i_di.di_nlink = 1;
  925. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  926. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  927. brelse(dibh);
  928. error = gfs2_unlinked_ondisk_rm(sdp, ul);
  929. if (error)
  930. goto fail_end_trans;
  931. return 0;
  932. fail_end_trans:
  933. gfs2_trans_end(sdp);
  934. fail_ipreserv:
  935. if (dip->i_alloc.al_rgd)
  936. gfs2_inplace_release(dip);
  937. fail_quota_locks:
  938. gfs2_quota_unlock(dip);
  939. fail:
  940. gfs2_alloc_put(dip);
  941. return error;
  942. }
  943. /**
  944. * gfs2_createi - Create a new inode
  945. * @ghs: An array of two holders
  946. * @name: The name of the new file
  947. * @mode: the permissions on the new inode
  948. *
  949. * @ghs[0] is an initialized holder for the directory
  950. * @ghs[1] is the holder for the inode lock
  951. *
  952. * If the return value is not NULL, the glocks on both the directory and the new
  953. * file are held. A transaction has been started and an inplace reservation
  954. * is held, as well.
  955. *
  956. * Returns: An inode
  957. */
  958. struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name,
  959. unsigned int mode)
  960. {
  961. struct inode *inode;
  962. struct gfs2_inode *dip = get_gl2ip(ghs->gh_gl);
  963. struct gfs2_sbd *sdp = dip->i_sbd;
  964. struct gfs2_unlinked *ul;
  965. struct gfs2_inode *ip;
  966. int error;
  967. if (!name->len || name->len > GFS2_FNAMESIZE)
  968. return ERR_PTR(-ENAMETOOLONG);
  969. error = gfs2_unlinked_get(sdp, &ul);
  970. if (error)
  971. return ERR_PTR(error);
  972. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  973. error = gfs2_glock_nq(ghs);
  974. if (error)
  975. goto fail;
  976. error = create_ok(dip, name, mode);
  977. if (error)
  978. goto fail_gunlock;
  979. error = pick_formal_ino(sdp, &ul->ul_ut.ut_inum.no_formal_ino);
  980. if (error)
  981. goto fail_gunlock;
  982. error = alloc_dinode(dip, ul);
  983. if (error)
  984. goto fail_gunlock;
  985. if (ul->ul_ut.ut_inum.no_addr < dip->i_num.no_addr) {
  986. gfs2_glock_dq(ghs);
  987. error = gfs2_glock_nq_num(sdp,
  988. ul->ul_ut.ut_inum.no_addr,
  989. &gfs2_inode_glops,
  990. LM_ST_EXCLUSIVE, GL_SKIP,
  991. ghs + 1);
  992. if (error) {
  993. gfs2_unlinked_put(sdp, ul);
  994. return ERR_PTR(error);
  995. }
  996. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  997. error = gfs2_glock_nq(ghs);
  998. if (error) {
  999. gfs2_glock_dq_uninit(ghs + 1);
  1000. gfs2_unlinked_put(sdp, ul);
  1001. return ERR_PTR(error);
  1002. }
  1003. error = create_ok(dip, name, mode);
  1004. if (error)
  1005. goto fail_gunlock2;
  1006. } else {
  1007. error = gfs2_glock_nq_num(sdp,
  1008. ul->ul_ut.ut_inum.no_addr,
  1009. &gfs2_inode_glops,
  1010. LM_ST_EXCLUSIVE, GL_SKIP,
  1011. ghs + 1);
  1012. if (error)
  1013. goto fail_gunlock;
  1014. }
  1015. error = make_dinode(dip, ghs[1].gh_gl, mode, ul);
  1016. if (error)
  1017. goto fail_gunlock2;
  1018. error = gfs2_inode_get(ghs[1].gh_gl, &ul->ul_ut.ut_inum, CREATE, &ip);
  1019. if (error)
  1020. goto fail_gunlock2;
  1021. error = gfs2_inode_refresh(ip);
  1022. if (error)
  1023. goto fail_iput;
  1024. error = gfs2_acl_create(dip, ip);
  1025. if (error)
  1026. goto fail_iput;
  1027. error = link_dinode(dip, name, ip, ul);
  1028. if (error)
  1029. goto fail_iput;
  1030. gfs2_unlinked_put(sdp, ul);
  1031. inode = gfs2_ip2v(ip);
  1032. gfs2_inode_put(ip);
  1033. if (!inode)
  1034. return ERR_PTR(-ENOMEM);
  1035. return inode;
  1036. fail_iput:
  1037. gfs2_inode_put(ip);
  1038. fail_gunlock2:
  1039. gfs2_glock_dq_uninit(ghs + 1);
  1040. fail_gunlock:
  1041. gfs2_glock_dq(ghs);
  1042. fail:
  1043. gfs2_unlinked_put(sdp, ul);
  1044. return ERR_PTR(error);
  1045. }
  1046. /**
  1047. * gfs2_unlinki - Unlink a file
  1048. * @dip: The inode of the directory
  1049. * @name: The name of the file to be unlinked
  1050. * @ip: The inode of the file to be removed
  1051. *
  1052. * Assumes Glocks on both dip and ip are held.
  1053. *
  1054. * Returns: errno
  1055. */
  1056. int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name,
  1057. struct gfs2_inode *ip, struct gfs2_unlinked *ul)
  1058. {
  1059. struct gfs2_sbd *sdp = dip->i_sbd;
  1060. int error;
  1061. error = gfs2_dir_del(dip, name);
  1062. if (error)
  1063. return error;
  1064. error = gfs2_change_nlink(ip, -1);
  1065. if (error)
  1066. return error;
  1067. /* If this inode is being unlinked from the directory structure,
  1068. we need to mark that in the log so that it isn't lost during
  1069. a crash. */
  1070. if (!ip->i_di.di_nlink) {
  1071. ul->ul_ut.ut_inum = ip->i_num;
  1072. error = gfs2_unlinked_ondisk_add(sdp, ul);
  1073. if (!error)
  1074. set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  1075. }
  1076. return error;
  1077. }
  1078. /**
  1079. * gfs2_rmdiri - Remove a directory
  1080. * @dip: The parent directory of the directory to be removed
  1081. * @name: The name of the directory to be removed
  1082. * @ip: The GFS2 inode of the directory to be removed
  1083. *
  1084. * Assumes Glocks on dip and ip are held
  1085. *
  1086. * Returns: errno
  1087. */
  1088. int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name,
  1089. struct gfs2_inode *ip, struct gfs2_unlinked *ul)
  1090. {
  1091. struct gfs2_sbd *sdp = dip->i_sbd;
  1092. struct qstr dotname;
  1093. int error;
  1094. if (ip->i_di.di_entries != 2) {
  1095. if (gfs2_consist_inode(ip))
  1096. gfs2_dinode_print(&ip->i_di);
  1097. return -EIO;
  1098. }
  1099. error = gfs2_dir_del(dip, name);
  1100. if (error)
  1101. return error;
  1102. error = gfs2_change_nlink(dip, -1);
  1103. if (error)
  1104. return error;
  1105. dotname.len = 1;
  1106. dotname.name = ".";
  1107. error = gfs2_dir_del(ip, &dotname);
  1108. if (error)
  1109. return error;
  1110. dotname.len = 2;
  1111. dotname.name = "..";
  1112. error = gfs2_dir_del(ip, &dotname);
  1113. if (error)
  1114. return error;
  1115. error = gfs2_change_nlink(ip, -2);
  1116. if (error)
  1117. return error;
  1118. /* This inode is being unlinked from the directory structure and
  1119. we need to mark that in the log so that it isn't lost during
  1120. a crash. */
  1121. ul->ul_ut.ut_inum = ip->i_num;
  1122. error = gfs2_unlinked_ondisk_add(sdp, ul);
  1123. if (!error)
  1124. set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  1125. return error;
  1126. }
  1127. /*
  1128. * gfs2_unlink_ok - check to see that a inode is still in a directory
  1129. * @dip: the directory
  1130. * @name: the name of the file
  1131. * @ip: the inode
  1132. *
  1133. * Assumes that the lock on (at least) @dip is held.
  1134. *
  1135. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  1136. */
  1137. int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name,
  1138. struct gfs2_inode *ip)
  1139. {
  1140. struct gfs2_inum inum;
  1141. unsigned int type;
  1142. int error;
  1143. if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
  1144. return -EPERM;
  1145. if ((dip->i_di.di_mode & S_ISVTX) &&
  1146. dip->i_di.di_uid != current->fsuid &&
  1147. ip->i_di.di_uid != current->fsuid &&
  1148. !capable(CAP_FOWNER))
  1149. return -EPERM;
  1150. if (IS_APPEND(dip->i_vnode))
  1151. return -EPERM;
  1152. error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
  1153. if (error)
  1154. return error;
  1155. error = gfs2_dir_search(dip, name, &inum, &type);
  1156. if (error)
  1157. return error;
  1158. if (!gfs2_inum_equal(&inum, &ip->i_num))
  1159. return -ENOENT;
  1160. if (IF2DT(ip->i_di.di_mode) != type) {
  1161. gfs2_consist_inode(dip);
  1162. return -EIO;
  1163. }
  1164. return 0;
  1165. }
  1166. /*
  1167. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  1168. * @this: move this
  1169. * @to: to here
  1170. *
  1171. * Follow @to back to the root and make sure we don't encounter @this
  1172. * Assumes we already hold the rename lock.
  1173. *
  1174. * Returns: errno
  1175. */
  1176. int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1177. {
  1178. struct gfs2_sbd *sdp = this->i_sbd;
  1179. struct inode *dir = to->i_vnode;
  1180. struct inode *tmp;
  1181. struct qstr dotdot;
  1182. int error = 0;
  1183. memset(&dotdot, 0, sizeof(struct qstr));
  1184. dotdot.name = "..";
  1185. dotdot.len = 2;
  1186. igrab(dir);
  1187. for (;;) {
  1188. if (dir == this->i_vnode) {
  1189. error = -EINVAL;
  1190. break;
  1191. }
  1192. if (dir == sdp->sd_root_dir) {
  1193. error = 0;
  1194. break;
  1195. }
  1196. error = gfs2_lookupi(dir, &dotdot, 1, &tmp);
  1197. if (error)
  1198. break;
  1199. iput(dir);
  1200. dir = tmp;
  1201. }
  1202. iput(dir);
  1203. return error;
  1204. }
  1205. /**
  1206. * gfs2_readlinki - return the contents of a symlink
  1207. * @ip: the symlink's inode
  1208. * @buf: a pointer to the buffer to be filled
  1209. * @len: a pointer to the length of @buf
  1210. *
  1211. * If @buf is too small, a piece of memory is kmalloc()ed and needs
  1212. * to be freed by the caller.
  1213. *
  1214. * Returns: errno
  1215. */
  1216. int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
  1217. {
  1218. struct gfs2_holder i_gh;
  1219. struct buffer_head *dibh;
  1220. unsigned int x;
  1221. int error;
  1222. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  1223. error = gfs2_glock_nq_atime(&i_gh);
  1224. if (error) {
  1225. gfs2_holder_uninit(&i_gh);
  1226. return error;
  1227. }
  1228. if (!ip->i_di.di_size) {
  1229. gfs2_consist_inode(ip);
  1230. error = -EIO;
  1231. goto out;
  1232. }
  1233. error = gfs2_meta_inode_buffer(ip, &dibh);
  1234. if (error)
  1235. goto out;
  1236. x = ip->i_di.di_size + 1;
  1237. if (x > *len) {
  1238. *buf = kmalloc(x, GFP_KERNEL);
  1239. if (!*buf) {
  1240. error = -ENOMEM;
  1241. goto out_brelse;
  1242. }
  1243. }
  1244. memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  1245. *len = x;
  1246. out_brelse:
  1247. brelse(dibh);
  1248. out:
  1249. gfs2_glock_dq_uninit(&i_gh);
  1250. return error;
  1251. }
  1252. /**
  1253. * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
  1254. * conditionally update the inode's atime
  1255. * @gh: the holder to acquire
  1256. *
  1257. * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
  1258. * Update if the difference between the current time and the inode's current
  1259. * atime is greater than an interval specified at mount.
  1260. *
  1261. * Returns: errno
  1262. */
  1263. int gfs2_glock_nq_atime(struct gfs2_holder *gh)
  1264. {
  1265. struct gfs2_glock *gl = gh->gh_gl;
  1266. struct gfs2_sbd *sdp = gl->gl_sbd;
  1267. struct gfs2_inode *ip = get_gl2ip(gl);
  1268. int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
  1269. unsigned int state;
  1270. int flags;
  1271. int error;
  1272. if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
  1273. gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
  1274. gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
  1275. return -EINVAL;
  1276. state = gh->gh_state;
  1277. flags = gh->gh_flags;
  1278. error = gfs2_glock_nq(gh);
  1279. if (error)
  1280. return error;
  1281. if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
  1282. (sdp->sd_vfs->s_flags & MS_RDONLY))
  1283. return 0;
  1284. curtime = get_seconds();
  1285. if (curtime - ip->i_di.di_atime >= quantum) {
  1286. gfs2_glock_dq(gh);
  1287. gfs2_holder_reinit(LM_ST_EXCLUSIVE,
  1288. gh->gh_flags & ~LM_FLAG_ANY,
  1289. gh);
  1290. error = gfs2_glock_nq(gh);
  1291. if (error)
  1292. return error;
  1293. /* Verify that atime hasn't been updated while we were
  1294. trying to get exclusive lock. */
  1295. curtime = get_seconds();
  1296. if (curtime - ip->i_di.di_atime >= quantum) {
  1297. struct buffer_head *dibh;
  1298. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1299. if (error == -EROFS)
  1300. return 0;
  1301. if (error)
  1302. goto fail;
  1303. error = gfs2_meta_inode_buffer(ip, &dibh);
  1304. if (error)
  1305. goto fail_end_trans;
  1306. ip->i_di.di_atime = curtime;
  1307. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1308. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1309. brelse(dibh);
  1310. gfs2_trans_end(sdp);
  1311. }
  1312. /* If someone else has asked for the glock,
  1313. unlock and let them have it. Then reacquire
  1314. in the original state. */
  1315. if (gfs2_glock_is_blocking(gl)) {
  1316. gfs2_glock_dq(gh);
  1317. gfs2_holder_reinit(state, flags, gh);
  1318. return gfs2_glock_nq(gh);
  1319. }
  1320. }
  1321. return 0;
  1322. fail_end_trans:
  1323. gfs2_trans_end(sdp);
  1324. fail:
  1325. gfs2_glock_dq(gh);
  1326. return error;
  1327. }
  1328. /**
  1329. * glock_compare_atime - Compare two struct gfs2_glock structures for sort
  1330. * @arg_a: the first structure
  1331. * @arg_b: the second structure
  1332. *
  1333. * Returns: 1 if A > B
  1334. * -1 if A < B
  1335. * 0 if A = B
  1336. */
  1337. static int glock_compare_atime(const void *arg_a, const void *arg_b)
  1338. {
  1339. struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
  1340. struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
  1341. struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1342. struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1343. int ret = 0;
  1344. if (a->ln_number > b->ln_number)
  1345. ret = 1;
  1346. else if (a->ln_number < b->ln_number)
  1347. ret = -1;
  1348. else {
  1349. if (gh_a->gh_state == LM_ST_SHARED &&
  1350. gh_b->gh_state == LM_ST_EXCLUSIVE)
  1351. ret = 1;
  1352. else if (gh_a->gh_state == LM_ST_SHARED &&
  1353. (gh_b->gh_flags & GL_ATIME))
  1354. ret = 1;
  1355. }
  1356. return ret;
  1357. }
  1358. /**
  1359. * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
  1360. * atime update
  1361. * @num_gh: the number of structures
  1362. * @ghs: an array of struct gfs2_holder structures
  1363. *
  1364. * Returns: 0 on success (all glocks acquired),
  1365. * errno on failure (no glocks acquired)
  1366. */
  1367. int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
  1368. {
  1369. struct gfs2_holder **p;
  1370. unsigned int x;
  1371. int error = 0;
  1372. if (!num_gh)
  1373. return 0;
  1374. if (num_gh == 1) {
  1375. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1376. if (ghs->gh_flags & GL_ATIME)
  1377. error = gfs2_glock_nq_atime(ghs);
  1378. else
  1379. error = gfs2_glock_nq(ghs);
  1380. return error;
  1381. }
  1382. p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
  1383. if (!p)
  1384. return -ENOMEM;
  1385. for (x = 0; x < num_gh; x++)
  1386. p[x] = &ghs[x];
  1387. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
  1388. for (x = 0; x < num_gh; x++) {
  1389. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1390. if (p[x]->gh_flags & GL_ATIME)
  1391. error = gfs2_glock_nq_atime(p[x]);
  1392. else
  1393. error = gfs2_glock_nq(p[x]);
  1394. if (error) {
  1395. while (x--)
  1396. gfs2_glock_dq(p[x]);
  1397. break;
  1398. }
  1399. }
  1400. kfree(p);
  1401. return error;
  1402. }
  1403. /**
  1404. * gfs2_try_toss_vnode - See if we can toss a vnode from memory
  1405. * @ip: the inode
  1406. *
  1407. * Returns: 1 if the vnode was tossed
  1408. */
  1409. void gfs2_try_toss_vnode(struct gfs2_inode *ip)
  1410. {
  1411. struct inode *inode;
  1412. inode = gfs2_ip2v_lookup(ip);
  1413. if (!inode)
  1414. return;
  1415. d_prune_aliases(inode);
  1416. if (S_ISDIR(ip->i_di.di_mode)) {
  1417. struct list_head *head = &inode->i_dentry;
  1418. struct dentry *d = NULL;
  1419. spin_lock(&dcache_lock);
  1420. if (list_empty(head))
  1421. spin_unlock(&dcache_lock);
  1422. else {
  1423. d = list_entry(head->next, struct dentry, d_alias);
  1424. dget_locked(d);
  1425. spin_unlock(&dcache_lock);
  1426. if (have_submounts(d))
  1427. dput(d);
  1428. else {
  1429. shrink_dcache_parent(d);
  1430. dput(d);
  1431. d_prune_aliases(inode);
  1432. }
  1433. }
  1434. }
  1435. inode->i_nlink = 0;
  1436. iput(inode);
  1437. }
  1438. static int
  1439. __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1440. {
  1441. struct buffer_head *dibh;
  1442. int error;
  1443. error = gfs2_meta_inode_buffer(ip, &dibh);
  1444. if (!error) {
  1445. error = inode_setattr(ip->i_vnode, attr);
  1446. gfs2_assert_warn(ip->i_sbd, !error);
  1447. gfs2_inode_attr_out(ip);
  1448. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1449. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1450. brelse(dibh);
  1451. }
  1452. return error;
  1453. }
  1454. /**
  1455. * gfs2_setattr_simple -
  1456. * @ip:
  1457. * @attr:
  1458. *
  1459. * Called with a reference on the vnode.
  1460. *
  1461. * Returns: errno
  1462. */
  1463. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1464. {
  1465. int error;
  1466. if (get_transaction)
  1467. return __gfs2_setattr_simple(ip, attr);
  1468. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
  1469. if (error)
  1470. return error;
  1471. error = __gfs2_setattr_simple(ip, attr);
  1472. gfs2_trans_end(ip->i_sbd);
  1473. return error;
  1474. }
  1475. int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd)
  1476. {
  1477. return permission(inode, mask, nd);
  1478. }