dir.c 46 KB

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