dir.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License v.2.
  8. */
  9. /*
  10. * Implements Extendible Hashing as described in:
  11. * "Extendible Hashing" by Fagin, et al in
  12. * __ACM Trans. on Database Systems__, Sept 1979.
  13. *
  14. *
  15. * Here's the layout of dirents which is essentially the same as that of ext2
  16. * within a single block. The field de_name_len is the number of bytes
  17. * actually required for the name (no null terminator). The field de_rec_len
  18. * is the number of bytes allocated to the dirent. The offset of the next
  19. * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
  20. * deleted, the preceding dirent inherits its allocated space, ie
  21. * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
  22. * by adding de_rec_len to the current dirent, this essentially causes the
  23. * deleted dirent to get jumped over when iterating through all the dirents.
  24. *
  25. * When deleting the first dirent in a block, there is no previous dirent so
  26. * the field de_ino is set to zero to designate it as deleted. When allocating
  27. * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
  28. * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
  29. * dirent is allocated. Otherwise it must go through all the 'used' dirents
  30. * searching for one in which the amount of total space minus the amount of
  31. * used space will provide enough space for the new dirent.
  32. *
  33. * There are two types of blocks in which dirents reside. In a stuffed dinode,
  34. * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
  35. * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
  36. * beginning of the leaf block. The dirents reside in leaves when
  37. *
  38. * dip->i_di.di_flags & GFS2_DIF_EXHASH is true
  39. *
  40. * Otherwise, the dirents are "linear", within a single stuffed dinode block.
  41. *
  42. * When the dirents are in leaves, the actual contents of the directory file are
  43. * used as an array of 64-bit block pointers pointing to the leaf blocks. The
  44. * dirents are NOT in the directory file itself. There can be more than one
  45. * block pointer in the array that points to the same leaf. In fact, when a
  46. * directory is first converted from linear to exhash, all of the pointers
  47. * point to the same leaf.
  48. *
  49. * When a leaf is completely full, the size of the hash table can be
  50. * doubled unless it is already at the maximum size which is hard coded into
  51. * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
  52. * but never before the maximum hash table size has been reached.
  53. */
  54. #include <linux/sched.h>
  55. #include <linux/slab.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/buffer_head.h>
  58. #include <linux/sort.h>
  59. #include <linux/gfs2_ondisk.h>
  60. #include <linux/crc32.h>
  61. #include <linux/vmalloc.h>
  62. #include "gfs2.h"
  63. #include "lm_interface.h"
  64. #include "incore.h"
  65. #include "dir.h"
  66. #include "glock.h"
  67. #include "inode.h"
  68. #include "meta_io.h"
  69. #include "quota.h"
  70. #include "rgrp.h"
  71. #include "trans.h"
  72. #include "bmap.h"
  73. #include "util.h"
  74. #define IS_LEAF 1 /* Hashed (leaf) directory */
  75. #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
  76. #if 1
  77. #define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
  78. #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
  79. #else
  80. #define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
  81. #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
  82. #endif
  83. typedef int (*leaf_call_t) (struct gfs2_inode *dip,
  84. uint32_t index, uint32_t len, uint64_t leaf_no,
  85. void *data);
  86. int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, uint64_t block,
  87. struct buffer_head **bhp)
  88. {
  89. struct buffer_head *bh;
  90. bh = gfs2_meta_new(ip->i_gl, block);
  91. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  92. gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
  93. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  94. *bhp = bh;
  95. return 0;
  96. }
  97. static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, uint64_t block,
  98. struct buffer_head **bhp)
  99. {
  100. struct buffer_head *bh;
  101. int error;
  102. error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT, &bh);
  103. if (error)
  104. return error;
  105. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
  106. brelse(bh);
  107. return -EIO;
  108. }
  109. *bhp = bh;
  110. return 0;
  111. }
  112. static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
  113. unsigned int offset, unsigned int size)
  114. {
  115. struct buffer_head *dibh;
  116. int error;
  117. error = gfs2_meta_inode_buffer(ip, &dibh);
  118. if (error)
  119. return error;
  120. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  121. memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
  122. if (ip->i_di.di_size < offset + size)
  123. ip->i_di.di_size = offset + size;
  124. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  125. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  126. brelse(dibh);
  127. return size;
  128. }
  129. /**
  130. * gfs2_dir_write_data - Write directory information to the inode
  131. * @ip: The GFS2 inode
  132. * @buf: The buffer containing information to be written
  133. * @offset: The file offset to start writing at
  134. * @size: The amount of data to write
  135. *
  136. * Returns: The number of bytes correctly written or error code
  137. */
  138. static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
  139. uint64_t offset, unsigned int size)
  140. {
  141. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  142. struct buffer_head *dibh;
  143. uint64_t lblock, dblock;
  144. uint32_t extlen = 0;
  145. unsigned int o;
  146. int copied = 0;
  147. int error = 0;
  148. if (!size)
  149. return 0;
  150. if (gfs2_is_stuffed(ip) &&
  151. offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  152. return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
  153. size);
  154. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  155. return -EINVAL;
  156. if (gfs2_is_stuffed(ip)) {
  157. error = gfs2_unstuff_dinode(ip, NULL, NULL);
  158. if (error)
  159. return error;
  160. }
  161. lblock = offset;
  162. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  163. while (copied < size) {
  164. unsigned int amount;
  165. struct buffer_head *bh;
  166. int new;
  167. amount = size - copied;
  168. if (amount > sdp->sd_sb.sb_bsize - o)
  169. amount = sdp->sd_sb.sb_bsize - o;
  170. if (!extlen) {
  171. new = 1;
  172. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  173. &dblock, &extlen);
  174. if (error)
  175. goto fail;
  176. error = -EIO;
  177. if (gfs2_assert_withdraw(sdp, dblock))
  178. goto fail;
  179. }
  180. if (amount == sdp->sd_jbsize || new)
  181. error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
  182. else
  183. error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
  184. if (error)
  185. goto fail;
  186. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  187. memcpy(bh->b_data + o, buf, amount);
  188. brelse(bh);
  189. if (error)
  190. goto fail;
  191. copied += amount;
  192. lblock++;
  193. dblock++;
  194. extlen--;
  195. o = sizeof(struct gfs2_meta_header);
  196. }
  197. out:
  198. error = gfs2_meta_inode_buffer(ip, &dibh);
  199. if (error)
  200. return error;
  201. if (ip->i_di.di_size < offset + copied)
  202. ip->i_di.di_size = offset + copied;
  203. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  204. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  205. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  206. brelse(dibh);
  207. return copied;
  208. fail:
  209. if (copied)
  210. goto out;
  211. return error;
  212. }
  213. static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
  214. unsigned int offset, unsigned int size)
  215. {
  216. struct buffer_head *dibh;
  217. int error;
  218. error = gfs2_meta_inode_buffer(ip, &dibh);
  219. if (!error) {
  220. offset += sizeof(struct gfs2_dinode);
  221. memcpy(buf, dibh->b_data + offset, size);
  222. brelse(dibh);
  223. }
  224. return (error) ? error : size;
  225. }
  226. /**
  227. * gfs2_dir_read_data - Read a data from a directory inode
  228. * @ip: The GFS2 Inode
  229. * @buf: The buffer to place result into
  230. * @offset: File offset to begin jdata_readng from
  231. * @size: Amount of data to transfer
  232. *
  233. * Returns: The amount of data actually copied or the error
  234. */
  235. static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
  236. uint64_t offset, unsigned int size)
  237. {
  238. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  239. uint64_t lblock, dblock;
  240. uint32_t extlen = 0;
  241. unsigned int o;
  242. int copied = 0;
  243. int error = 0;
  244. if (offset >= ip->i_di.di_size)
  245. return 0;
  246. if ((offset + size) > ip->i_di.di_size)
  247. size = ip->i_di.di_size - offset;
  248. if (!size)
  249. return 0;
  250. if (gfs2_is_stuffed(ip))
  251. return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
  252. size);
  253. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  254. return -EINVAL;
  255. lblock = offset;
  256. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  257. while (copied < size) {
  258. unsigned int amount;
  259. struct buffer_head *bh;
  260. int new;
  261. amount = size - copied;
  262. if (amount > sdp->sd_sb.sb_bsize - o)
  263. amount = sdp->sd_sb.sb_bsize - o;
  264. if (!extlen) {
  265. new = 0;
  266. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  267. &dblock, &extlen);
  268. if (error)
  269. goto fail;
  270. }
  271. if (extlen > 1)
  272. gfs2_meta_ra(ip->i_gl, dblock, extlen);
  273. if (dblock) {
  274. if (new)
  275. error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
  276. else
  277. error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
  278. if (error)
  279. goto fail;
  280. dblock++;
  281. extlen--;
  282. } else
  283. bh = NULL;
  284. memcpy(buf, bh->b_data + o, amount);
  285. brelse(bh);
  286. if (error)
  287. goto fail;
  288. copied += amount;
  289. lblock++;
  290. o = sizeof(struct gfs2_meta_header);
  291. }
  292. return copied;
  293. fail:
  294. return (copied) ? copied : error;
  295. }
  296. typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
  297. const struct qstr *name,
  298. void *opaque);
  299. static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
  300. const struct qstr *name, int ret)
  301. {
  302. if (dent->de_inum.no_addr != 0 &&
  303. be32_to_cpu(dent->de_hash) == name->hash &&
  304. be16_to_cpu(dent->de_name_len) == name->len &&
  305. memcmp((char *)(dent+1), name->name, name->len) == 0)
  306. return ret;
  307. return 0;
  308. }
  309. static int gfs2_dirent_find(const struct gfs2_dirent *dent,
  310. const struct qstr *name,
  311. void *opaque)
  312. {
  313. return __gfs2_dirent_find(dent, name, 1);
  314. }
  315. static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
  316. const struct qstr *name,
  317. void *opaque)
  318. {
  319. return __gfs2_dirent_find(dent, name, 2);
  320. }
  321. /*
  322. * name->name holds ptr to start of block.
  323. * name->len holds size of block.
  324. */
  325. static int gfs2_dirent_last(const struct gfs2_dirent *dent,
  326. const struct qstr *name,
  327. void *opaque)
  328. {
  329. const char *start = name->name;
  330. const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
  331. if (name->len == (end - start))
  332. return 1;
  333. return 0;
  334. }
  335. static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
  336. const struct qstr *name,
  337. void *opaque)
  338. {
  339. unsigned required = GFS2_DIRENT_SIZE(name->len);
  340. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  341. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  342. if (!dent->de_inum.no_addr)
  343. actual = GFS2_DIRENT_SIZE(0);
  344. if ((totlen - actual) >= required)
  345. return 1;
  346. return 0;
  347. }
  348. struct dirent_gather {
  349. const struct gfs2_dirent **pdent;
  350. unsigned offset;
  351. };
  352. static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
  353. const struct qstr *name,
  354. void *opaque)
  355. {
  356. struct dirent_gather *g = opaque;
  357. if (dent->de_inum.no_addr) {
  358. g->pdent[g->offset++] = dent;
  359. }
  360. return 0;
  361. }
  362. /*
  363. * Other possible things to check:
  364. * - Inode located within filesystem size (and on valid block)
  365. * - Valid directory entry type
  366. * Not sure how heavy-weight we want to make this... could also check
  367. * hash is correct for example, but that would take a lot of extra time.
  368. * For now the most important thing is to check that the various sizes
  369. * are correct.
  370. */
  371. static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
  372. unsigned int size, unsigned int len, int first)
  373. {
  374. const char *msg = "gfs2_dirent too small";
  375. if (unlikely(size < sizeof(struct gfs2_dirent)))
  376. goto error;
  377. msg = "gfs2_dirent misaligned";
  378. if (unlikely(offset & 0x7))
  379. goto error;
  380. msg = "gfs2_dirent points beyond end of block";
  381. if (unlikely(offset + size > len))
  382. goto error;
  383. msg = "zero inode number";
  384. if (unlikely(!first && !dent->de_inum.no_addr))
  385. goto error;
  386. msg = "name length is greater than space in dirent";
  387. if (dent->de_inum.no_addr &&
  388. unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
  389. size))
  390. goto error;
  391. return 0;
  392. error:
  393. printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
  394. first ? "first in block" : "not first in block");
  395. return -EIO;
  396. }
  397. static int gfs2_dirent_offset(const void *buf)
  398. {
  399. const struct gfs2_meta_header *h = buf;
  400. int offset;
  401. BUG_ON(buf == NULL);
  402. switch(be32_to_cpu(h->mh_type)) {
  403. case GFS2_METATYPE_LF:
  404. offset = sizeof(struct gfs2_leaf);
  405. break;
  406. case GFS2_METATYPE_DI:
  407. offset = sizeof(struct gfs2_dinode);
  408. break;
  409. default:
  410. goto wrong_type;
  411. }
  412. return offset;
  413. wrong_type:
  414. printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
  415. be32_to_cpu(h->mh_type));
  416. return -1;
  417. }
  418. static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
  419. void *buf,
  420. unsigned int len, gfs2_dscan_t scan,
  421. const struct qstr *name,
  422. void *opaque)
  423. {
  424. struct gfs2_dirent *dent, *prev;
  425. unsigned offset;
  426. unsigned size;
  427. int ret = 0;
  428. ret = gfs2_dirent_offset(buf);
  429. if (ret < 0)
  430. goto consist_inode;
  431. offset = ret;
  432. prev = NULL;
  433. dent = (struct gfs2_dirent *)(buf + offset);
  434. size = be16_to_cpu(dent->de_rec_len);
  435. if (gfs2_check_dirent(dent, offset, size, len, 1))
  436. goto consist_inode;
  437. do {
  438. ret = scan(dent, name, opaque);
  439. if (ret)
  440. break;
  441. offset += size;
  442. if (offset == len)
  443. break;
  444. prev = dent;
  445. dent = (struct gfs2_dirent *)(buf + offset);
  446. size = be16_to_cpu(dent->de_rec_len);
  447. if (gfs2_check_dirent(dent, offset, size, len, 0))
  448. goto consist_inode;
  449. } while(1);
  450. switch(ret) {
  451. case 0:
  452. return NULL;
  453. case 1:
  454. return dent;
  455. case 2:
  456. return prev ? prev : dent;
  457. default:
  458. BUG_ON(ret > 0);
  459. return ERR_PTR(ret);
  460. }
  461. consist_inode:
  462. gfs2_consist_inode(GFS2_I(inode));
  463. return ERR_PTR(-EIO);
  464. }
  465. /**
  466. * dirent_first - Return the first dirent
  467. * @dip: the directory
  468. * @bh: The buffer
  469. * @dent: Pointer to list of dirents
  470. *
  471. * return first dirent whether bh points to leaf or stuffed dinode
  472. *
  473. * Returns: IS_LEAF, IS_DINODE, or -errno
  474. */
  475. static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
  476. struct gfs2_dirent **dent)
  477. {
  478. struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
  479. if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
  480. if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
  481. return -EIO;
  482. *dent = (struct gfs2_dirent *)(bh->b_data +
  483. sizeof(struct gfs2_leaf));
  484. return IS_LEAF;
  485. } else {
  486. if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
  487. return -EIO;
  488. *dent = (struct gfs2_dirent *)(bh->b_data +
  489. sizeof(struct gfs2_dinode));
  490. return IS_DINODE;
  491. }
  492. }
  493. /**
  494. * dirent_next - Next dirent
  495. * @dip: the directory
  496. * @bh: The buffer
  497. * @dent: Pointer to list of dirents
  498. *
  499. * Returns: 0 on success, error code otherwise
  500. */
  501. static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
  502. struct gfs2_dirent **dent)
  503. {
  504. struct gfs2_dirent *tmp, *cur;
  505. char *bh_end;
  506. uint16_t cur_rec_len;
  507. cur = *dent;
  508. bh_end = bh->b_data + bh->b_size;
  509. cur_rec_len = be16_to_cpu(cur->de_rec_len);
  510. if ((char *)cur + cur_rec_len >= bh_end) {
  511. if ((char *)cur + cur_rec_len > bh_end) {
  512. gfs2_consist_inode(dip);
  513. return -EIO;
  514. }
  515. return -ENOENT;
  516. }
  517. tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
  518. if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
  519. gfs2_consist_inode(dip);
  520. return -EIO;
  521. }
  522. if (cur_rec_len == 0) {
  523. gfs2_consist_inode(dip);
  524. return -EIO;
  525. }
  526. /* Only the first dent could ever have de_inum.no_addr == 0 */
  527. if (!tmp->de_inum.no_addr) {
  528. gfs2_consist_inode(dip);
  529. return -EIO;
  530. }
  531. *dent = tmp;
  532. return 0;
  533. }
  534. /**
  535. * dirent_del - Delete a dirent
  536. * @dip: The GFS2 inode
  537. * @bh: The buffer
  538. * @prev: The previous dirent
  539. * @cur: The current dirent
  540. *
  541. */
  542. static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
  543. struct gfs2_dirent *prev, struct gfs2_dirent *cur)
  544. {
  545. uint16_t cur_rec_len, prev_rec_len;
  546. if (!cur->de_inum.no_addr) {
  547. gfs2_consist_inode(dip);
  548. return;
  549. }
  550. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  551. /* If there is no prev entry, this is the first entry in the block.
  552. The de_rec_len is already as big as it needs to be. Just zero
  553. out the inode number and return. */
  554. if (!prev) {
  555. cur->de_inum.no_addr = 0; /* No endianess worries */
  556. return;
  557. }
  558. /* Combine this dentry with the previous one. */
  559. prev_rec_len = be16_to_cpu(prev->de_rec_len);
  560. cur_rec_len = be16_to_cpu(cur->de_rec_len);
  561. if ((char *)prev + prev_rec_len != (char *)cur)
  562. gfs2_consist_inode(dip);
  563. if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
  564. gfs2_consist_inode(dip);
  565. prev_rec_len += cur_rec_len;
  566. prev->de_rec_len = cpu_to_be16(prev_rec_len);
  567. }
  568. /*
  569. * Takes a dent from which to grab space as an argument. Returns the
  570. * newly created dent.
  571. */
  572. static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
  573. struct gfs2_dirent *dent,
  574. const struct qstr *name,
  575. struct buffer_head *bh)
  576. {
  577. struct gfs2_inode *ip = GFS2_I(inode);
  578. struct gfs2_dirent *ndent;
  579. unsigned offset = 0, totlen;
  580. if (dent->de_inum.no_addr)
  581. offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  582. totlen = be16_to_cpu(dent->de_rec_len);
  583. BUG_ON(offset + name->len > totlen);
  584. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  585. ndent = (struct gfs2_dirent *)((char *)dent + offset);
  586. dent->de_rec_len = cpu_to_be16(offset);
  587. gfs2_qstr2dirent(name, totlen - offset, ndent);
  588. return ndent;
  589. }
  590. static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
  591. struct buffer_head *bh,
  592. const struct qstr *name)
  593. {
  594. struct gfs2_dirent *dent;
  595. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  596. gfs2_dirent_find_space, name, NULL);
  597. if (!dent || IS_ERR(dent))
  598. return dent;
  599. return gfs2_init_dirent(inode, dent, name, bh);
  600. }
  601. static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
  602. struct buffer_head **bhp)
  603. {
  604. int error;
  605. error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
  606. if (!error && gfs2_metatype_check(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, uint32_t index,
  621. uint64_t *leaf_out)
  622. {
  623. uint64_t leaf_no;
  624. int error;
  625. error = gfs2_dir_read_data(dip, (char *)&leaf_no,
  626. index * sizeof(uint64_t),
  627. sizeof(uint64_t));
  628. if (error != sizeof(uint64_t))
  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, uint32_t index,
  634. struct buffer_head **bh_out)
  635. {
  636. uint64_t 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 = cpu_to_be16(0);
  706. leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
  707. leaf->lf_next = cpu_to_be64(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. uint32_t x;
  730. uint64_t *lp, bn;
  731. int error;
  732. error = gfs2_meta_inode_buffer(dip, &dibh);
  733. if (error)
  734. return error;
  735. /* Turn over a new leaf */
  736. leaf = new_leaf(inode, &bh, 0);
  737. if (!leaf)
  738. return -ENOSPC;
  739. bn = bh->b_blocknr;
  740. gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
  741. leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
  742. /* Copy dirents */
  743. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  744. sizeof(struct gfs2_dinode));
  745. /* Find last entry */
  746. x = 0;
  747. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  748. sizeof(struct gfs2_leaf);
  749. args.name = bh->b_data;
  750. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  751. gfs2_dirent_last, &args, NULL);
  752. if (!dent) {
  753. brelse(bh);
  754. brelse(dibh);
  755. return -EIO;
  756. }
  757. if (IS_ERR(dent)) {
  758. brelse(bh);
  759. brelse(dibh);
  760. return PTR_ERR(dent);
  761. }
  762. /* Adjust the last dirent's record length
  763. (Remember that dent still points to the last entry.) */
  764. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  765. sizeof(struct gfs2_dinode) -
  766. sizeof(struct gfs2_leaf));
  767. brelse(bh);
  768. /* We're done with the new leaf block, now setup the new
  769. hash table. */
  770. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  771. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  772. lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
  773. for (x = sdp->sd_hash_ptrs; x--; lp++)
  774. *lp = cpu_to_be64(bn);
  775. dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
  776. dip->i_di.di_blocks++;
  777. dip->i_di.di_flags |= GFS2_DIF_EXHASH;
  778. dip->i_di.di_payload_format = 0;
  779. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  780. dip->i_di.di_depth = y;
  781. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  782. brelse(dibh);
  783. return 0;
  784. }
  785. /**
  786. * dir_split_leaf - Split a leaf block into two
  787. * @dip: The GFS2 inode
  788. * @index:
  789. * @leaf_no:
  790. *
  791. * Returns: 0 on success, error code on failure
  792. */
  793. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  794. {
  795. struct gfs2_inode *dip = GFS2_I(inode);
  796. struct buffer_head *nbh, *obh, *dibh;
  797. struct gfs2_leaf *nleaf, *oleaf;
  798. struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
  799. uint32_t start, len, half_len, divider;
  800. uint64_t bn, *lp, leaf_no;
  801. uint32_t index;
  802. int x, moved = 0;
  803. int error;
  804. index = name->hash >> (32 - dip->i_di.di_depth);
  805. error = get_leaf_nr(dip, index, &leaf_no);
  806. if (error)
  807. return error;
  808. /* Get the old leaf block */
  809. error = get_leaf(dip, leaf_no, &obh);
  810. if (error)
  811. return error;
  812. oleaf = (struct gfs2_leaf *)obh->b_data;
  813. if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
  814. brelse(obh);
  815. return 1; /* can't split */
  816. }
  817. gfs2_trans_add_bh(dip->i_gl, obh, 1);
  818. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  819. if (!nleaf) {
  820. brelse(obh);
  821. return -ENOSPC;
  822. }
  823. bn = nbh->b_blocknr;
  824. /* Compute the start and len of leaf pointers in the hash table. */
  825. len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
  826. half_len = len >> 1;
  827. if (!half_len) {
  828. printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
  829. gfs2_consist_inode(dip);
  830. error = -EIO;
  831. goto fail_brelse;
  832. }
  833. start = (index & ~(len - 1));
  834. /* Change the pointers.
  835. Don't bother distinguishing stuffed from non-stuffed.
  836. This code is complicated enough already. */
  837. lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
  838. /* Change the pointers */
  839. for (x = 0; x < half_len; x++)
  840. lp[x] = cpu_to_be64(bn);
  841. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
  842. half_len * sizeof(uint64_t));
  843. if (error != half_len * sizeof(uint64_t)) {
  844. if (error >= 0)
  845. error = -EIO;
  846. goto fail_lpfree;
  847. }
  848. kfree(lp);
  849. /* Compute the divider */
  850. divider = (start + half_len) << (32 - dip->i_di.di_depth);
  851. /* Copy the entries */
  852. dirent_first(dip, obh, &dent);
  853. do {
  854. next = dent;
  855. if (dirent_next(dip, obh, &next))
  856. next = NULL;
  857. if (dent->de_inum.no_addr &&
  858. be32_to_cpu(dent->de_hash) < divider) {
  859. struct qstr str;
  860. str.name = (char*)(dent+1);
  861. str.len = be16_to_cpu(dent->de_name_len);
  862. str.hash = be32_to_cpu(dent->de_hash);
  863. new = gfs2_dirent_alloc(inode, nbh, &str);
  864. if (IS_ERR(new)) {
  865. error = PTR_ERR(new);
  866. break;
  867. }
  868. new->de_inum = dent->de_inum; /* No endian worries */
  869. new->de_type = dent->de_type; /* No endian worries */
  870. nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
  871. dirent_del(dip, obh, prev, dent);
  872. if (!oleaf->lf_entries)
  873. gfs2_consist_inode(dip);
  874. oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
  875. if (!prev)
  876. prev = dent;
  877. moved = 1;
  878. } else {
  879. prev = dent;
  880. }
  881. dent = next;
  882. } while (dent);
  883. oleaf->lf_depth = nleaf->lf_depth;
  884. error = gfs2_meta_inode_buffer(dip, &dibh);
  885. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  886. dip->i_di.di_blocks++;
  887. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  888. brelse(dibh);
  889. }
  890. brelse(obh);
  891. brelse(nbh);
  892. return error;
  893. fail_lpfree:
  894. kfree(lp);
  895. fail_brelse:
  896. brelse(obh);
  897. brelse(nbh);
  898. return error;
  899. }
  900. /**
  901. * dir_double_exhash - Double size of ExHash table
  902. * @dip: The GFS2 dinode
  903. *
  904. * Returns: 0 on success, error code on failure
  905. */
  906. static int dir_double_exhash(struct gfs2_inode *dip)
  907. {
  908. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  909. struct buffer_head *dibh;
  910. uint32_t hsize;
  911. uint64_t *buf;
  912. uint64_t *from, *to;
  913. uint64_t block;
  914. int x;
  915. int error = 0;
  916. hsize = 1 << dip->i_di.di_depth;
  917. if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
  918. gfs2_consist_inode(dip);
  919. return -EIO;
  920. }
  921. /* Allocate both the "from" and "to" buffers in one big chunk */
  922. buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
  923. for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
  924. error = gfs2_dir_read_data(dip, (char *)buf,
  925. block * sdp->sd_hash_bsize,
  926. sdp->sd_hash_bsize);
  927. if (error != sdp->sd_hash_bsize) {
  928. if (error >= 0)
  929. error = -EIO;
  930. goto fail;
  931. }
  932. from = buf;
  933. to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
  934. for (x = sdp->sd_hash_ptrs; x--; from++) {
  935. *to++ = *from; /* No endianess worries */
  936. *to++ = *from;
  937. }
  938. error = gfs2_dir_write_data(dip,
  939. (char *)buf + sdp->sd_hash_bsize,
  940. block * sdp->sd_sb.sb_bsize,
  941. sdp->sd_sb.sb_bsize);
  942. if (error != sdp->sd_sb.sb_bsize) {
  943. if (error >= 0)
  944. error = -EIO;
  945. goto fail;
  946. }
  947. }
  948. kfree(buf);
  949. error = gfs2_meta_inode_buffer(dip, &dibh);
  950. if (!gfs2_assert_withdraw(sdp, !error)) {
  951. dip->i_di.di_depth++;
  952. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  953. brelse(dibh);
  954. }
  955. return error;
  956. fail:
  957. kfree(buf);
  958. return error;
  959. }
  960. /**
  961. * compare_dents - compare directory entries by hash value
  962. * @a: first dent
  963. * @b: second dent
  964. *
  965. * When comparing the hash entries of @a to @b:
  966. * gt: returns 1
  967. * lt: returns -1
  968. * eq: returns 0
  969. */
  970. static int compare_dents(const void *a, const void *b)
  971. {
  972. struct gfs2_dirent *dent_a, *dent_b;
  973. uint32_t hash_a, hash_b;
  974. int ret = 0;
  975. dent_a = *(struct gfs2_dirent **)a;
  976. hash_a = be32_to_cpu(dent_a->de_hash);
  977. dent_b = *(struct gfs2_dirent **)b;
  978. hash_b = be32_to_cpu(dent_b->de_hash);
  979. if (hash_a > hash_b)
  980. ret = 1;
  981. else if (hash_a < hash_b)
  982. ret = -1;
  983. else {
  984. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  985. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  986. if (len_a > len_b)
  987. ret = 1;
  988. else if (len_a < len_b)
  989. ret = -1;
  990. else
  991. ret = memcmp((char *)(dent_a + 1),
  992. (char *)(dent_b + 1),
  993. len_a);
  994. }
  995. return ret;
  996. }
  997. /**
  998. * do_filldir_main - read out directory entries
  999. * @dip: The GFS2 inode
  1000. * @offset: The offset in the file to read from
  1001. * @opaque: opaque data to pass to filldir
  1002. * @filldir: The function to pass entries to
  1003. * @darr: an array of struct gfs2_dirent pointers to read
  1004. * @entries: the number of entries in darr
  1005. * @copied: pointer to int that's non-zero if a entry has been copied out
  1006. *
  1007. * Jump through some hoops to make sure that if there are hash collsions,
  1008. * they are read out at the beginning of a buffer. We want to minimize
  1009. * the possibility that they will fall into different readdir buffers or
  1010. * that someone will want to seek to that location.
  1011. *
  1012. * Returns: errno, >0 on exception from filldir
  1013. */
  1014. static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
  1015. void *opaque, gfs2_filldir_t filldir,
  1016. const struct gfs2_dirent **darr, uint32_t entries,
  1017. int *copied)
  1018. {
  1019. const struct gfs2_dirent *dent, *dent_next;
  1020. struct gfs2_inum inum;
  1021. uint64_t 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. gfs2_inum_in(&inum, (char *)&dent->de_inum);
  1051. error = filldir(opaque, (char *)(dent + 1),
  1052. be16_to_cpu(dent->de_name_len),
  1053. off, &inum,
  1054. be16_to_cpu(dent->de_type));
  1055. if (error)
  1056. return 1;
  1057. *copied = 1;
  1058. }
  1059. /* Increment the *offset by one, so the next time we come into the
  1060. do_filldir fxn, we get the next entry instead of the last one in the
  1061. current leaf */
  1062. (*offset)++;
  1063. return 0;
  1064. }
  1065. static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
  1066. gfs2_filldir_t filldir, int *copied,
  1067. unsigned *depth, u64 leaf_no)
  1068. {
  1069. struct gfs2_inode *ip = GFS2_I(inode);
  1070. struct buffer_head *bh;
  1071. struct gfs2_leaf *lf;
  1072. unsigned entries = 0;
  1073. unsigned leaves = 0;
  1074. const struct gfs2_dirent **darr, *dent;
  1075. struct dirent_gather g;
  1076. struct buffer_head **larr;
  1077. int leaf = 0;
  1078. int error, i;
  1079. u64 lfn = leaf_no;
  1080. do {
  1081. error = get_leaf(ip, lfn, &bh);
  1082. if (error)
  1083. goto out;
  1084. lf = (struct gfs2_leaf *)bh->b_data;
  1085. if (leaves == 0)
  1086. *depth = be16_to_cpu(lf->lf_depth);
  1087. entries += be16_to_cpu(lf->lf_entries);
  1088. leaves++;
  1089. lfn = be64_to_cpu(lf->lf_next);
  1090. brelse(bh);
  1091. } while(lfn);
  1092. if (!entries)
  1093. return 0;
  1094. error = -ENOMEM;
  1095. larr = vmalloc((leaves + entries) * sizeof(void*));
  1096. if (!larr)
  1097. goto out;
  1098. darr = (const struct gfs2_dirent **)(larr + leaves);
  1099. g.pdent = darr;
  1100. g.offset = 0;
  1101. lfn = leaf_no;
  1102. do {
  1103. error = get_leaf(ip, lfn, &bh);
  1104. if (error)
  1105. goto out_kfree;
  1106. lf = (struct gfs2_leaf *)bh->b_data;
  1107. lfn = be64_to_cpu(lf->lf_next);
  1108. if (lf->lf_entries) {
  1109. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1110. gfs2_dirent_gather, NULL, &g);
  1111. error = PTR_ERR(dent);
  1112. if (IS_ERR(dent)) {
  1113. goto out_kfree;
  1114. }
  1115. error = 0;
  1116. larr[leaf++] = bh;
  1117. } else {
  1118. brelse(bh);
  1119. }
  1120. } while(lfn);
  1121. error = do_filldir_main(ip, offset, opaque, filldir, darr,
  1122. entries, copied);
  1123. out_kfree:
  1124. for(i = 0; i < leaf; i++)
  1125. brelse(larr[i]);
  1126. vfree(larr);
  1127. out:
  1128. return error;
  1129. }
  1130. /**
  1131. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1132. * @dip: dinode pointer
  1133. * @offset: the hash of the last entry read shifted to the right once
  1134. * @opaque: buffer for the filldir function to fill
  1135. * @filldir: points to the filldir function to use
  1136. *
  1137. * Returns: errno
  1138. */
  1139. static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque,
  1140. gfs2_filldir_t filldir)
  1141. {
  1142. struct gfs2_inode *dip = GFS2_I(inode);
  1143. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1144. uint32_t hsize, len = 0;
  1145. uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
  1146. uint32_t hash, index;
  1147. uint64_t *lp;
  1148. int copied = 0;
  1149. int error = 0;
  1150. unsigned depth;
  1151. hsize = 1 << dip->i_di.di_depth;
  1152. if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
  1153. gfs2_consist_inode(dip);
  1154. return -EIO;
  1155. }
  1156. hash = gfs2_dir_offset2hash(*offset);
  1157. index = hash >> (32 - dip->i_di.di_depth);
  1158. lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
  1159. if (!lp)
  1160. return -ENOMEM;
  1161. while (index < hsize) {
  1162. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1163. ht_offset = index - lp_offset;
  1164. if (ht_offset_cur != ht_offset) {
  1165. error = gfs2_dir_read_data(dip, (char *)lp,
  1166. ht_offset * sizeof(uint64_t),
  1167. sdp->sd_hash_bsize);
  1168. if (error != sdp->sd_hash_bsize) {
  1169. if (error >= 0)
  1170. error = -EIO;
  1171. goto out;
  1172. }
  1173. ht_offset_cur = ht_offset;
  1174. }
  1175. error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
  1176. &copied, &depth,
  1177. be64_to_cpu(lp[lp_offset]));
  1178. if (error)
  1179. break;
  1180. len = 1 << (dip->i_di.di_depth - depth);
  1181. index = (index & ~(len - 1)) + len;
  1182. }
  1183. out:
  1184. kfree(lp);
  1185. if (error > 0)
  1186. error = 0;
  1187. return error;
  1188. }
  1189. int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque,
  1190. gfs2_filldir_t filldir)
  1191. {
  1192. struct gfs2_inode *dip = GFS2_I(inode);
  1193. struct dirent_gather g;
  1194. const struct gfs2_dirent **darr, *dent;
  1195. struct buffer_head *dibh;
  1196. int copied = 0;
  1197. int error;
  1198. if (!dip->i_di.di_entries)
  1199. return 0;
  1200. if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
  1201. return dir_e_read(inode, offset, opaque, filldir);
  1202. if (!gfs2_is_stuffed(dip)) {
  1203. gfs2_consist_inode(dip);
  1204. return -EIO;
  1205. }
  1206. error = gfs2_meta_inode_buffer(dip, &dibh);
  1207. if (error)
  1208. return error;
  1209. error = -ENOMEM;
  1210. darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
  1211. GFP_KERNEL);
  1212. if (darr) {
  1213. g.pdent = darr;
  1214. g.offset = 0;
  1215. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1216. gfs2_dirent_gather, NULL, &g);
  1217. if (IS_ERR(dent)) {
  1218. error = PTR_ERR(dent);
  1219. goto out;
  1220. }
  1221. error = do_filldir_main(dip, offset, opaque, filldir, darr,
  1222. dip->i_di.di_entries, &copied);
  1223. out:
  1224. kfree(darr);
  1225. }
  1226. if (error > 0)
  1227. error = 0;
  1228. brelse(dibh);
  1229. return error;
  1230. }
  1231. /**
  1232. * gfs2_dir_search - Search a directory
  1233. * @dip: The GFS2 inode
  1234. * @filename:
  1235. * @inode:
  1236. *
  1237. * This routine searches a directory for a file or another directory.
  1238. * Assumes a glock is held on dip.
  1239. *
  1240. * Returns: errno
  1241. */
  1242. int gfs2_dir_search(struct inode *dir, const struct qstr *name,
  1243. struct gfs2_inum *inum, unsigned int *type)
  1244. {
  1245. struct buffer_head *bh;
  1246. struct gfs2_dirent *dent;
  1247. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1248. if (dent) {
  1249. if (IS_ERR(dent))
  1250. return PTR_ERR(dent);
  1251. if (inum)
  1252. gfs2_inum_in(inum, (char *)&dent->de_inum);
  1253. if (type)
  1254. *type = be16_to_cpu(dent->de_type);
  1255. brelse(bh);
  1256. return 0;
  1257. }
  1258. return -ENOENT;
  1259. }
  1260. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1261. {
  1262. struct buffer_head *bh, *obh;
  1263. struct gfs2_inode *ip = GFS2_I(inode);
  1264. struct gfs2_leaf *leaf, *oleaf;
  1265. int error;
  1266. u32 index;
  1267. u64 bn;
  1268. index = name->hash >> (32 - ip->i_di.di_depth);
  1269. error = get_first_leaf(ip, index, &obh);
  1270. if (error)
  1271. return error;
  1272. do {
  1273. oleaf = (struct gfs2_leaf *)obh->b_data;
  1274. bn = be64_to_cpu(oleaf->lf_next);
  1275. if (!bn)
  1276. break;
  1277. brelse(obh);
  1278. error = get_leaf(ip, bn, &obh);
  1279. if (error)
  1280. return error;
  1281. } while(1);
  1282. gfs2_trans_add_bh(ip->i_gl, obh, 1);
  1283. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1284. if (!leaf) {
  1285. brelse(obh);
  1286. return -ENOSPC;
  1287. }
  1288. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1289. brelse(bh);
  1290. brelse(obh);
  1291. error = gfs2_meta_inode_buffer(ip, &bh);
  1292. if (error)
  1293. return error;
  1294. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1295. ip->i_di.di_blocks++;
  1296. gfs2_dinode_out(&ip->i_di, 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 *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_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  1337. gfs2_dinode_out(&ip->i_di, 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_di.di_mtime = dip->i_di.di_ctime = get_seconds();
  1416. gfs2_dinode_out(&dip->i_di, 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 *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_di.di_mtime = dip->i_di.di_ctime = get_seconds();
  1457. gfs2_dinode_out(&dip->i_di, 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. uint32_t hsize, len;
  1475. uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
  1476. uint32_t index = 0;
  1477. uint64_t *lp;
  1478. uint64_t leaf_no;
  1479. int error = 0;
  1480. hsize = 1 << dip->i_di.di_depth;
  1481. if (hsize * sizeof(uint64_t) != 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(uint64_t),
  1494. sdp->sd_hash_bsize);
  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. brelse(bh);
  1509. len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
  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, uint32_t index, uint32_t len,
  1536. uint64_t 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. uint64_t blk, nblk;
  1543. unsigned int rg_blocks = 0, l_blocks = 0;
  1544. char *ht;
  1545. unsigned int x, size = len * sizeof(uint64_t);
  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. }
  1595. error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
  1596. if (error != size) {
  1597. if (error >= 0)
  1598. error = -EIO;
  1599. goto out_end_trans;
  1600. }
  1601. error = gfs2_meta_inode_buffer(dip, &dibh);
  1602. if (error)
  1603. goto out_end_trans;
  1604. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  1605. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  1606. brelse(dibh);
  1607. out_end_trans:
  1608. gfs2_trans_end(sdp);
  1609. out_rg_gunlock:
  1610. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1611. out_rlist:
  1612. gfs2_rlist_free(&rlist);
  1613. gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
  1614. out_qs:
  1615. gfs2_quota_unhold(dip);
  1616. out:
  1617. gfs2_alloc_put(dip);
  1618. kfree(ht);
  1619. return error;
  1620. }
  1621. /**
  1622. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1623. * @dip: the directory
  1624. *
  1625. * Dealloc all on-disk directory leaves to FREEMETA state
  1626. * Change on-disk inode type to "regular file"
  1627. *
  1628. * Returns: errno
  1629. */
  1630. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1631. {
  1632. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1633. struct buffer_head *bh;
  1634. int error;
  1635. /* Dealloc on-disk leaves to FREEMETA state */
  1636. error = foreach_leaf(dip, leaf_dealloc, NULL);
  1637. if (error)
  1638. return error;
  1639. /* Make this a regular file in case we crash.
  1640. (We don't want to free these blocks a second time.) */
  1641. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1642. if (error)
  1643. return error;
  1644. error = gfs2_meta_inode_buffer(dip, &bh);
  1645. if (!error) {
  1646. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1647. ((struct gfs2_dinode *)bh->b_data)->di_mode =
  1648. cpu_to_be32(S_IFREG);
  1649. brelse(bh);
  1650. }
  1651. gfs2_trans_end(sdp);
  1652. return error;
  1653. }
  1654. /**
  1655. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1656. * @ip: the file being written to
  1657. * @filname: the filename that's going to be added
  1658. *
  1659. * Returns: 1 if alloc required, 0 if not, -ve on error
  1660. */
  1661. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
  1662. {
  1663. struct gfs2_dirent *dent;
  1664. struct buffer_head *bh;
  1665. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1666. if (!dent) {
  1667. return 1;
  1668. }
  1669. if (IS_ERR(dent))
  1670. return PTR_ERR(dent);
  1671. brelse(bh);
  1672. return 0;
  1673. }