dir.c 47 KB

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