dir.c 46 KB

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