dir.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. /*
  10. * Implements Extendible Hashing as described in:
  11. * "Extendible Hashing" by Fagin, et al in
  12. * __ACM Trans. on Database Systems__, Sept 1979.
  13. *
  14. *
  15. * Here's the layout of dirents which is essentially the same as that of ext2
  16. * within a single block. The field de_name_len is the number of bytes
  17. * actually required for the name (no null terminator). The field de_rec_len
  18. * is the number of bytes allocated to the dirent. The offset of the next
  19. * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
  20. * deleted, the preceding dirent inherits its allocated space, ie
  21. * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
  22. * by adding de_rec_len to the current dirent, this essentially causes the
  23. * deleted dirent to get jumped over when iterating through all the dirents.
  24. *
  25. * When deleting the first dirent in a block, there is no previous dirent so
  26. * the field de_ino is set to zero to designate it as deleted. When allocating
  27. * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
  28. * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
  29. * dirent is allocated. Otherwise it must go through all the 'used' dirents
  30. * searching for one in which the amount of total space minus the amount of
  31. * used space will provide enough space for the new dirent.
  32. *
  33. * There are two types of blocks in which dirents reside. In a stuffed dinode,
  34. * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
  35. * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
  36. * beginning of the leaf block. The dirents reside in leaves when
  37. *
  38. * dip->i_diskflags & GFS2_DIF_EXHASH is true
  39. *
  40. * Otherwise, the dirents are "linear", within a single stuffed dinode block.
  41. *
  42. * When the dirents are in leaves, the actual contents of the directory file are
  43. * used as an array of 64-bit block pointers pointing to the leaf blocks. The
  44. * dirents are NOT in the directory file itself. There can be more than one
  45. * block pointer in the array that points to the same leaf. In fact, when a
  46. * directory is first converted from linear to exhash, all of the pointers
  47. * point to the same leaf.
  48. *
  49. * When a leaf is completely full, the size of the hash table can be
  50. * doubled unless it is already at the maximum size which is hard coded into
  51. * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
  52. * but never before the maximum hash table size has been reached.
  53. */
  54. #include <linux/slab.h>
  55. #include <linux/spinlock.h>
  56. #include <linux/buffer_head.h>
  57. #include <linux/sort.h>
  58. #include <linux/gfs2_ondisk.h>
  59. #include <linux/crc32.h>
  60. #include <linux/vmalloc.h>
  61. #include <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_disksize < offset + size)
  118. ip->i_disksize = 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. int new = 0;
  144. if (!size)
  145. return 0;
  146. if (gfs2_is_stuffed(ip) &&
  147. offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  148. return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
  149. size);
  150. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  151. return -EINVAL;
  152. if (gfs2_is_stuffed(ip)) {
  153. error = gfs2_unstuff_dinode(ip, NULL);
  154. if (error)
  155. return error;
  156. }
  157. lblock = offset;
  158. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  159. while (copied < size) {
  160. unsigned int amount;
  161. struct buffer_head *bh;
  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_disksize < offset + copied)
  196. ip->i_disksize = 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_disksize)
  239. return 0;
  240. if (offset + size > ip->i_disksize)
  241. size = ip->i_disksize - 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_diskflags & 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_disksize) {
  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. unsigned int n = 1;
  694. u64 bn = gfs2_alloc_block(ip, &n);
  695. struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
  696. struct gfs2_leaf *leaf;
  697. struct gfs2_dirent *dent;
  698. struct qstr name = { .name = "", .len = 0, .hash = 0 };
  699. if (!bh)
  700. return NULL;
  701. gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
  702. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  703. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  704. leaf = (struct gfs2_leaf *)bh->b_data;
  705. leaf->lf_depth = cpu_to_be16(depth);
  706. leaf->lf_entries = 0;
  707. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  708. leaf->lf_next = 0;
  709. memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
  710. dent = (struct gfs2_dirent *)(leaf+1);
  711. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  712. *pbh = bh;
  713. return leaf;
  714. }
  715. /**
  716. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  717. * @dip: The GFS2 inode
  718. *
  719. * Returns: 0 on success, error code otherwise
  720. */
  721. static int dir_make_exhash(struct inode *inode)
  722. {
  723. struct gfs2_inode *dip = GFS2_I(inode);
  724. struct gfs2_sbd *sdp = GFS2_SB(inode);
  725. struct gfs2_dirent *dent;
  726. struct qstr args;
  727. struct buffer_head *bh, *dibh;
  728. struct gfs2_leaf *leaf;
  729. int y;
  730. u32 x;
  731. __be64 *lp;
  732. u64 bn;
  733. int error;
  734. error = gfs2_meta_inode_buffer(dip, &dibh);
  735. if (error)
  736. return error;
  737. /* Turn over a new leaf */
  738. leaf = new_leaf(inode, &bh, 0);
  739. if (!leaf)
  740. return -ENOSPC;
  741. bn = bh->b_blocknr;
  742. gfs2_assert(sdp, dip->i_entries < (1 << 16));
  743. leaf->lf_entries = cpu_to_be16(dip->i_entries);
  744. /* Copy dirents */
  745. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  746. sizeof(struct gfs2_dinode));
  747. /* Find last entry */
  748. x = 0;
  749. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  750. sizeof(struct gfs2_leaf);
  751. args.name = bh->b_data;
  752. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  753. gfs2_dirent_last, &args, NULL);
  754. if (!dent) {
  755. brelse(bh);
  756. brelse(dibh);
  757. return -EIO;
  758. }
  759. if (IS_ERR(dent)) {
  760. brelse(bh);
  761. brelse(dibh);
  762. return PTR_ERR(dent);
  763. }
  764. /* Adjust the last dirent's record length
  765. (Remember that dent still points to the last entry.) */
  766. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  767. sizeof(struct gfs2_dinode) -
  768. sizeof(struct gfs2_leaf));
  769. brelse(bh);
  770. /* We're done with the new leaf block, now setup the new
  771. hash table. */
  772. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  773. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  774. lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  775. for (x = sdp->sd_hash_ptrs; x--; lp++)
  776. *lp = cpu_to_be64(bn);
  777. dip->i_disksize = sdp->sd_sb.sb_bsize / 2;
  778. gfs2_add_inode_blocks(&dip->i_inode, 1);
  779. dip->i_diskflags |= 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. be16_add_cpu(&nleaf->lf_entries, 1);
  873. dirent_del(dip, obh, prev, dent);
  874. if (!oleaf->lf_entries)
  875. gfs2_consist_inode(dip);
  876. be16_add_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. gfs2_add_inode_blocks(&dip->i_inode, 1);
  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_depth;
  920. if (hsize * sizeof(u64) != dip->i_disksize) {
  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_NOFS | __GFP_NOFAIL);
  926. for (block = dip->i_disksize >> 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_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_depth;
  1168. if (hsize * sizeof(u64) != dip->i_disksize) {
  1169. gfs2_consist_inode(dip);
  1170. return -EIO;
  1171. }
  1172. hash = gfs2_dir_offset2hash(*offset);
  1173. index = hash >> (32 - dip->i_depth);
  1174. lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
  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_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_entries)
  1216. return 0;
  1217. if (dip->i_diskflags & 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_NOFS);
  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_entries != g.offset) {
  1239. fs_warn(sdp, "Number of entries corrupt in dir %llu, "
  1240. "ip->i_entries (%u) != g.offset (%u)\n",
  1241. (unsigned long long)dip->i_no_addr,
  1242. dip->i_entries,
  1243. g.offset);
  1244. error = -EIO;
  1245. goto out;
  1246. }
  1247. error = do_filldir_main(dip, offset, opaque, filldir, darr,
  1248. dip->i_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_CAST(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_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. gfs2_add_inode_blocks(&ip->i_inode, 1);
  1351. gfs2_dinode_out(ip, bh->b_data);
  1352. brelse(bh);
  1353. return 0;
  1354. }
  1355. /**
  1356. * gfs2_dir_add - Add new filename into directory
  1357. * @dip: The GFS2 inode
  1358. * @filename: The new name
  1359. * @inode: The inode number of the entry
  1360. * @type: The type of the entry
  1361. *
  1362. * Returns: 0 on success, error code on failure
  1363. */
  1364. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1365. const struct gfs2_inode *nip, unsigned type)
  1366. {
  1367. struct gfs2_inode *ip = GFS2_I(inode);
  1368. struct buffer_head *bh;
  1369. struct gfs2_dirent *dent;
  1370. struct gfs2_leaf *leaf;
  1371. int error;
  1372. while(1) {
  1373. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
  1374. &bh);
  1375. if (dent) {
  1376. if (IS_ERR(dent))
  1377. return PTR_ERR(dent);
  1378. dent = gfs2_init_dirent(inode, dent, name, bh);
  1379. gfs2_inum_out(nip, dent);
  1380. dent->de_type = cpu_to_be16(type);
  1381. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  1382. leaf = (struct gfs2_leaf *)bh->b_data;
  1383. be16_add_cpu(&leaf->lf_entries, 1);
  1384. }
  1385. brelse(bh);
  1386. error = gfs2_meta_inode_buffer(ip, &bh);
  1387. if (error)
  1388. break;
  1389. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1390. ip->i_entries++;
  1391. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1392. gfs2_dinode_out(ip, bh->b_data);
  1393. brelse(bh);
  1394. error = 0;
  1395. break;
  1396. }
  1397. if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
  1398. error = dir_make_exhash(inode);
  1399. if (error)
  1400. break;
  1401. continue;
  1402. }
  1403. error = dir_split_leaf(inode, name);
  1404. if (error == 0)
  1405. continue;
  1406. if (error < 0)
  1407. break;
  1408. if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
  1409. error = dir_double_exhash(ip);
  1410. if (error)
  1411. break;
  1412. error = dir_split_leaf(inode, name);
  1413. if (error < 0)
  1414. break;
  1415. if (error == 0)
  1416. continue;
  1417. }
  1418. error = dir_new_leaf(inode, name);
  1419. if (!error)
  1420. continue;
  1421. error = -ENOSPC;
  1422. break;
  1423. }
  1424. return error;
  1425. }
  1426. /**
  1427. * gfs2_dir_del - Delete a directory entry
  1428. * @dip: The GFS2 inode
  1429. * @filename: The filename
  1430. *
  1431. * Returns: 0 on success, error code on failure
  1432. */
  1433. int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
  1434. {
  1435. struct gfs2_dirent *dent, *prev = NULL;
  1436. struct buffer_head *bh;
  1437. int error;
  1438. /* Returns _either_ the entry (if its first in block) or the
  1439. previous entry otherwise */
  1440. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1441. if (!dent) {
  1442. gfs2_consist_inode(dip);
  1443. return -EIO;
  1444. }
  1445. if (IS_ERR(dent)) {
  1446. gfs2_consist_inode(dip);
  1447. return PTR_ERR(dent);
  1448. }
  1449. /* If not first in block, adjust pointers accordingly */
  1450. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1451. prev = dent;
  1452. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1453. }
  1454. dirent_del(dip, bh, prev, dent);
  1455. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1456. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1457. u16 entries = be16_to_cpu(leaf->lf_entries);
  1458. if (!entries)
  1459. gfs2_consist_inode(dip);
  1460. leaf->lf_entries = cpu_to_be16(--entries);
  1461. }
  1462. brelse(bh);
  1463. error = gfs2_meta_inode_buffer(dip, &bh);
  1464. if (error)
  1465. return error;
  1466. if (!dip->i_entries)
  1467. gfs2_consist_inode(dip);
  1468. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1469. dip->i_entries--;
  1470. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1471. gfs2_dinode_out(dip, bh->b_data);
  1472. brelse(bh);
  1473. mark_inode_dirty(&dip->i_inode);
  1474. return error;
  1475. }
  1476. /**
  1477. * gfs2_dir_mvino - Change inode number of directory entry
  1478. * @dip: The GFS2 inode
  1479. * @filename:
  1480. * @new_inode:
  1481. *
  1482. * This routine changes the inode number of a directory entry. It's used
  1483. * by rename to change ".." when a directory is moved.
  1484. * Assumes a glock is held on dvp.
  1485. *
  1486. * Returns: errno
  1487. */
  1488. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1489. const struct gfs2_inode *nip, unsigned int new_type)
  1490. {
  1491. struct buffer_head *bh;
  1492. struct gfs2_dirent *dent;
  1493. int error;
  1494. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1495. if (!dent) {
  1496. gfs2_consist_inode(dip);
  1497. return -EIO;
  1498. }
  1499. if (IS_ERR(dent))
  1500. return PTR_ERR(dent);
  1501. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1502. gfs2_inum_out(nip, dent);
  1503. dent->de_type = cpu_to_be16(new_type);
  1504. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1505. brelse(bh);
  1506. error = gfs2_meta_inode_buffer(dip, &bh);
  1507. if (error)
  1508. return error;
  1509. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1510. }
  1511. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1512. gfs2_dinode_out(dip, bh->b_data);
  1513. brelse(bh);
  1514. return 0;
  1515. }
  1516. /**
  1517. * foreach_leaf - call a function for each leaf in a directory
  1518. * @dip: the directory
  1519. * @lc: the function to call for each each
  1520. * @data: private data to pass to it
  1521. *
  1522. * Returns: errno
  1523. */
  1524. static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
  1525. {
  1526. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1527. struct buffer_head *bh;
  1528. struct gfs2_leaf *leaf;
  1529. u32 hsize, len;
  1530. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1531. u32 index = 0;
  1532. __be64 *lp;
  1533. u64 leaf_no;
  1534. int error = 0;
  1535. hsize = 1 << dip->i_depth;
  1536. if (hsize * sizeof(u64) != dip->i_disksize) {
  1537. gfs2_consist_inode(dip);
  1538. return -EIO;
  1539. }
  1540. lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
  1541. if (!lp)
  1542. return -ENOMEM;
  1543. while (index < hsize) {
  1544. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1545. ht_offset = index - lp_offset;
  1546. if (ht_offset_cur != ht_offset) {
  1547. error = gfs2_dir_read_data(dip, (char *)lp,
  1548. ht_offset * sizeof(__be64),
  1549. sdp->sd_hash_bsize, 1);
  1550. if (error != sdp->sd_hash_bsize) {
  1551. if (error >= 0)
  1552. error = -EIO;
  1553. goto out;
  1554. }
  1555. ht_offset_cur = ht_offset;
  1556. }
  1557. leaf_no = be64_to_cpu(lp[lp_offset]);
  1558. if (leaf_no) {
  1559. error = get_leaf(dip, leaf_no, &bh);
  1560. if (error)
  1561. goto out;
  1562. leaf = (struct gfs2_leaf *)bh->b_data;
  1563. len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
  1564. brelse(bh);
  1565. error = lc(dip, index, len, leaf_no, data);
  1566. if (error)
  1567. goto out;
  1568. index = (index & ~(len - 1)) + len;
  1569. } else
  1570. index++;
  1571. }
  1572. if (index != hsize) {
  1573. gfs2_consist_inode(dip);
  1574. error = -EIO;
  1575. }
  1576. out:
  1577. kfree(lp);
  1578. return error;
  1579. }
  1580. /**
  1581. * leaf_dealloc - Deallocate a directory leaf
  1582. * @dip: the directory
  1583. * @index: the hash table offset in the directory
  1584. * @len: the number of pointers to this leaf
  1585. * @leaf_no: the leaf number
  1586. * @data: not used
  1587. *
  1588. * Returns: errno
  1589. */
  1590. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1591. u64 leaf_no, void *data)
  1592. {
  1593. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1594. struct gfs2_leaf *tmp_leaf;
  1595. struct gfs2_rgrp_list rlist;
  1596. struct buffer_head *bh, *dibh;
  1597. u64 blk, nblk;
  1598. unsigned int rg_blocks = 0, l_blocks = 0;
  1599. char *ht;
  1600. unsigned int x, size = len * sizeof(u64);
  1601. int error;
  1602. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1603. ht = kzalloc(size, GFP_NOFS);
  1604. if (!ht)
  1605. return -ENOMEM;
  1606. if (!gfs2_alloc_get(dip)) {
  1607. error = -ENOMEM;
  1608. goto out;
  1609. }
  1610. error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1611. if (error)
  1612. goto out_put;
  1613. error = gfs2_rindex_hold(sdp, &dip->i_alloc->al_ri_gh);
  1614. if (error)
  1615. goto out_qs;
  1616. /* Count the number of leaves */
  1617. for (blk = leaf_no; blk; blk = nblk) {
  1618. error = get_leaf(dip, blk, &bh);
  1619. if (error)
  1620. goto out_rlist;
  1621. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1622. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1623. brelse(bh);
  1624. gfs2_rlist_add(sdp, &rlist, blk);
  1625. l_blocks++;
  1626. }
  1627. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1628. for (x = 0; x < rlist.rl_rgrps; x++) {
  1629. struct gfs2_rgrpd *rgd;
  1630. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1631. rg_blocks += rgd->rd_length;
  1632. }
  1633. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1634. if (error)
  1635. goto out_rlist;
  1636. error = gfs2_trans_begin(sdp,
  1637. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1638. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1639. if (error)
  1640. goto out_rg_gunlock;
  1641. for (blk = leaf_no; blk; blk = nblk) {
  1642. error = get_leaf(dip, blk, &bh);
  1643. if (error)
  1644. goto out_end_trans;
  1645. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1646. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1647. brelse(bh);
  1648. gfs2_free_meta(dip, blk, 1);
  1649. gfs2_add_inode_blocks(&dip->i_inode, -1);
  1650. }
  1651. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1652. if (error != size) {
  1653. if (error >= 0)
  1654. error = -EIO;
  1655. goto out_end_trans;
  1656. }
  1657. error = gfs2_meta_inode_buffer(dip, &dibh);
  1658. if (error)
  1659. goto out_end_trans;
  1660. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  1661. gfs2_dinode_out(dip, dibh->b_data);
  1662. brelse(dibh);
  1663. out_end_trans:
  1664. gfs2_trans_end(sdp);
  1665. out_rg_gunlock:
  1666. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1667. out_rlist:
  1668. gfs2_rlist_free(&rlist);
  1669. gfs2_glock_dq_uninit(&dip->i_alloc->al_ri_gh);
  1670. out_qs:
  1671. gfs2_quota_unhold(dip);
  1672. out_put:
  1673. gfs2_alloc_put(dip);
  1674. out:
  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. }