dir.c 49 KB

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