dir.c 47 KB

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