dir.c 46 KB

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