dir.c 47 KB

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