dir.c 46 KB

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