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_di.di_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_di.di_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_meta(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_bh(ip->i_gl, bh, 1);
  701. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  702. leaf = (struct gfs2_leaf *)bh->b_data;
  703. leaf->lf_depth = cpu_to_be16(depth);
  704. leaf->lf_entries = 0;
  705. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  706. leaf->lf_next = 0;
  707. memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
  708. dent = (struct gfs2_dirent *)(leaf+1);
  709. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  710. *pbh = bh;
  711. return leaf;
  712. }
  713. /**
  714. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  715. * @dip: The GFS2 inode
  716. *
  717. * Returns: 0 on success, error code otherwise
  718. */
  719. static int dir_make_exhash(struct inode *inode)
  720. {
  721. struct gfs2_inode *dip = GFS2_I(inode);
  722. struct gfs2_sbd *sdp = GFS2_SB(inode);
  723. struct gfs2_dirent *dent;
  724. struct qstr args;
  725. struct buffer_head *bh, *dibh;
  726. struct gfs2_leaf *leaf;
  727. int y;
  728. u32 x;
  729. __be64 *lp;
  730. u64 bn;
  731. int error;
  732. error = gfs2_meta_inode_buffer(dip, &dibh);
  733. if (error)
  734. return error;
  735. /* Turn over a new leaf */
  736. leaf = new_leaf(inode, &bh, 0);
  737. if (!leaf)
  738. return -ENOSPC;
  739. bn = bh->b_blocknr;
  740. gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
  741. leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
  742. /* Copy dirents */
  743. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  744. sizeof(struct gfs2_dinode));
  745. /* Find last entry */
  746. x = 0;
  747. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  748. sizeof(struct gfs2_leaf);
  749. args.name = bh->b_data;
  750. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  751. gfs2_dirent_last, &args, NULL);
  752. if (!dent) {
  753. brelse(bh);
  754. brelse(dibh);
  755. return -EIO;
  756. }
  757. if (IS_ERR(dent)) {
  758. brelse(bh);
  759. brelse(dibh);
  760. return PTR_ERR(dent);
  761. }
  762. /* Adjust the last dirent's record length
  763. (Remember that dent still points to the last entry.) */
  764. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  765. sizeof(struct gfs2_dinode) -
  766. sizeof(struct gfs2_leaf));
  767. brelse(bh);
  768. /* We're done with the new leaf block, now setup the new
  769. hash table. */
  770. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  771. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  772. lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  773. for (x = sdp->sd_hash_ptrs; x--; lp++)
  774. *lp = cpu_to_be64(bn);
  775. dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
  776. dip->i_di.di_blocks++;
  777. gfs2_set_inode_blocks(&dip->i_inode);
  778. dip->i_di.di_flags |= GFS2_DIF_EXHASH;
  779. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  780. dip->i_di.di_depth = y;
  781. gfs2_dinode_out(dip, dibh->b_data);
  782. brelse(dibh);
  783. return 0;
  784. }
  785. /**
  786. * dir_split_leaf - Split a leaf block into two
  787. * @dip: The GFS2 inode
  788. * @index:
  789. * @leaf_no:
  790. *
  791. * Returns: 0 on success, error code on failure
  792. */
  793. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  794. {
  795. struct gfs2_inode *dip = GFS2_I(inode);
  796. struct buffer_head *nbh, *obh, *dibh;
  797. struct gfs2_leaf *nleaf, *oleaf;
  798. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  799. u32 start, len, half_len, divider;
  800. u64 bn, leaf_no;
  801. __be64 *lp;
  802. u32 index;
  803. int x, moved = 0;
  804. int error;
  805. index = name->hash >> (32 - dip->i_di.di_depth);
  806. error = get_leaf_nr(dip, index, &leaf_no);
  807. if (error)
  808. return error;
  809. /* Get the old leaf block */
  810. error = get_leaf(dip, leaf_no, &obh);
  811. if (error)
  812. return error;
  813. oleaf = (struct gfs2_leaf *)obh->b_data;
  814. if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
  815. brelse(obh);
  816. return 1; /* can't split */
  817. }
  818. gfs2_trans_add_bh(dip->i_gl, obh, 1);
  819. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  820. if (!nleaf) {
  821. brelse(obh);
  822. return -ENOSPC;
  823. }
  824. bn = nbh->b_blocknr;
  825. /* Compute the start and len of leaf pointers in the hash table. */
  826. len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
  827. half_len = len >> 1;
  828. if (!half_len) {
  829. printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
  830. gfs2_consist_inode(dip);
  831. error = -EIO;
  832. goto fail_brelse;
  833. }
  834. start = (index & ~(len - 1));
  835. /* Change the pointers.
  836. Don't bother distinguishing stuffed from non-stuffed.
  837. This code is complicated enough already. */
  838. lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS | __GFP_NOFAIL);
  839. /* Change the pointers */
  840. for (x = 0; x < half_len; x++)
  841. lp[x] = cpu_to_be64(bn);
  842. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  843. half_len * sizeof(u64));
  844. if (error != half_len * sizeof(u64)) {
  845. if (error >= 0)
  846. error = -EIO;
  847. goto fail_lpfree;
  848. }
  849. kfree(lp);
  850. /* Compute the divider */
  851. divider = (start + half_len) << (32 - dip->i_di.di_depth);
  852. /* Copy the entries */
  853. dirent_first(dip, obh, &dent);
  854. do {
  855. next = dent;
  856. if (dirent_next(dip, obh, &next))
  857. next = NULL;
  858. if (!gfs2_dirent_sentinel(dent) &&
  859. be32_to_cpu(dent->de_hash) < divider) {
  860. struct qstr str;
  861. str.name = (char*)(dent+1);
  862. str.len = be16_to_cpu(dent->de_name_len);
  863. str.hash = be32_to_cpu(dent->de_hash);
  864. new = gfs2_dirent_alloc(inode, nbh, &str);
  865. if (IS_ERR(new)) {
  866. error = PTR_ERR(new);
  867. break;
  868. }
  869. new->de_inum = dent->de_inum; /* No endian worries */
  870. new->de_type = dent->de_type; /* No endian worries */
  871. nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
  872. dirent_del(dip, obh, prev, dent);
  873. if (!oleaf->lf_entries)
  874. gfs2_consist_inode(dip);
  875. oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
  876. if (!prev)
  877. prev = dent;
  878. moved = 1;
  879. } else {
  880. prev = dent;
  881. }
  882. dent = next;
  883. } while (dent);
  884. oleaf->lf_depth = nleaf->lf_depth;
  885. error = gfs2_meta_inode_buffer(dip, &dibh);
  886. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  887. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  888. dip->i_di.di_blocks++;
  889. gfs2_set_inode_blocks(&dip->i_inode);
  890. gfs2_dinode_out(dip, dibh->b_data);
  891. brelse(dibh);
  892. }
  893. brelse(obh);
  894. brelse(nbh);
  895. return error;
  896. fail_lpfree:
  897. kfree(lp);
  898. fail_brelse:
  899. brelse(obh);
  900. brelse(nbh);
  901. return error;
  902. }
  903. /**
  904. * dir_double_exhash - Double size of ExHash table
  905. * @dip: The GFS2 dinode
  906. *
  907. * Returns: 0 on success, error code on failure
  908. */
  909. static int dir_double_exhash(struct gfs2_inode *dip)
  910. {
  911. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  912. struct buffer_head *dibh;
  913. u32 hsize;
  914. u64 *buf;
  915. u64 *from, *to;
  916. u64 block;
  917. int x;
  918. int error = 0;
  919. hsize = 1 << dip->i_di.di_depth;
  920. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  921. gfs2_consist_inode(dip);
  922. return -EIO;
  923. }
  924. /* Allocate both the "from" and "to" buffers in one big chunk */
  925. buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
  926. for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
  927. error = gfs2_dir_read_data(dip, (char *)buf,
  928. block * sdp->sd_hash_bsize,
  929. sdp->sd_hash_bsize, 1);
  930. if (error != sdp->sd_hash_bsize) {
  931. if (error >= 0)
  932. error = -EIO;
  933. goto fail;
  934. }
  935. from = buf;
  936. to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
  937. for (x = sdp->sd_hash_ptrs; x--; from++) {
  938. *to++ = *from; /* No endianess worries */
  939. *to++ = *from;
  940. }
  941. error = gfs2_dir_write_data(dip,
  942. (char *)buf + sdp->sd_hash_bsize,
  943. block * sdp->sd_sb.sb_bsize,
  944. sdp->sd_sb.sb_bsize);
  945. if (error != sdp->sd_sb.sb_bsize) {
  946. if (error >= 0)
  947. error = -EIO;
  948. goto fail;
  949. }
  950. }
  951. kfree(buf);
  952. error = gfs2_meta_inode_buffer(dip, &dibh);
  953. if (!gfs2_assert_withdraw(sdp, !error)) {
  954. dip->i_di.di_depth++;
  955. gfs2_dinode_out(dip, dibh->b_data);
  956. brelse(dibh);
  957. }
  958. return error;
  959. fail:
  960. kfree(buf);
  961. return error;
  962. }
  963. /**
  964. * compare_dents - compare directory entries by hash value
  965. * @a: first dent
  966. * @b: second dent
  967. *
  968. * When comparing the hash entries of @a to @b:
  969. * gt: returns 1
  970. * lt: returns -1
  971. * eq: returns 0
  972. */
  973. static int compare_dents(const void *a, const void *b)
  974. {
  975. const struct gfs2_dirent *dent_a, *dent_b;
  976. u32 hash_a, hash_b;
  977. int ret = 0;
  978. dent_a = *(const struct gfs2_dirent **)a;
  979. hash_a = be32_to_cpu(dent_a->de_hash);
  980. dent_b = *(const struct gfs2_dirent **)b;
  981. hash_b = be32_to_cpu(dent_b->de_hash);
  982. if (hash_a > hash_b)
  983. ret = 1;
  984. else if (hash_a < hash_b)
  985. ret = -1;
  986. else {
  987. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  988. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  989. if (len_a > len_b)
  990. ret = 1;
  991. else if (len_a < len_b)
  992. ret = -1;
  993. else
  994. ret = memcmp(dent_a + 1, dent_b + 1, len_a);
  995. }
  996. return ret;
  997. }
  998. /**
  999. * do_filldir_main - read out directory entries
  1000. * @dip: The GFS2 inode
  1001. * @offset: The offset in the file to read from
  1002. * @opaque: opaque data to pass to filldir
  1003. * @filldir: The function to pass entries to
  1004. * @darr: an array of struct gfs2_dirent pointers to read
  1005. * @entries: the number of entries in darr
  1006. * @copied: pointer to int that's non-zero if a entry has been copied out
  1007. *
  1008. * Jump through some hoops to make sure that if there are hash collsions,
  1009. * they are read out at the beginning of a buffer. We want to minimize
  1010. * the possibility that they will fall into different readdir buffers or
  1011. * that someone will want to seek to that location.
  1012. *
  1013. * Returns: errno, >0 on exception from filldir
  1014. */
  1015. static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
  1016. void *opaque, filldir_t filldir,
  1017. const struct gfs2_dirent **darr, u32 entries,
  1018. int *copied)
  1019. {
  1020. const struct gfs2_dirent *dent, *dent_next;
  1021. u64 off, off_next;
  1022. unsigned int x, y;
  1023. int run = 0;
  1024. int error = 0;
  1025. sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
  1026. dent_next = darr[0];
  1027. off_next = be32_to_cpu(dent_next->de_hash);
  1028. off_next = gfs2_disk_hash2offset(off_next);
  1029. for (x = 0, y = 1; x < entries; x++, y++) {
  1030. dent = dent_next;
  1031. off = off_next;
  1032. if (y < entries) {
  1033. dent_next = darr[y];
  1034. off_next = be32_to_cpu(dent_next->de_hash);
  1035. off_next = gfs2_disk_hash2offset(off_next);
  1036. if (off < *offset)
  1037. continue;
  1038. *offset = off;
  1039. if (off_next == off) {
  1040. if (*copied && !run)
  1041. return 1;
  1042. run = 1;
  1043. } else
  1044. run = 0;
  1045. } else {
  1046. if (off < *offset)
  1047. continue;
  1048. *offset = off;
  1049. }
  1050. error = filldir(opaque, (const char *)(dent + 1),
  1051. be16_to_cpu(dent->de_name_len),
  1052. off, be64_to_cpu(dent->de_inum.no_addr),
  1053. be16_to_cpu(dent->de_type));
  1054. if (error)
  1055. return 1;
  1056. *copied = 1;
  1057. }
  1058. /* Increment the *offset by one, so the next time we come into the
  1059. do_filldir fxn, we get the next entry instead of the last one in the
  1060. current leaf */
  1061. (*offset)++;
  1062. return 0;
  1063. }
  1064. static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
  1065. filldir_t filldir, int *copied, unsigned *depth,
  1066. u64 leaf_no)
  1067. {
  1068. struct gfs2_inode *ip = GFS2_I(inode);
  1069. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1070. struct buffer_head *bh;
  1071. struct gfs2_leaf *lf;
  1072. unsigned entries = 0, entries2 = 0;
  1073. unsigned leaves = 0;
  1074. const struct gfs2_dirent **darr, *dent;
  1075. struct dirent_gather g;
  1076. struct buffer_head **larr;
  1077. int leaf = 0;
  1078. int error, i;
  1079. u64 lfn = leaf_no;
  1080. do {
  1081. error = get_leaf(ip, lfn, &bh);
  1082. if (error)
  1083. goto out;
  1084. lf = (struct gfs2_leaf *)bh->b_data;
  1085. if (leaves == 0)
  1086. *depth = be16_to_cpu(lf->lf_depth);
  1087. entries += be16_to_cpu(lf->lf_entries);
  1088. leaves++;
  1089. lfn = be64_to_cpu(lf->lf_next);
  1090. brelse(bh);
  1091. } while(lfn);
  1092. if (!entries)
  1093. return 0;
  1094. error = -ENOMEM;
  1095. /*
  1096. * The extra 99 entries are not normally used, but are a buffer
  1097. * zone in case the number of entries in the leaf is corrupt.
  1098. * 99 is the maximum number of entries that can fit in a single
  1099. * leaf block.
  1100. */
  1101. larr = vmalloc((leaves + entries + 99) * sizeof(void *));
  1102. if (!larr)
  1103. goto out;
  1104. darr = (const struct gfs2_dirent **)(larr + leaves);
  1105. g.pdent = darr;
  1106. g.offset = 0;
  1107. lfn = leaf_no;
  1108. do {
  1109. error = get_leaf(ip, lfn, &bh);
  1110. if (error)
  1111. goto out_kfree;
  1112. lf = (struct gfs2_leaf *)bh->b_data;
  1113. lfn = be64_to_cpu(lf->lf_next);
  1114. if (lf->lf_entries) {
  1115. entries2 += be16_to_cpu(lf->lf_entries);
  1116. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1117. gfs2_dirent_gather, NULL, &g);
  1118. error = PTR_ERR(dent);
  1119. if (IS_ERR(dent))
  1120. goto out_kfree;
  1121. if (entries2 != g.offset) {
  1122. fs_warn(sdp, "Number of entries corrupt in dir "
  1123. "leaf %llu, entries2 (%u) != "
  1124. "g.offset (%u)\n",
  1125. (unsigned long long)bh->b_blocknr,
  1126. entries2, g.offset);
  1127. error = -EIO;
  1128. goto out_kfree;
  1129. }
  1130. error = 0;
  1131. larr[leaf++] = bh;
  1132. } else {
  1133. brelse(bh);
  1134. }
  1135. } while(lfn);
  1136. BUG_ON(entries2 != entries);
  1137. error = do_filldir_main(ip, offset, opaque, filldir, darr,
  1138. entries, copied);
  1139. out_kfree:
  1140. for(i = 0; i < leaf; i++)
  1141. brelse(larr[i]);
  1142. vfree(larr);
  1143. out:
  1144. return error;
  1145. }
  1146. /**
  1147. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1148. * @dip: dinode pointer
  1149. * @offset: the hash of the last entry read shifted to the right once
  1150. * @opaque: buffer for the filldir function to fill
  1151. * @filldir: points to the filldir function to use
  1152. *
  1153. * Returns: errno
  1154. */
  1155. static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
  1156. filldir_t filldir)
  1157. {
  1158. struct gfs2_inode *dip = GFS2_I(inode);
  1159. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1160. u32 hsize, len = 0;
  1161. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1162. u32 hash, index;
  1163. __be64 *lp;
  1164. int copied = 0;
  1165. int error = 0;
  1166. unsigned depth = 0;
  1167. hsize = 1 << dip->i_di.di_depth;
  1168. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  1169. gfs2_consist_inode(dip);
  1170. return -EIO;
  1171. }
  1172. hash = gfs2_dir_offset2hash(*offset);
  1173. index = hash >> (32 - dip->i_di.di_depth);
  1174. lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
  1175. if (!lp)
  1176. return -ENOMEM;
  1177. while (index < hsize) {
  1178. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1179. ht_offset = index - lp_offset;
  1180. if (ht_offset_cur != ht_offset) {
  1181. error = gfs2_dir_read_data(dip, (char *)lp,
  1182. ht_offset * sizeof(__be64),
  1183. sdp->sd_hash_bsize, 1);
  1184. if (error != sdp->sd_hash_bsize) {
  1185. if (error >= 0)
  1186. error = -EIO;
  1187. goto out;
  1188. }
  1189. ht_offset_cur = ht_offset;
  1190. }
  1191. error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
  1192. &copied, &depth,
  1193. be64_to_cpu(lp[lp_offset]));
  1194. if (error)
  1195. break;
  1196. len = 1 << (dip->i_di.di_depth - depth);
  1197. index = (index & ~(len - 1)) + len;
  1198. }
  1199. out:
  1200. kfree(lp);
  1201. if (error > 0)
  1202. error = 0;
  1203. return error;
  1204. }
  1205. int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
  1206. filldir_t filldir)
  1207. {
  1208. struct gfs2_inode *dip = GFS2_I(inode);
  1209. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1210. struct dirent_gather g;
  1211. const struct gfs2_dirent **darr, *dent;
  1212. struct buffer_head *dibh;
  1213. int copied = 0;
  1214. int error;
  1215. if (!dip->i_di.di_entries)
  1216. return 0;
  1217. if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
  1218. return dir_e_read(inode, offset, opaque, filldir);
  1219. if (!gfs2_is_stuffed(dip)) {
  1220. gfs2_consist_inode(dip);
  1221. return -EIO;
  1222. }
  1223. error = gfs2_meta_inode_buffer(dip, &dibh);
  1224. if (error)
  1225. return error;
  1226. error = -ENOMEM;
  1227. /* 96 is max number of dirents which can be stuffed into an inode */
  1228. darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_KERNEL);
  1229. if (darr) {
  1230. g.pdent = darr;
  1231. g.offset = 0;
  1232. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1233. gfs2_dirent_gather, NULL, &g);
  1234. if (IS_ERR(dent)) {
  1235. error = PTR_ERR(dent);
  1236. goto out;
  1237. }
  1238. if (dip->i_di.di_entries != g.offset) {
  1239. fs_warn(sdp, "Number of entries corrupt in dir %llu, "
  1240. "ip->i_di.di_entries (%u) != g.offset (%u)\n",
  1241. (unsigned long long)dip->i_no_addr,
  1242. dip->i_di.di_entries,
  1243. g.offset);
  1244. error = -EIO;
  1245. goto out;
  1246. }
  1247. error = do_filldir_main(dip, offset, opaque, filldir, darr,
  1248. dip->i_di.di_entries, &copied);
  1249. out:
  1250. kfree(darr);
  1251. }
  1252. if (error > 0)
  1253. error = 0;
  1254. brelse(dibh);
  1255. return error;
  1256. }
  1257. /**
  1258. * gfs2_dir_search - Search a directory
  1259. * @dip: The GFS2 inode
  1260. * @filename:
  1261. * @inode:
  1262. *
  1263. * This routine searches a directory for a file or another directory.
  1264. * Assumes a glock is held on dip.
  1265. *
  1266. * Returns: errno
  1267. */
  1268. struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name)
  1269. {
  1270. struct buffer_head *bh;
  1271. struct gfs2_dirent *dent;
  1272. struct inode *inode;
  1273. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1274. if (dent) {
  1275. if (IS_ERR(dent))
  1276. return ERR_PTR(PTR_ERR(dent));
  1277. inode = gfs2_inode_lookup(dir->i_sb,
  1278. be16_to_cpu(dent->de_type),
  1279. be64_to_cpu(dent->de_inum.no_addr),
  1280. be64_to_cpu(dent->de_inum.no_formal_ino), 0);
  1281. brelse(bh);
  1282. return inode;
  1283. }
  1284. return ERR_PTR(-ENOENT);
  1285. }
  1286. int gfs2_dir_check(struct inode *dir, const struct qstr *name,
  1287. const struct gfs2_inode *ip)
  1288. {
  1289. struct buffer_head *bh;
  1290. struct gfs2_dirent *dent;
  1291. int ret = -ENOENT;
  1292. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1293. if (dent) {
  1294. if (IS_ERR(dent))
  1295. return PTR_ERR(dent);
  1296. if (ip) {
  1297. if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
  1298. goto out;
  1299. if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
  1300. ip->i_no_formal_ino)
  1301. goto out;
  1302. if (unlikely(IF2DT(ip->i_inode.i_mode) !=
  1303. be16_to_cpu(dent->de_type))) {
  1304. gfs2_consist_inode(GFS2_I(dir));
  1305. ret = -EIO;
  1306. goto out;
  1307. }
  1308. }
  1309. ret = 0;
  1310. out:
  1311. brelse(bh);
  1312. }
  1313. return ret;
  1314. }
  1315. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1316. {
  1317. struct buffer_head *bh, *obh;
  1318. struct gfs2_inode *ip = GFS2_I(inode);
  1319. struct gfs2_leaf *leaf, *oleaf;
  1320. int error;
  1321. u32 index;
  1322. u64 bn;
  1323. index = name->hash >> (32 - ip->i_di.di_depth);
  1324. error = get_first_leaf(ip, index, &obh);
  1325. if (error)
  1326. return error;
  1327. do {
  1328. oleaf = (struct gfs2_leaf *)obh->b_data;
  1329. bn = be64_to_cpu(oleaf->lf_next);
  1330. if (!bn)
  1331. break;
  1332. brelse(obh);
  1333. error = get_leaf(ip, bn, &obh);
  1334. if (error)
  1335. return error;
  1336. } while(1);
  1337. gfs2_trans_add_bh(ip->i_gl, obh, 1);
  1338. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1339. if (!leaf) {
  1340. brelse(obh);
  1341. return -ENOSPC;
  1342. }
  1343. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1344. brelse(bh);
  1345. brelse(obh);
  1346. error = gfs2_meta_inode_buffer(ip, &bh);
  1347. if (error)
  1348. return error;
  1349. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1350. ip->i_di.di_blocks++;
  1351. gfs2_set_inode_blocks(&ip->i_inode);
  1352. gfs2_dinode_out(ip, bh->b_data);
  1353. brelse(bh);
  1354. return 0;
  1355. }
  1356. /**
  1357. * gfs2_dir_add - Add new filename into directory
  1358. * @dip: The GFS2 inode
  1359. * @filename: The new name
  1360. * @inode: The inode number of the entry
  1361. * @type: The type of the entry
  1362. *
  1363. * Returns: 0 on success, error code on failure
  1364. */
  1365. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1366. const struct gfs2_inode *nip, unsigned type)
  1367. {
  1368. struct gfs2_inode *ip = GFS2_I(inode);
  1369. struct buffer_head *bh;
  1370. struct gfs2_dirent *dent;
  1371. struct gfs2_leaf *leaf;
  1372. int error;
  1373. while(1) {
  1374. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
  1375. &bh);
  1376. if (dent) {
  1377. if (IS_ERR(dent))
  1378. return PTR_ERR(dent);
  1379. dent = gfs2_init_dirent(inode, dent, name, bh);
  1380. gfs2_inum_out(nip, dent);
  1381. dent->de_type = cpu_to_be16(type);
  1382. if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1383. leaf = (struct gfs2_leaf *)bh->b_data;
  1384. leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
  1385. }
  1386. brelse(bh);
  1387. error = gfs2_meta_inode_buffer(ip, &bh);
  1388. if (error)
  1389. break;
  1390. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1391. ip->i_di.di_entries++;
  1392. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1393. gfs2_dinode_out(ip, bh->b_data);
  1394. brelse(bh);
  1395. error = 0;
  1396. break;
  1397. }
  1398. if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
  1399. error = dir_make_exhash(inode);
  1400. if (error)
  1401. break;
  1402. continue;
  1403. }
  1404. error = dir_split_leaf(inode, name);
  1405. if (error == 0)
  1406. continue;
  1407. if (error < 0)
  1408. break;
  1409. if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
  1410. error = dir_double_exhash(ip);
  1411. if (error)
  1412. break;
  1413. error = dir_split_leaf(inode, name);
  1414. if (error < 0)
  1415. break;
  1416. if (error == 0)
  1417. continue;
  1418. }
  1419. error = dir_new_leaf(inode, name);
  1420. if (!error)
  1421. continue;
  1422. error = -ENOSPC;
  1423. break;
  1424. }
  1425. return error;
  1426. }
  1427. /**
  1428. * gfs2_dir_del - Delete a directory entry
  1429. * @dip: The GFS2 inode
  1430. * @filename: The filename
  1431. *
  1432. * Returns: 0 on success, error code on failure
  1433. */
  1434. int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
  1435. {
  1436. struct gfs2_dirent *dent, *prev = NULL;
  1437. struct buffer_head *bh;
  1438. int error;
  1439. /* Returns _either_ the entry (if its first in block) or the
  1440. previous entry otherwise */
  1441. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1442. if (!dent) {
  1443. gfs2_consist_inode(dip);
  1444. return -EIO;
  1445. }
  1446. if (IS_ERR(dent)) {
  1447. gfs2_consist_inode(dip);
  1448. return PTR_ERR(dent);
  1449. }
  1450. /* If not first in block, adjust pointers accordingly */
  1451. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1452. prev = dent;
  1453. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1454. }
  1455. dirent_del(dip, bh, prev, dent);
  1456. if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1457. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1458. u16 entries = be16_to_cpu(leaf->lf_entries);
  1459. if (!entries)
  1460. gfs2_consist_inode(dip);
  1461. leaf->lf_entries = cpu_to_be16(--entries);
  1462. }
  1463. brelse(bh);
  1464. error = gfs2_meta_inode_buffer(dip, &bh);
  1465. if (error)
  1466. return error;
  1467. if (!dip->i_di.di_entries)
  1468. gfs2_consist_inode(dip);
  1469. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1470. dip->i_di.di_entries--;
  1471. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1472. gfs2_dinode_out(dip, bh->b_data);
  1473. brelse(bh);
  1474. mark_inode_dirty(&dip->i_inode);
  1475. return error;
  1476. }
  1477. /**
  1478. * gfs2_dir_mvino - Change inode number of directory entry
  1479. * @dip: The GFS2 inode
  1480. * @filename:
  1481. * @new_inode:
  1482. *
  1483. * This routine changes the inode number of a directory entry. It's used
  1484. * by rename to change ".." when a directory is moved.
  1485. * Assumes a glock is held on dvp.
  1486. *
  1487. * Returns: errno
  1488. */
  1489. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1490. const struct gfs2_inode *nip, unsigned int new_type)
  1491. {
  1492. struct buffer_head *bh;
  1493. struct gfs2_dirent *dent;
  1494. int error;
  1495. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1496. if (!dent) {
  1497. gfs2_consist_inode(dip);
  1498. return -EIO;
  1499. }
  1500. if (IS_ERR(dent))
  1501. return PTR_ERR(dent);
  1502. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1503. gfs2_inum_out(nip, dent);
  1504. dent->de_type = cpu_to_be16(new_type);
  1505. if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1506. brelse(bh);
  1507. error = gfs2_meta_inode_buffer(dip, &bh);
  1508. if (error)
  1509. return error;
  1510. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1511. }
  1512. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1513. gfs2_dinode_out(dip, bh->b_data);
  1514. brelse(bh);
  1515. return 0;
  1516. }
  1517. /**
  1518. * foreach_leaf - call a function for each leaf in a directory
  1519. * @dip: the directory
  1520. * @lc: the function to call for each each
  1521. * @data: private data to pass to it
  1522. *
  1523. * Returns: errno
  1524. */
  1525. static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
  1526. {
  1527. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1528. struct buffer_head *bh;
  1529. struct gfs2_leaf *leaf;
  1530. u32 hsize, len;
  1531. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1532. u32 index = 0;
  1533. __be64 *lp;
  1534. u64 leaf_no;
  1535. int error = 0;
  1536. hsize = 1 << dip->i_di.di_depth;
  1537. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  1538. gfs2_consist_inode(dip);
  1539. return -EIO;
  1540. }
  1541. lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
  1542. if (!lp)
  1543. return -ENOMEM;
  1544. while (index < hsize) {
  1545. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1546. ht_offset = index - lp_offset;
  1547. if (ht_offset_cur != ht_offset) {
  1548. error = gfs2_dir_read_data(dip, (char *)lp,
  1549. ht_offset * sizeof(__be64),
  1550. sdp->sd_hash_bsize, 1);
  1551. if (error != sdp->sd_hash_bsize) {
  1552. if (error >= 0)
  1553. error = -EIO;
  1554. goto out;
  1555. }
  1556. ht_offset_cur = ht_offset;
  1557. }
  1558. leaf_no = be64_to_cpu(lp[lp_offset]);
  1559. if (leaf_no) {
  1560. error = get_leaf(dip, leaf_no, &bh);
  1561. if (error)
  1562. goto out;
  1563. leaf = (struct gfs2_leaf *)bh->b_data;
  1564. len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
  1565. brelse(bh);
  1566. error = lc(dip, index, len, leaf_no, data);
  1567. if (error)
  1568. goto out;
  1569. index = (index & ~(len - 1)) + len;
  1570. } else
  1571. index++;
  1572. }
  1573. if (index != hsize) {
  1574. gfs2_consist_inode(dip);
  1575. error = -EIO;
  1576. }
  1577. out:
  1578. kfree(lp);
  1579. return error;
  1580. }
  1581. /**
  1582. * leaf_dealloc - Deallocate a directory leaf
  1583. * @dip: the directory
  1584. * @index: the hash table offset in the directory
  1585. * @len: the number of pointers to this leaf
  1586. * @leaf_no: the leaf number
  1587. * @data: not used
  1588. *
  1589. * Returns: errno
  1590. */
  1591. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1592. u64 leaf_no, void *data)
  1593. {
  1594. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1595. struct gfs2_leaf *tmp_leaf;
  1596. struct gfs2_rgrp_list rlist;
  1597. struct buffer_head *bh, *dibh;
  1598. u64 blk, nblk;
  1599. unsigned int rg_blocks = 0, l_blocks = 0;
  1600. char *ht;
  1601. unsigned int x, size = len * sizeof(u64);
  1602. int error;
  1603. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1604. ht = kzalloc(size, GFP_KERNEL);
  1605. if (!ht)
  1606. return -ENOMEM;
  1607. gfs2_alloc_get(dip);
  1608. error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1609. if (error)
  1610. goto out;
  1611. error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
  1612. if (error)
  1613. goto out_qs;
  1614. /* Count the number of leaves */
  1615. for (blk = leaf_no; blk; blk = nblk) {
  1616. error = get_leaf(dip, blk, &bh);
  1617. if (error)
  1618. goto out_rlist;
  1619. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1620. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1621. brelse(bh);
  1622. gfs2_rlist_add(sdp, &rlist, blk);
  1623. l_blocks++;
  1624. }
  1625. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  1626. for (x = 0; x < rlist.rl_rgrps; x++) {
  1627. struct gfs2_rgrpd *rgd;
  1628. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1629. rg_blocks += rgd->rd_length;
  1630. }
  1631. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1632. if (error)
  1633. goto out_rlist;
  1634. error = gfs2_trans_begin(sdp,
  1635. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1636. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1637. if (error)
  1638. goto out_rg_gunlock;
  1639. for (blk = leaf_no; blk; blk = nblk) {
  1640. error = get_leaf(dip, blk, &bh);
  1641. if (error)
  1642. goto out_end_trans;
  1643. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1644. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1645. brelse(bh);
  1646. gfs2_free_meta(dip, blk, 1);
  1647. if (!dip->i_di.di_blocks)
  1648. gfs2_consist_inode(dip);
  1649. dip->i_di.di_blocks--;
  1650. gfs2_set_inode_blocks(&dip->i_inode);
  1651. }
  1652. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1653. if (error != size) {
  1654. if (error >= 0)
  1655. error = -EIO;
  1656. goto out_end_trans;
  1657. }
  1658. error = gfs2_meta_inode_buffer(dip, &dibh);
  1659. if (error)
  1660. goto out_end_trans;
  1661. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  1662. gfs2_dinode_out(dip, dibh->b_data);
  1663. brelse(dibh);
  1664. out_end_trans:
  1665. gfs2_trans_end(sdp);
  1666. out_rg_gunlock:
  1667. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1668. out_rlist:
  1669. gfs2_rlist_free(&rlist);
  1670. gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
  1671. out_qs:
  1672. gfs2_quota_unhold(dip);
  1673. out:
  1674. gfs2_alloc_put(dip);
  1675. kfree(ht);
  1676. return error;
  1677. }
  1678. /**
  1679. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1680. * @dip: the directory
  1681. *
  1682. * Dealloc all on-disk directory leaves to FREEMETA state
  1683. * Change on-disk inode type to "regular file"
  1684. *
  1685. * Returns: errno
  1686. */
  1687. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1688. {
  1689. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1690. struct buffer_head *bh;
  1691. int error;
  1692. /* Dealloc on-disk leaves to FREEMETA state */
  1693. error = foreach_leaf(dip, leaf_dealloc, NULL);
  1694. if (error)
  1695. return error;
  1696. /* Make this a regular file in case we crash.
  1697. (We don't want to free these blocks a second time.) */
  1698. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1699. if (error)
  1700. return error;
  1701. error = gfs2_meta_inode_buffer(dip, &bh);
  1702. if (!error) {
  1703. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1704. ((struct gfs2_dinode *)bh->b_data)->di_mode =
  1705. cpu_to_be32(S_IFREG);
  1706. brelse(bh);
  1707. }
  1708. gfs2_trans_end(sdp);
  1709. return error;
  1710. }
  1711. /**
  1712. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1713. * @ip: the file being written to
  1714. * @filname: the filename that's going to be added
  1715. *
  1716. * Returns: 1 if alloc required, 0 if not, -ve on error
  1717. */
  1718. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
  1719. {
  1720. struct gfs2_dirent *dent;
  1721. struct buffer_head *bh;
  1722. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1723. if (!dent) {
  1724. return 1;
  1725. }
  1726. if (IS_ERR(dent))
  1727. return PTR_ERR(dent);
  1728. brelse(bh);
  1729. return 0;
  1730. }