dir.c 46 KB

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