dir.c 46 KB

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