dir.c 46 KB

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