dir.c 46 KB

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