dir.c 48 KB

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