dir.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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. /*
  10. * Implements Extendible Hashing as described in:
  11. * "Extendible Hashing" by Fagin, et al in
  12. * __ACM Trans. on Database Systems__, Sept 1979.
  13. *
  14. *
  15. * Here's the layout of dirents which is essentially the same as that of ext2
  16. * within a single block. The field de_name_len is the number of bytes
  17. * actually required for the name (no null terminator). The field de_rec_len
  18. * is the number of bytes allocated to the dirent. The offset of the next
  19. * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
  20. * deleted, the preceding dirent inherits its allocated space, ie
  21. * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
  22. * by adding de_rec_len to the current dirent, this essentially causes the
  23. * deleted dirent to get jumped over when iterating through all the dirents.
  24. *
  25. * When deleting the first dirent in a block, there is no previous dirent so
  26. * the field de_ino is set to zero to designate it as deleted. When allocating
  27. * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
  28. * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
  29. * dirent is allocated. Otherwise it must go through all the 'used' dirents
  30. * searching for one in which the amount of total space minus the amount of
  31. * used space will provide enough space for the new dirent.
  32. *
  33. * There are two types of blocks in which dirents reside. In a stuffed dinode,
  34. * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
  35. * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
  36. * beginning of the leaf block. The dirents reside in leaves when
  37. *
  38. * dip->i_diskflags & GFS2_DIF_EXHASH is true
  39. *
  40. * Otherwise, the dirents are "linear", within a single stuffed dinode block.
  41. *
  42. * When the dirents are in leaves, the actual contents of the directory file are
  43. * used as an array of 64-bit block pointers pointing to the leaf blocks. The
  44. * dirents are NOT in the directory file itself. There can be more than one
  45. * block pointer in the array that points to the same leaf. In fact, when a
  46. * directory is first converted from linear to exhash, all of the pointers
  47. * point to the same leaf.
  48. *
  49. * When a leaf is completely full, the size of the hash table can be
  50. * doubled unless it is already at the maximum size which is hard coded into
  51. * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
  52. * but never before the maximum hash table size has been reached.
  53. */
  54. #include <linux/slab.h>
  55. #include <linux/spinlock.h>
  56. #include <linux/buffer_head.h>
  57. #include <linux/sort.h>
  58. #include <linux/gfs2_ondisk.h>
  59. #include <linux/crc32.h>
  60. #include <linux/vmalloc.h>
  61. #include "gfs2.h"
  62. #include "incore.h"
  63. #include "dir.h"
  64. #include "glock.h"
  65. #include "inode.h"
  66. #include "meta_io.h"
  67. #include "quota.h"
  68. #include "rgrp.h"
  69. #include "trans.h"
  70. #include "bmap.h"
  71. #include "util.h"
  72. #define IS_LEAF 1 /* Hashed (leaf) directory */
  73. #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
  74. #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
  75. #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
  76. typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
  77. u64 leaf_no, void *data);
  78. typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
  79. const struct qstr *name, void *opaque);
  80. int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
  81. struct buffer_head **bhp)
  82. {
  83. struct buffer_head *bh;
  84. bh = gfs2_meta_new(ip->i_gl, block);
  85. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  86. gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
  87. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  88. *bhp = bh;
  89. return 0;
  90. }
  91. static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
  92. struct buffer_head **bhp)
  93. {
  94. struct buffer_head *bh;
  95. int error;
  96. error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
  97. if (error)
  98. return error;
  99. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
  100. brelse(bh);
  101. return -EIO;
  102. }
  103. *bhp = bh;
  104. return 0;
  105. }
  106. static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
  107. unsigned int offset, unsigned int size)
  108. {
  109. struct buffer_head *dibh;
  110. int error;
  111. error = gfs2_meta_inode_buffer(ip, &dibh);
  112. if (error)
  113. return error;
  114. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  115. memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
  116. if (ip->i_disksize < offset + size)
  117. ip->i_disksize = offset + size;
  118. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  119. gfs2_dinode_out(ip, dibh->b_data);
  120. brelse(dibh);
  121. return size;
  122. }
  123. /**
  124. * gfs2_dir_write_data - Write directory information to the inode
  125. * @ip: The GFS2 inode
  126. * @buf: The buffer containing information to be written
  127. * @offset: The file offset to start writing at
  128. * @size: The amount of data to write
  129. *
  130. * Returns: The number of bytes correctly written or error code
  131. */
  132. static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
  133. u64 offset, unsigned int size)
  134. {
  135. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  136. struct buffer_head *dibh;
  137. u64 lblock, dblock;
  138. u32 extlen = 0;
  139. unsigned int o;
  140. int copied = 0;
  141. int error = 0;
  142. int new = 0;
  143. if (!size)
  144. return 0;
  145. if (gfs2_is_stuffed(ip) &&
  146. offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  147. return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
  148. size);
  149. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  150. return -EINVAL;
  151. if (gfs2_is_stuffed(ip)) {
  152. error = gfs2_unstuff_dinode(ip, NULL);
  153. if (error)
  154. return error;
  155. }
  156. lblock = offset;
  157. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  158. while (copied < size) {
  159. unsigned int amount;
  160. struct buffer_head *bh;
  161. amount = size - copied;
  162. if (amount > sdp->sd_sb.sb_bsize - o)
  163. amount = sdp->sd_sb.sb_bsize - o;
  164. if (!extlen) {
  165. new = 1;
  166. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  167. &dblock, &extlen);
  168. if (error)
  169. goto fail;
  170. error = -EIO;
  171. if (gfs2_assert_withdraw(sdp, dblock))
  172. goto fail;
  173. }
  174. if (amount == sdp->sd_jbsize || new)
  175. error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
  176. else
  177. error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
  178. if (error)
  179. goto fail;
  180. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  181. memcpy(bh->b_data + o, buf, amount);
  182. brelse(bh);
  183. buf += amount;
  184. copied += amount;
  185. lblock++;
  186. dblock++;
  187. extlen--;
  188. o = sizeof(struct gfs2_meta_header);
  189. }
  190. out:
  191. error = gfs2_meta_inode_buffer(ip, &dibh);
  192. if (error)
  193. return error;
  194. if (ip->i_disksize < offset + copied)
  195. ip->i_disksize = offset + copied;
  196. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  197. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  198. gfs2_dinode_out(ip, dibh->b_data);
  199. brelse(dibh);
  200. return copied;
  201. fail:
  202. if (copied)
  203. goto out;
  204. return error;
  205. }
  206. static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
  207. u64 offset, unsigned int size)
  208. {
  209. struct buffer_head *dibh;
  210. int error;
  211. error = gfs2_meta_inode_buffer(ip, &dibh);
  212. if (!error) {
  213. offset += sizeof(struct gfs2_dinode);
  214. memcpy(buf, dibh->b_data + offset, size);
  215. brelse(dibh);
  216. }
  217. return (error) ? error : size;
  218. }
  219. /**
  220. * gfs2_dir_read_data - Read a data from a directory inode
  221. * @ip: The GFS2 Inode
  222. * @buf: The buffer to place result into
  223. * @offset: File offset to begin jdata_readng from
  224. * @size: Amount of data to transfer
  225. *
  226. * Returns: The amount of data actually copied or the error
  227. */
  228. static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
  229. unsigned int size, unsigned ra)
  230. {
  231. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  232. u64 lblock, dblock;
  233. u32 extlen = 0;
  234. unsigned int o;
  235. int copied = 0;
  236. int error = 0;
  237. if (offset >= ip->i_disksize)
  238. return 0;
  239. if (offset + size > ip->i_disksize)
  240. size = ip->i_disksize - offset;
  241. if (!size)
  242. return 0;
  243. if (gfs2_is_stuffed(ip))
  244. return gfs2_dir_read_stuffed(ip, buf, offset, size);
  245. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  246. return -EINVAL;
  247. lblock = offset;
  248. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  249. while (copied < size) {
  250. unsigned int amount;
  251. struct buffer_head *bh;
  252. int new;
  253. amount = size - copied;
  254. if (amount > sdp->sd_sb.sb_bsize - o)
  255. amount = sdp->sd_sb.sb_bsize - o;
  256. if (!extlen) {
  257. new = 0;
  258. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  259. &dblock, &extlen);
  260. if (error || !dblock)
  261. goto fail;
  262. BUG_ON(extlen < 1);
  263. if (!ra)
  264. extlen = 1;
  265. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  266. } else {
  267. error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
  268. if (error)
  269. goto fail;
  270. }
  271. error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
  272. if (error) {
  273. brelse(bh);
  274. goto fail;
  275. }
  276. dblock++;
  277. extlen--;
  278. memcpy(buf, bh->b_data + o, amount);
  279. brelse(bh);
  280. buf += amount;
  281. copied += amount;
  282. lblock++;
  283. o = sizeof(struct gfs2_meta_header);
  284. }
  285. return copied;
  286. fail:
  287. return (copied) ? copied : error;
  288. }
  289. static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
  290. {
  291. return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
  292. }
  293. static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
  294. const struct qstr *name, int ret)
  295. {
  296. if (!gfs2_dirent_sentinel(dent) &&
  297. be32_to_cpu(dent->de_hash) == name->hash &&
  298. be16_to_cpu(dent->de_name_len) == name->len &&
  299. memcmp(dent+1, name->name, name->len) == 0)
  300. return ret;
  301. return 0;
  302. }
  303. static int gfs2_dirent_find(const struct gfs2_dirent *dent,
  304. const struct qstr *name,
  305. void *opaque)
  306. {
  307. return __gfs2_dirent_find(dent, name, 1);
  308. }
  309. static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
  310. const struct qstr *name,
  311. void *opaque)
  312. {
  313. return __gfs2_dirent_find(dent, name, 2);
  314. }
  315. /*
  316. * name->name holds ptr to start of block.
  317. * name->len holds size of block.
  318. */
  319. static int gfs2_dirent_last(const struct gfs2_dirent *dent,
  320. const struct qstr *name,
  321. void *opaque)
  322. {
  323. const char *start = name->name;
  324. const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
  325. if (name->len == (end - start))
  326. return 1;
  327. return 0;
  328. }
  329. static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
  330. const struct qstr *name,
  331. void *opaque)
  332. {
  333. unsigned required = GFS2_DIRENT_SIZE(name->len);
  334. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  335. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  336. if (gfs2_dirent_sentinel(dent))
  337. actual = GFS2_DIRENT_SIZE(0);
  338. if (totlen - actual >= required)
  339. return 1;
  340. return 0;
  341. }
  342. struct dirent_gather {
  343. const struct gfs2_dirent **pdent;
  344. unsigned offset;
  345. };
  346. static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
  347. const struct qstr *name,
  348. void *opaque)
  349. {
  350. struct dirent_gather *g = opaque;
  351. if (!gfs2_dirent_sentinel(dent)) {
  352. g->pdent[g->offset++] = dent;
  353. }
  354. return 0;
  355. }
  356. /*
  357. * Other possible things to check:
  358. * - Inode located within filesystem size (and on valid block)
  359. * - Valid directory entry type
  360. * Not sure how heavy-weight we want to make this... could also check
  361. * hash is correct for example, but that would take a lot of extra time.
  362. * For now the most important thing is to check that the various sizes
  363. * are correct.
  364. */
  365. static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
  366. unsigned int size, unsigned int len, int first)
  367. {
  368. const char *msg = "gfs2_dirent too small";
  369. if (unlikely(size < sizeof(struct gfs2_dirent)))
  370. goto error;
  371. msg = "gfs2_dirent misaligned";
  372. if (unlikely(offset & 0x7))
  373. goto error;
  374. msg = "gfs2_dirent points beyond end of block";
  375. if (unlikely(offset + size > len))
  376. goto error;
  377. msg = "zero inode number";
  378. if (unlikely(!first && gfs2_dirent_sentinel(dent)))
  379. goto error;
  380. msg = "name length is greater than space in dirent";
  381. if (!gfs2_dirent_sentinel(dent) &&
  382. unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
  383. size))
  384. goto error;
  385. return 0;
  386. error:
  387. printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
  388. first ? "first in block" : "not first in block");
  389. return -EIO;
  390. }
  391. static int gfs2_dirent_offset(const void *buf)
  392. {
  393. const struct gfs2_meta_header *h = buf;
  394. int offset;
  395. BUG_ON(buf == NULL);
  396. switch(be32_to_cpu(h->mh_type)) {
  397. case GFS2_METATYPE_LF:
  398. offset = sizeof(struct gfs2_leaf);
  399. break;
  400. case GFS2_METATYPE_DI:
  401. offset = sizeof(struct gfs2_dinode);
  402. break;
  403. default:
  404. goto wrong_type;
  405. }
  406. return offset;
  407. wrong_type:
  408. printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
  409. be32_to_cpu(h->mh_type));
  410. return -1;
  411. }
  412. static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
  413. unsigned int len, gfs2_dscan_t scan,
  414. const struct qstr *name,
  415. void *opaque)
  416. {
  417. struct gfs2_dirent *dent, *prev;
  418. unsigned offset;
  419. unsigned size;
  420. int ret = 0;
  421. ret = gfs2_dirent_offset(buf);
  422. if (ret < 0)
  423. goto consist_inode;
  424. offset = ret;
  425. prev = NULL;
  426. dent = buf + offset;
  427. size = be16_to_cpu(dent->de_rec_len);
  428. if (gfs2_check_dirent(dent, offset, size, len, 1))
  429. goto consist_inode;
  430. do {
  431. ret = scan(dent, name, opaque);
  432. if (ret)
  433. break;
  434. offset += size;
  435. if (offset == len)
  436. break;
  437. prev = dent;
  438. dent = buf + offset;
  439. size = be16_to_cpu(dent->de_rec_len);
  440. if (gfs2_check_dirent(dent, offset, size, len, 0))
  441. goto consist_inode;
  442. } while(1);
  443. switch(ret) {
  444. case 0:
  445. return NULL;
  446. case 1:
  447. return dent;
  448. case 2:
  449. return prev ? prev : dent;
  450. default:
  451. BUG_ON(ret > 0);
  452. return ERR_PTR(ret);
  453. }
  454. consist_inode:
  455. gfs2_consist_inode(GFS2_I(inode));
  456. return ERR_PTR(-EIO);
  457. }
  458. /**
  459. * dirent_first - Return the first dirent
  460. * @dip: the directory
  461. * @bh: The buffer
  462. * @dent: Pointer to list of dirents
  463. *
  464. * return first dirent whether bh points to leaf or stuffed dinode
  465. *
  466. * Returns: IS_LEAF, IS_DINODE, or -errno
  467. */
  468. static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
  469. struct gfs2_dirent **dent)
  470. {
  471. struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
  472. if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
  473. if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
  474. return -EIO;
  475. *dent = (struct gfs2_dirent *)(bh->b_data +
  476. sizeof(struct gfs2_leaf));
  477. return IS_LEAF;
  478. } else {
  479. if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
  480. return -EIO;
  481. *dent = (struct gfs2_dirent *)(bh->b_data +
  482. sizeof(struct gfs2_dinode));
  483. return IS_DINODE;
  484. }
  485. }
  486. static int dirent_check_reclen(struct gfs2_inode *dip,
  487. const struct gfs2_dirent *d, const void *end_p)
  488. {
  489. const void *ptr = d;
  490. u16 rec_len = be16_to_cpu(d->de_rec_len);
  491. if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
  492. goto broken;
  493. ptr += rec_len;
  494. if (ptr < end_p)
  495. return rec_len;
  496. if (ptr == end_p)
  497. return -ENOENT;
  498. broken:
  499. gfs2_consist_inode(dip);
  500. return -EIO;
  501. }
  502. /**
  503. * dirent_next - Next dirent
  504. * @dip: the directory
  505. * @bh: The buffer
  506. * @dent: Pointer to list of dirents
  507. *
  508. * Returns: 0 on success, error code otherwise
  509. */
  510. static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
  511. struct gfs2_dirent **dent)
  512. {
  513. struct gfs2_dirent *cur = *dent, *tmp;
  514. char *bh_end = bh->b_data + bh->b_size;
  515. int ret;
  516. ret = dirent_check_reclen(dip, cur, bh_end);
  517. if (ret < 0)
  518. return ret;
  519. tmp = (void *)cur + ret;
  520. ret = dirent_check_reclen(dip, tmp, bh_end);
  521. if (ret == -EIO)
  522. return ret;
  523. /* Only the first dent could ever have de_inum.no_addr == 0 */
  524. if (gfs2_dirent_sentinel(tmp)) {
  525. gfs2_consist_inode(dip);
  526. return -EIO;
  527. }
  528. *dent = tmp;
  529. return 0;
  530. }
  531. /**
  532. * dirent_del - Delete a dirent
  533. * @dip: The GFS2 inode
  534. * @bh: The buffer
  535. * @prev: The previous dirent
  536. * @cur: The current dirent
  537. *
  538. */
  539. static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
  540. struct gfs2_dirent *prev, struct gfs2_dirent *cur)
  541. {
  542. u16 cur_rec_len, prev_rec_len;
  543. if (gfs2_dirent_sentinel(cur)) {
  544. gfs2_consist_inode(dip);
  545. return;
  546. }
  547. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  548. /* If there is no prev entry, this is the first entry in the block.
  549. The de_rec_len is already as big as it needs to be. Just zero
  550. out the inode number and return. */
  551. if (!prev) {
  552. cur->de_inum.no_addr = 0;
  553. cur->de_inum.no_formal_ino = 0;
  554. return;
  555. }
  556. /* Combine this dentry with the previous one. */
  557. prev_rec_len = be16_to_cpu(prev->de_rec_len);
  558. cur_rec_len = be16_to_cpu(cur->de_rec_len);
  559. if ((char *)prev + prev_rec_len != (char *)cur)
  560. gfs2_consist_inode(dip);
  561. if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
  562. gfs2_consist_inode(dip);
  563. prev_rec_len += cur_rec_len;
  564. prev->de_rec_len = cpu_to_be16(prev_rec_len);
  565. }
  566. /*
  567. * Takes a dent from which to grab space as an argument. Returns the
  568. * newly created dent.
  569. */
  570. static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
  571. struct gfs2_dirent *dent,
  572. const struct qstr *name,
  573. struct buffer_head *bh)
  574. {
  575. struct gfs2_inode *ip = GFS2_I(inode);
  576. struct gfs2_dirent *ndent;
  577. unsigned offset = 0, totlen;
  578. if (!gfs2_dirent_sentinel(dent))
  579. offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  580. totlen = be16_to_cpu(dent->de_rec_len);
  581. BUG_ON(offset + name->len > totlen);
  582. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  583. ndent = (struct gfs2_dirent *)((char *)dent + offset);
  584. dent->de_rec_len = cpu_to_be16(offset);
  585. gfs2_qstr2dirent(name, totlen - offset, ndent);
  586. return ndent;
  587. }
  588. static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
  589. struct buffer_head *bh,
  590. const struct qstr *name)
  591. {
  592. struct gfs2_dirent *dent;
  593. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  594. gfs2_dirent_find_space, name, NULL);
  595. if (!dent || IS_ERR(dent))
  596. return dent;
  597. return gfs2_init_dirent(inode, dent, name, bh);
  598. }
  599. static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
  600. struct buffer_head **bhp)
  601. {
  602. int error;
  603. error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
  604. if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
  605. /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
  606. error = -EIO;
  607. }
  608. return error;
  609. }
  610. /**
  611. * get_leaf_nr - Get a leaf number associated with the index
  612. * @dip: The GFS2 inode
  613. * @index:
  614. * @leaf_out:
  615. *
  616. * Returns: 0 on success, error code otherwise
  617. */
  618. static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
  619. u64 *leaf_out)
  620. {
  621. __be64 leaf_no;
  622. int error;
  623. error = gfs2_dir_read_data(dip, (char *)&leaf_no,
  624. index * sizeof(__be64),
  625. sizeof(__be64), 0);
  626. if (error != sizeof(u64))
  627. return (error < 0) ? error : -EIO;
  628. *leaf_out = be64_to_cpu(leaf_no);
  629. return 0;
  630. }
  631. static int get_first_leaf(struct gfs2_inode *dip, u32 index,
  632. struct buffer_head **bh_out)
  633. {
  634. u64 leaf_no;
  635. int error;
  636. error = get_leaf_nr(dip, index, &leaf_no);
  637. if (!error)
  638. error = get_leaf(dip, leaf_no, bh_out);
  639. return error;
  640. }
  641. static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
  642. const struct qstr *name,
  643. gfs2_dscan_t scan,
  644. struct buffer_head **pbh)
  645. {
  646. struct buffer_head *bh;
  647. struct gfs2_dirent *dent;
  648. struct gfs2_inode *ip = GFS2_I(inode);
  649. int error;
  650. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  651. struct gfs2_leaf *leaf;
  652. unsigned hsize = 1 << ip->i_depth;
  653. unsigned index;
  654. u64 ln;
  655. if (hsize * sizeof(u64) != ip->i_disksize) {
  656. gfs2_consist_inode(ip);
  657. return ERR_PTR(-EIO);
  658. }
  659. index = name->hash >> (32 - ip->i_depth);
  660. error = get_first_leaf(ip, index, &bh);
  661. if (error)
  662. return ERR_PTR(error);
  663. do {
  664. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  665. scan, name, NULL);
  666. if (dent)
  667. goto got_dent;
  668. leaf = (struct gfs2_leaf *)bh->b_data;
  669. ln = be64_to_cpu(leaf->lf_next);
  670. brelse(bh);
  671. if (!ln)
  672. break;
  673. error = get_leaf(ip, ln, &bh);
  674. } while(!error);
  675. return error ? ERR_PTR(error) : NULL;
  676. }
  677. error = gfs2_meta_inode_buffer(ip, &bh);
  678. if (error)
  679. return ERR_PTR(error);
  680. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
  681. got_dent:
  682. if (unlikely(dent == NULL || IS_ERR(dent))) {
  683. brelse(bh);
  684. bh = NULL;
  685. }
  686. *pbh = bh;
  687. return dent;
  688. }
  689. static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
  690. {
  691. struct gfs2_inode *ip = GFS2_I(inode);
  692. unsigned int n = 1;
  693. u64 bn = gfs2_alloc_block(ip, &n);
  694. struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
  695. struct gfs2_leaf *leaf;
  696. struct gfs2_dirent *dent;
  697. struct qstr name = { .name = "", .len = 0, .hash = 0 };
  698. if (!bh)
  699. return NULL;
  700. gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
  701. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  702. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  703. leaf = (struct gfs2_leaf *)bh->b_data;
  704. leaf->lf_depth = cpu_to_be16(depth);
  705. leaf->lf_entries = 0;
  706. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  707. leaf->lf_next = 0;
  708. memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
  709. dent = (struct gfs2_dirent *)(leaf+1);
  710. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  711. *pbh = bh;
  712. return leaf;
  713. }
  714. /**
  715. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  716. * @dip: The GFS2 inode
  717. *
  718. * Returns: 0 on success, error code otherwise
  719. */
  720. static int dir_make_exhash(struct inode *inode)
  721. {
  722. struct gfs2_inode *dip = GFS2_I(inode);
  723. struct gfs2_sbd *sdp = GFS2_SB(inode);
  724. struct gfs2_dirent *dent;
  725. struct qstr args;
  726. struct buffer_head *bh, *dibh;
  727. struct gfs2_leaf *leaf;
  728. int y;
  729. u32 x;
  730. __be64 *lp;
  731. u64 bn;
  732. int error;
  733. error = gfs2_meta_inode_buffer(dip, &dibh);
  734. if (error)
  735. return error;
  736. /* Turn over a new leaf */
  737. leaf = new_leaf(inode, &bh, 0);
  738. if (!leaf)
  739. return -ENOSPC;
  740. bn = bh->b_blocknr;
  741. gfs2_assert(sdp, dip->i_entries < (1 << 16));
  742. leaf->lf_entries = cpu_to_be16(dip->i_entries);
  743. /* Copy dirents */
  744. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  745. sizeof(struct gfs2_dinode));
  746. /* Find last entry */
  747. x = 0;
  748. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  749. sizeof(struct gfs2_leaf);
  750. args.name = bh->b_data;
  751. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  752. gfs2_dirent_last, &args, NULL);
  753. if (!dent) {
  754. brelse(bh);
  755. brelse(dibh);
  756. return -EIO;
  757. }
  758. if (IS_ERR(dent)) {
  759. brelse(bh);
  760. brelse(dibh);
  761. return PTR_ERR(dent);
  762. }
  763. /* Adjust the last dirent's record length
  764. (Remember that dent still points to the last entry.) */
  765. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  766. sizeof(struct gfs2_dinode) -
  767. sizeof(struct gfs2_leaf));
  768. brelse(bh);
  769. /* We're done with the new leaf block, now setup the new
  770. hash table. */
  771. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  772. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  773. lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  774. for (x = sdp->sd_hash_ptrs; x--; lp++)
  775. *lp = cpu_to_be64(bn);
  776. dip->i_disksize = sdp->sd_sb.sb_bsize / 2;
  777. gfs2_add_inode_blocks(&dip->i_inode, 1);
  778. dip->i_diskflags |= GFS2_DIF_EXHASH;
  779. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  780. dip->i_depth = y;
  781. gfs2_dinode_out(dip, dibh->b_data);
  782. brelse(dibh);
  783. return 0;
  784. }
  785. /**
  786. * dir_split_leaf - Split a leaf block into two
  787. * @dip: The GFS2 inode
  788. * @index:
  789. * @leaf_no:
  790. *
  791. * Returns: 0 on success, error code on failure
  792. */
  793. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  794. {
  795. struct gfs2_inode *dip = GFS2_I(inode);
  796. struct buffer_head *nbh, *obh, *dibh;
  797. struct gfs2_leaf *nleaf, *oleaf;
  798. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  799. u32 start, len, half_len, divider;
  800. u64 bn, leaf_no;
  801. __be64 *lp;
  802. u32 index;
  803. int x, moved = 0;
  804. int error;
  805. index = name->hash >> (32 - dip->i_depth);
  806. error = get_leaf_nr(dip, index, &leaf_no);
  807. if (error)
  808. return error;
  809. /* Get the old leaf block */
  810. error = get_leaf(dip, leaf_no, &obh);
  811. if (error)
  812. return error;
  813. oleaf = (struct gfs2_leaf *)obh->b_data;
  814. if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
  815. brelse(obh);
  816. return 1; /* can't split */
  817. }
  818. gfs2_trans_add_bh(dip->i_gl, obh, 1);
  819. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  820. if (!nleaf) {
  821. brelse(obh);
  822. return -ENOSPC;
  823. }
  824. bn = nbh->b_blocknr;
  825. /* Compute the start and len of leaf pointers in the hash table. */
  826. len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
  827. half_len = len >> 1;
  828. if (!half_len) {
  829. printk(KERN_WARNING "i_depth %u lf_depth %u index %u\n", dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
  830. gfs2_consist_inode(dip);
  831. error = -EIO;
  832. goto fail_brelse;
  833. }
  834. start = (index & ~(len - 1));
  835. /* Change the pointers.
  836. Don't bother distinguishing stuffed from non-stuffed.
  837. This code is complicated enough already. */
  838. lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS | __GFP_NOFAIL);
  839. /* Change the pointers */
  840. for (x = 0; x < half_len; x++)
  841. lp[x] = cpu_to_be64(bn);
  842. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  843. half_len * sizeof(u64));
  844. if (error != half_len * sizeof(u64)) {
  845. if (error >= 0)
  846. error = -EIO;
  847. goto fail_lpfree;
  848. }
  849. kfree(lp);
  850. /* Compute the divider */
  851. divider = (start + half_len) << (32 - dip->i_depth);
  852. /* Copy the entries */
  853. dirent_first(dip, obh, &dent);
  854. do {
  855. next = dent;
  856. if (dirent_next(dip, obh, &next))
  857. next = NULL;
  858. if (!gfs2_dirent_sentinel(dent) &&
  859. be32_to_cpu(dent->de_hash) < divider) {
  860. struct qstr str;
  861. str.name = (char*)(dent+1);
  862. str.len = be16_to_cpu(dent->de_name_len);
  863. str.hash = be32_to_cpu(dent->de_hash);
  864. new = gfs2_dirent_alloc(inode, nbh, &str);
  865. if (IS_ERR(new)) {
  866. error = PTR_ERR(new);
  867. break;
  868. }
  869. new->de_inum = dent->de_inum; /* No endian worries */
  870. new->de_type = dent->de_type; /* No endian worries */
  871. be16_add_cpu(&nleaf->lf_entries, 1);
  872. dirent_del(dip, obh, prev, dent);
  873. if (!oleaf->lf_entries)
  874. gfs2_consist_inode(dip);
  875. be16_add_cpu(&oleaf->lf_entries, -1);
  876. if (!prev)
  877. prev = dent;
  878. moved = 1;
  879. } else {
  880. prev = dent;
  881. }
  882. dent = next;
  883. } while (dent);
  884. oleaf->lf_depth = nleaf->lf_depth;
  885. error = gfs2_meta_inode_buffer(dip, &dibh);
  886. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  887. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  888. gfs2_add_inode_blocks(&dip->i_inode, 1);
  889. gfs2_dinode_out(dip, dibh->b_data);
  890. brelse(dibh);
  891. }
  892. brelse(obh);
  893. brelse(nbh);
  894. return error;
  895. fail_lpfree:
  896. kfree(lp);
  897. fail_brelse:
  898. brelse(obh);
  899. brelse(nbh);
  900. return error;
  901. }
  902. /**
  903. * dir_double_exhash - Double size of ExHash table
  904. * @dip: The GFS2 dinode
  905. *
  906. * Returns: 0 on success, error code on failure
  907. */
  908. static int dir_double_exhash(struct gfs2_inode *dip)
  909. {
  910. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  911. struct buffer_head *dibh;
  912. u32 hsize;
  913. u64 *buf;
  914. u64 *from, *to;
  915. u64 block;
  916. int x;
  917. int error = 0;
  918. hsize = 1 << dip->i_depth;
  919. if (hsize * sizeof(u64) != dip->i_disksize) {
  920. gfs2_consist_inode(dip);
  921. return -EIO;
  922. }
  923. /* Allocate both the "from" and "to" buffers in one big chunk */
  924. buf = kcalloc(3, sdp->sd_hash_bsize, GFP_NOFS | __GFP_NOFAIL);
  925. for (block = dip->i_disksize >> sdp->sd_hash_bsize_shift; block--;) {
  926. error = gfs2_dir_read_data(dip, (char *)buf,
  927. block * sdp->sd_hash_bsize,
  928. sdp->sd_hash_bsize, 1);
  929. if (error != sdp->sd_hash_bsize) {
  930. if (error >= 0)
  931. error = -EIO;
  932. goto fail;
  933. }
  934. from = buf;
  935. to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
  936. for (x = sdp->sd_hash_ptrs; x--; from++) {
  937. *to++ = *from; /* No endianess worries */
  938. *to++ = *from;
  939. }
  940. error = gfs2_dir_write_data(dip,
  941. (char *)buf + sdp->sd_hash_bsize,
  942. block * sdp->sd_sb.sb_bsize,
  943. sdp->sd_sb.sb_bsize);
  944. if (error != sdp->sd_sb.sb_bsize) {
  945. if (error >= 0)
  946. error = -EIO;
  947. goto fail;
  948. }
  949. }
  950. kfree(buf);
  951. error = gfs2_meta_inode_buffer(dip, &dibh);
  952. if (!gfs2_assert_withdraw(sdp, !error)) {
  953. dip->i_depth++;
  954. gfs2_dinode_out(dip, dibh->b_data);
  955. brelse(dibh);
  956. }
  957. return error;
  958. fail:
  959. kfree(buf);
  960. return error;
  961. }
  962. /**
  963. * compare_dents - compare directory entries by hash value
  964. * @a: first dent
  965. * @b: second dent
  966. *
  967. * When comparing the hash entries of @a to @b:
  968. * gt: returns 1
  969. * lt: returns -1
  970. * eq: returns 0
  971. */
  972. static int compare_dents(const void *a, const void *b)
  973. {
  974. const struct gfs2_dirent *dent_a, *dent_b;
  975. u32 hash_a, hash_b;
  976. int ret = 0;
  977. dent_a = *(const struct gfs2_dirent **)a;
  978. hash_a = be32_to_cpu(dent_a->de_hash);
  979. dent_b = *(const struct gfs2_dirent **)b;
  980. hash_b = be32_to_cpu(dent_b->de_hash);
  981. if (hash_a > hash_b)
  982. ret = 1;
  983. else if (hash_a < hash_b)
  984. ret = -1;
  985. else {
  986. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  987. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  988. if (len_a > len_b)
  989. ret = 1;
  990. else if (len_a < len_b)
  991. ret = -1;
  992. else
  993. ret = memcmp(dent_a + 1, dent_b + 1, len_a);
  994. }
  995. return ret;
  996. }
  997. /**
  998. * do_filldir_main - read out directory entries
  999. * @dip: The GFS2 inode
  1000. * @offset: The offset in the file to read from
  1001. * @opaque: opaque data to pass to filldir
  1002. * @filldir: The function to pass entries to
  1003. * @darr: an array of struct gfs2_dirent pointers to read
  1004. * @entries: the number of entries in darr
  1005. * @copied: pointer to int that's non-zero if a entry has been copied out
  1006. *
  1007. * Jump through some hoops to make sure that if there are hash collsions,
  1008. * they are read out at the beginning of a buffer. We want to minimize
  1009. * the possibility that they will fall into different readdir buffers or
  1010. * that someone will want to seek to that location.
  1011. *
  1012. * Returns: errno, >0 on exception from filldir
  1013. */
  1014. static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
  1015. void *opaque, filldir_t filldir,
  1016. const struct gfs2_dirent **darr, u32 entries,
  1017. int *copied)
  1018. {
  1019. const struct gfs2_dirent *dent, *dent_next;
  1020. u64 off, off_next;
  1021. unsigned int x, y;
  1022. int run = 0;
  1023. int error = 0;
  1024. sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
  1025. dent_next = darr[0];
  1026. off_next = be32_to_cpu(dent_next->de_hash);
  1027. off_next = gfs2_disk_hash2offset(off_next);
  1028. for (x = 0, y = 1; x < entries; x++, y++) {
  1029. dent = dent_next;
  1030. off = off_next;
  1031. if (y < entries) {
  1032. dent_next = darr[y];
  1033. off_next = be32_to_cpu(dent_next->de_hash);
  1034. off_next = gfs2_disk_hash2offset(off_next);
  1035. if (off < *offset)
  1036. continue;
  1037. *offset = off;
  1038. if (off_next == off) {
  1039. if (*copied && !run)
  1040. return 1;
  1041. run = 1;
  1042. } else
  1043. run = 0;
  1044. } else {
  1045. if (off < *offset)
  1046. continue;
  1047. *offset = off;
  1048. }
  1049. error = filldir(opaque, (const char *)(dent + 1),
  1050. be16_to_cpu(dent->de_name_len),
  1051. off, be64_to_cpu(dent->de_inum.no_addr),
  1052. be16_to_cpu(dent->de_type));
  1053. if (error)
  1054. return 1;
  1055. *copied = 1;
  1056. }
  1057. /* Increment the *offset by one, so the next time we come into the
  1058. do_filldir fxn, we get the next entry instead of the last one in the
  1059. current leaf */
  1060. (*offset)++;
  1061. return 0;
  1062. }
  1063. static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
  1064. filldir_t filldir, int *copied, unsigned *depth,
  1065. u64 leaf_no)
  1066. {
  1067. struct gfs2_inode *ip = GFS2_I(inode);
  1068. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1069. struct buffer_head *bh;
  1070. struct gfs2_leaf *lf;
  1071. unsigned entries = 0, entries2 = 0;
  1072. unsigned leaves = 0;
  1073. const struct gfs2_dirent **darr, *dent;
  1074. struct dirent_gather g;
  1075. struct buffer_head **larr;
  1076. int leaf = 0;
  1077. int error, i;
  1078. u64 lfn = leaf_no;
  1079. do {
  1080. error = get_leaf(ip, lfn, &bh);
  1081. if (error)
  1082. goto out;
  1083. lf = (struct gfs2_leaf *)bh->b_data;
  1084. if (leaves == 0)
  1085. *depth = be16_to_cpu(lf->lf_depth);
  1086. entries += be16_to_cpu(lf->lf_entries);
  1087. leaves++;
  1088. lfn = be64_to_cpu(lf->lf_next);
  1089. brelse(bh);
  1090. } while(lfn);
  1091. if (!entries)
  1092. return 0;
  1093. error = -ENOMEM;
  1094. /*
  1095. * The extra 99 entries are not normally used, but are a buffer
  1096. * zone in case the number of entries in the leaf is corrupt.
  1097. * 99 is the maximum number of entries that can fit in a single
  1098. * leaf block.
  1099. */
  1100. larr = vmalloc((leaves + entries + 99) * sizeof(void *));
  1101. if (!larr)
  1102. goto out;
  1103. darr = (const struct gfs2_dirent **)(larr + leaves);
  1104. g.pdent = darr;
  1105. g.offset = 0;
  1106. lfn = leaf_no;
  1107. do {
  1108. error = get_leaf(ip, lfn, &bh);
  1109. if (error)
  1110. goto out_kfree;
  1111. lf = (struct gfs2_leaf *)bh->b_data;
  1112. lfn = be64_to_cpu(lf->lf_next);
  1113. if (lf->lf_entries) {
  1114. entries2 += be16_to_cpu(lf->lf_entries);
  1115. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1116. gfs2_dirent_gather, NULL, &g);
  1117. error = PTR_ERR(dent);
  1118. if (IS_ERR(dent))
  1119. goto out_kfree;
  1120. if (entries2 != g.offset) {
  1121. fs_warn(sdp, "Number of entries corrupt in dir "
  1122. "leaf %llu, entries2 (%u) != "
  1123. "g.offset (%u)\n",
  1124. (unsigned long long)bh->b_blocknr,
  1125. entries2, g.offset);
  1126. error = -EIO;
  1127. goto out_kfree;
  1128. }
  1129. error = 0;
  1130. larr[leaf++] = bh;
  1131. } else {
  1132. brelse(bh);
  1133. }
  1134. } while(lfn);
  1135. BUG_ON(entries2 != entries);
  1136. error = do_filldir_main(ip, offset, opaque, filldir, darr,
  1137. entries, copied);
  1138. out_kfree:
  1139. for(i = 0; i < leaf; i++)
  1140. brelse(larr[i]);
  1141. vfree(larr);
  1142. out:
  1143. return error;
  1144. }
  1145. /**
  1146. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1147. * @dip: dinode pointer
  1148. * @offset: the hash of the last entry read shifted to the right once
  1149. * @opaque: buffer for the filldir function to fill
  1150. * @filldir: points to the filldir function to use
  1151. *
  1152. * Returns: errno
  1153. */
  1154. static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
  1155. filldir_t filldir)
  1156. {
  1157. struct gfs2_inode *dip = GFS2_I(inode);
  1158. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1159. u32 hsize, len = 0;
  1160. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1161. u32 hash, index;
  1162. __be64 *lp;
  1163. int copied = 0;
  1164. int error = 0;
  1165. unsigned depth = 0;
  1166. hsize = 1 << dip->i_depth;
  1167. if (hsize * sizeof(u64) != dip->i_disksize) {
  1168. gfs2_consist_inode(dip);
  1169. return -EIO;
  1170. }
  1171. hash = gfs2_dir_offset2hash(*offset);
  1172. index = hash >> (32 - dip->i_depth);
  1173. lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
  1174. if (!lp)
  1175. return -ENOMEM;
  1176. while (index < hsize) {
  1177. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1178. ht_offset = index - lp_offset;
  1179. if (ht_offset_cur != ht_offset) {
  1180. error = gfs2_dir_read_data(dip, (char *)lp,
  1181. ht_offset * sizeof(__be64),
  1182. sdp->sd_hash_bsize, 1);
  1183. if (error != sdp->sd_hash_bsize) {
  1184. if (error >= 0)
  1185. error = -EIO;
  1186. goto out;
  1187. }
  1188. ht_offset_cur = ht_offset;
  1189. }
  1190. error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
  1191. &copied, &depth,
  1192. be64_to_cpu(lp[lp_offset]));
  1193. if (error)
  1194. break;
  1195. len = 1 << (dip->i_depth - depth);
  1196. index = (index & ~(len - 1)) + len;
  1197. }
  1198. out:
  1199. kfree(lp);
  1200. if (error > 0)
  1201. error = 0;
  1202. return error;
  1203. }
  1204. int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
  1205. filldir_t filldir)
  1206. {
  1207. struct gfs2_inode *dip = GFS2_I(inode);
  1208. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1209. struct dirent_gather g;
  1210. const struct gfs2_dirent **darr, *dent;
  1211. struct buffer_head *dibh;
  1212. int copied = 0;
  1213. int error;
  1214. if (!dip->i_entries)
  1215. return 0;
  1216. if (dip->i_diskflags & GFS2_DIF_EXHASH)
  1217. return dir_e_read(inode, offset, opaque, filldir);
  1218. if (!gfs2_is_stuffed(dip)) {
  1219. gfs2_consist_inode(dip);
  1220. return -EIO;
  1221. }
  1222. error = gfs2_meta_inode_buffer(dip, &dibh);
  1223. if (error)
  1224. return error;
  1225. error = -ENOMEM;
  1226. /* 96 is max number of dirents which can be stuffed into an inode */
  1227. darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
  1228. if (darr) {
  1229. g.pdent = darr;
  1230. g.offset = 0;
  1231. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1232. gfs2_dirent_gather, NULL, &g);
  1233. if (IS_ERR(dent)) {
  1234. error = PTR_ERR(dent);
  1235. goto out;
  1236. }
  1237. if (dip->i_entries != g.offset) {
  1238. fs_warn(sdp, "Number of entries corrupt in dir %llu, "
  1239. "ip->i_entries (%u) != g.offset (%u)\n",
  1240. (unsigned long long)dip->i_no_addr,
  1241. dip->i_entries,
  1242. g.offset);
  1243. error = -EIO;
  1244. goto out;
  1245. }
  1246. error = do_filldir_main(dip, offset, opaque, filldir, darr,
  1247. dip->i_entries, &copied);
  1248. out:
  1249. kfree(darr);
  1250. }
  1251. if (error > 0)
  1252. error = 0;
  1253. brelse(dibh);
  1254. return error;
  1255. }
  1256. /**
  1257. * gfs2_dir_search - Search a directory
  1258. * @dip: The GFS2 inode
  1259. * @filename:
  1260. * @inode:
  1261. *
  1262. * This routine searches a directory for a file or another directory.
  1263. * Assumes a glock is held on dip.
  1264. *
  1265. * Returns: errno
  1266. */
  1267. struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name)
  1268. {
  1269. struct buffer_head *bh;
  1270. struct gfs2_dirent *dent;
  1271. struct inode *inode;
  1272. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1273. if (dent) {
  1274. if (IS_ERR(dent))
  1275. return ERR_CAST(dent);
  1276. inode = gfs2_inode_lookup(dir->i_sb,
  1277. be16_to_cpu(dent->de_type),
  1278. be64_to_cpu(dent->de_inum.no_addr),
  1279. be64_to_cpu(dent->de_inum.no_formal_ino), 0);
  1280. brelse(bh);
  1281. return inode;
  1282. }
  1283. return ERR_PTR(-ENOENT);
  1284. }
  1285. int gfs2_dir_check(struct inode *dir, const struct qstr *name,
  1286. const struct gfs2_inode *ip)
  1287. {
  1288. struct buffer_head *bh;
  1289. struct gfs2_dirent *dent;
  1290. int ret = -ENOENT;
  1291. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1292. if (dent) {
  1293. if (IS_ERR(dent))
  1294. return PTR_ERR(dent);
  1295. if (ip) {
  1296. if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
  1297. goto out;
  1298. if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
  1299. ip->i_no_formal_ino)
  1300. goto out;
  1301. if (unlikely(IF2DT(ip->i_inode.i_mode) !=
  1302. be16_to_cpu(dent->de_type))) {
  1303. gfs2_consist_inode(GFS2_I(dir));
  1304. ret = -EIO;
  1305. goto out;
  1306. }
  1307. }
  1308. ret = 0;
  1309. out:
  1310. brelse(bh);
  1311. }
  1312. return ret;
  1313. }
  1314. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1315. {
  1316. struct buffer_head *bh, *obh;
  1317. struct gfs2_inode *ip = GFS2_I(inode);
  1318. struct gfs2_leaf *leaf, *oleaf;
  1319. int error;
  1320. u32 index;
  1321. u64 bn;
  1322. index = name->hash >> (32 - ip->i_depth);
  1323. error = get_first_leaf(ip, index, &obh);
  1324. if (error)
  1325. return error;
  1326. do {
  1327. oleaf = (struct gfs2_leaf *)obh->b_data;
  1328. bn = be64_to_cpu(oleaf->lf_next);
  1329. if (!bn)
  1330. break;
  1331. brelse(obh);
  1332. error = get_leaf(ip, bn, &obh);
  1333. if (error)
  1334. return error;
  1335. } while(1);
  1336. gfs2_trans_add_bh(ip->i_gl, obh, 1);
  1337. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1338. if (!leaf) {
  1339. brelse(obh);
  1340. return -ENOSPC;
  1341. }
  1342. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1343. brelse(bh);
  1344. brelse(obh);
  1345. error = gfs2_meta_inode_buffer(ip, &bh);
  1346. if (error)
  1347. return error;
  1348. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1349. gfs2_add_inode_blocks(&ip->i_inode, 1);
  1350. gfs2_dinode_out(ip, bh->b_data);
  1351. brelse(bh);
  1352. return 0;
  1353. }
  1354. /**
  1355. * gfs2_dir_add - Add new filename into directory
  1356. * @dip: The GFS2 inode
  1357. * @filename: The new name
  1358. * @inode: The inode number of the entry
  1359. * @type: The type of the entry
  1360. *
  1361. * Returns: 0 on success, error code on failure
  1362. */
  1363. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1364. const struct gfs2_inode *nip, unsigned type)
  1365. {
  1366. struct gfs2_inode *ip = GFS2_I(inode);
  1367. struct buffer_head *bh;
  1368. struct gfs2_dirent *dent;
  1369. struct gfs2_leaf *leaf;
  1370. int error;
  1371. while(1) {
  1372. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
  1373. &bh);
  1374. if (dent) {
  1375. if (IS_ERR(dent))
  1376. return PTR_ERR(dent);
  1377. dent = gfs2_init_dirent(inode, dent, name, bh);
  1378. gfs2_inum_out(nip, dent);
  1379. dent->de_type = cpu_to_be16(type);
  1380. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  1381. leaf = (struct gfs2_leaf *)bh->b_data;
  1382. be16_add_cpu(&leaf->lf_entries, 1);
  1383. }
  1384. brelse(bh);
  1385. error = gfs2_meta_inode_buffer(ip, &bh);
  1386. if (error)
  1387. break;
  1388. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1389. ip->i_entries++;
  1390. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1391. gfs2_dinode_out(ip, bh->b_data);
  1392. brelse(bh);
  1393. error = 0;
  1394. break;
  1395. }
  1396. if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
  1397. error = dir_make_exhash(inode);
  1398. if (error)
  1399. break;
  1400. continue;
  1401. }
  1402. error = dir_split_leaf(inode, name);
  1403. if (error == 0)
  1404. continue;
  1405. if (error < 0)
  1406. break;
  1407. if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
  1408. error = dir_double_exhash(ip);
  1409. if (error)
  1410. break;
  1411. error = dir_split_leaf(inode, name);
  1412. if (error < 0)
  1413. break;
  1414. if (error == 0)
  1415. continue;
  1416. }
  1417. error = dir_new_leaf(inode, name);
  1418. if (!error)
  1419. continue;
  1420. error = -ENOSPC;
  1421. break;
  1422. }
  1423. return error;
  1424. }
  1425. /**
  1426. * gfs2_dir_del - Delete a directory entry
  1427. * @dip: The GFS2 inode
  1428. * @filename: The filename
  1429. *
  1430. * Returns: 0 on success, error code on failure
  1431. */
  1432. int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
  1433. {
  1434. struct gfs2_dirent *dent, *prev = NULL;
  1435. struct buffer_head *bh;
  1436. int error;
  1437. /* Returns _either_ the entry (if its first in block) or the
  1438. previous entry otherwise */
  1439. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1440. if (!dent) {
  1441. gfs2_consist_inode(dip);
  1442. return -EIO;
  1443. }
  1444. if (IS_ERR(dent)) {
  1445. gfs2_consist_inode(dip);
  1446. return PTR_ERR(dent);
  1447. }
  1448. /* If not first in block, adjust pointers accordingly */
  1449. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1450. prev = dent;
  1451. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1452. }
  1453. dirent_del(dip, bh, prev, dent);
  1454. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1455. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1456. u16 entries = be16_to_cpu(leaf->lf_entries);
  1457. if (!entries)
  1458. gfs2_consist_inode(dip);
  1459. leaf->lf_entries = cpu_to_be16(--entries);
  1460. }
  1461. brelse(bh);
  1462. error = gfs2_meta_inode_buffer(dip, &bh);
  1463. if (error)
  1464. return error;
  1465. if (!dip->i_entries)
  1466. gfs2_consist_inode(dip);
  1467. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1468. dip->i_entries--;
  1469. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1470. gfs2_dinode_out(dip, bh->b_data);
  1471. brelse(bh);
  1472. mark_inode_dirty(&dip->i_inode);
  1473. return error;
  1474. }
  1475. /**
  1476. * gfs2_dir_mvino - Change inode number of directory entry
  1477. * @dip: The GFS2 inode
  1478. * @filename:
  1479. * @new_inode:
  1480. *
  1481. * This routine changes the inode number of a directory entry. It's used
  1482. * by rename to change ".." when a directory is moved.
  1483. * Assumes a glock is held on dvp.
  1484. *
  1485. * Returns: errno
  1486. */
  1487. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1488. const struct gfs2_inode *nip, unsigned int new_type)
  1489. {
  1490. struct buffer_head *bh;
  1491. struct gfs2_dirent *dent;
  1492. int error;
  1493. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1494. if (!dent) {
  1495. gfs2_consist_inode(dip);
  1496. return -EIO;
  1497. }
  1498. if (IS_ERR(dent))
  1499. return PTR_ERR(dent);
  1500. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1501. gfs2_inum_out(nip, dent);
  1502. dent->de_type = cpu_to_be16(new_type);
  1503. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1504. brelse(bh);
  1505. error = gfs2_meta_inode_buffer(dip, &bh);
  1506. if (error)
  1507. return error;
  1508. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1509. }
  1510. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1511. gfs2_dinode_out(dip, bh->b_data);
  1512. brelse(bh);
  1513. return 0;
  1514. }
  1515. /**
  1516. * foreach_leaf - call a function for each leaf in a directory
  1517. * @dip: the directory
  1518. * @lc: the function to call for each each
  1519. * @data: private data to pass to it
  1520. *
  1521. * Returns: errno
  1522. */
  1523. static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
  1524. {
  1525. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1526. struct buffer_head *bh;
  1527. struct gfs2_leaf *leaf;
  1528. u32 hsize, len;
  1529. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1530. u32 index = 0;
  1531. __be64 *lp;
  1532. u64 leaf_no;
  1533. int error = 0;
  1534. hsize = 1 << dip->i_depth;
  1535. if (hsize * sizeof(u64) != dip->i_disksize) {
  1536. gfs2_consist_inode(dip);
  1537. return -EIO;
  1538. }
  1539. lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
  1540. if (!lp)
  1541. return -ENOMEM;
  1542. while (index < hsize) {
  1543. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1544. ht_offset = index - lp_offset;
  1545. if (ht_offset_cur != ht_offset) {
  1546. error = gfs2_dir_read_data(dip, (char *)lp,
  1547. ht_offset * sizeof(__be64),
  1548. sdp->sd_hash_bsize, 1);
  1549. if (error != sdp->sd_hash_bsize) {
  1550. if (error >= 0)
  1551. error = -EIO;
  1552. goto out;
  1553. }
  1554. ht_offset_cur = ht_offset;
  1555. }
  1556. leaf_no = be64_to_cpu(lp[lp_offset]);
  1557. if (leaf_no) {
  1558. error = get_leaf(dip, leaf_no, &bh);
  1559. if (error)
  1560. goto out;
  1561. leaf = (struct gfs2_leaf *)bh->b_data;
  1562. len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
  1563. brelse(bh);
  1564. error = lc(dip, index, len, leaf_no, data);
  1565. if (error)
  1566. goto out;
  1567. index = (index & ~(len - 1)) + len;
  1568. } else
  1569. index++;
  1570. }
  1571. if (index != hsize) {
  1572. gfs2_consist_inode(dip);
  1573. error = -EIO;
  1574. }
  1575. out:
  1576. kfree(lp);
  1577. return error;
  1578. }
  1579. /**
  1580. * leaf_dealloc - Deallocate a directory leaf
  1581. * @dip: the directory
  1582. * @index: the hash table offset in the directory
  1583. * @len: the number of pointers to this leaf
  1584. * @leaf_no: the leaf number
  1585. * @data: not used
  1586. *
  1587. * Returns: errno
  1588. */
  1589. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1590. u64 leaf_no, void *data)
  1591. {
  1592. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1593. struct gfs2_leaf *tmp_leaf;
  1594. struct gfs2_rgrp_list rlist;
  1595. struct buffer_head *bh, *dibh;
  1596. u64 blk, nblk;
  1597. unsigned int rg_blocks = 0, l_blocks = 0;
  1598. char *ht;
  1599. unsigned int x, size = len * sizeof(u64);
  1600. int error;
  1601. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1602. ht = kzalloc(size, GFP_NOFS);
  1603. if (!ht)
  1604. return -ENOMEM;
  1605. if (!gfs2_alloc_get(dip)) {
  1606. error = -ENOMEM;
  1607. goto out;
  1608. }
  1609. error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1610. if (error)
  1611. goto out_put;
  1612. error = gfs2_rindex_hold(sdp, &dip->i_alloc->al_ri_gh);
  1613. if (error)
  1614. goto out_qs;
  1615. /* Count the number of leaves */
  1616. for (blk = leaf_no; blk; blk = nblk) {
  1617. error = get_leaf(dip, blk, &bh);
  1618. if (error)
  1619. goto out_rlist;
  1620. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1621. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1622. brelse(bh);
  1623. gfs2_rlist_add(sdp, &rlist, blk);
  1624. l_blocks++;
  1625. }
  1626. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1627. for (x = 0; x < rlist.rl_rgrps; x++) {
  1628. struct gfs2_rgrpd *rgd;
  1629. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1630. rg_blocks += rgd->rd_length;
  1631. }
  1632. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1633. if (error)
  1634. goto out_rlist;
  1635. error = gfs2_trans_begin(sdp,
  1636. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1637. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1638. if (error)
  1639. goto out_rg_gunlock;
  1640. for (blk = leaf_no; blk; blk = nblk) {
  1641. error = get_leaf(dip, blk, &bh);
  1642. if (error)
  1643. goto out_end_trans;
  1644. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1645. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1646. brelse(bh);
  1647. gfs2_free_meta(dip, blk, 1);
  1648. gfs2_add_inode_blocks(&dip->i_inode, -1);
  1649. }
  1650. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1651. if (error != size) {
  1652. if (error >= 0)
  1653. error = -EIO;
  1654. goto out_end_trans;
  1655. }
  1656. error = gfs2_meta_inode_buffer(dip, &dibh);
  1657. if (error)
  1658. goto out_end_trans;
  1659. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  1660. gfs2_dinode_out(dip, dibh->b_data);
  1661. brelse(dibh);
  1662. out_end_trans:
  1663. gfs2_trans_end(sdp);
  1664. out_rg_gunlock:
  1665. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1666. out_rlist:
  1667. gfs2_rlist_free(&rlist);
  1668. gfs2_glock_dq_uninit(&dip->i_alloc->al_ri_gh);
  1669. out_qs:
  1670. gfs2_quota_unhold(dip);
  1671. out_put:
  1672. gfs2_alloc_put(dip);
  1673. out:
  1674. kfree(ht);
  1675. return error;
  1676. }
  1677. /**
  1678. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1679. * @dip: the directory
  1680. *
  1681. * Dealloc all on-disk directory leaves to FREEMETA state
  1682. * Change on-disk inode type to "regular file"
  1683. *
  1684. * Returns: errno
  1685. */
  1686. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1687. {
  1688. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1689. struct buffer_head *bh;
  1690. int error;
  1691. /* Dealloc on-disk leaves to FREEMETA state */
  1692. error = foreach_leaf(dip, leaf_dealloc, NULL);
  1693. if (error)
  1694. return error;
  1695. /* Make this a regular file in case we crash.
  1696. (We don't want to free these blocks a second time.) */
  1697. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1698. if (error)
  1699. return error;
  1700. error = gfs2_meta_inode_buffer(dip, &bh);
  1701. if (!error) {
  1702. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1703. ((struct gfs2_dinode *)bh->b_data)->di_mode =
  1704. cpu_to_be32(S_IFREG);
  1705. brelse(bh);
  1706. }
  1707. gfs2_trans_end(sdp);
  1708. return error;
  1709. }
  1710. /**
  1711. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1712. * @ip: the file being written to
  1713. * @filname: the filename that's going to be added
  1714. *
  1715. * Returns: 1 if alloc required, 0 if not, -ve on error
  1716. */
  1717. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
  1718. {
  1719. struct gfs2_dirent *dent;
  1720. struct buffer_head *bh;
  1721. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1722. if (!dent) {
  1723. return 1;
  1724. }
  1725. if (IS_ERR(dent))
  1726. return PTR_ERR(dent);
  1727. brelse(bh);
  1728. return 0;
  1729. }