dir.c 47 KB

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