dir.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. /*
  10. * Implements Extendible Hashing as described in:
  11. * "Extendible Hashing" by Fagin, et al in
  12. * __ACM Trans. on Database Systems__, Sept 1979.
  13. *
  14. *
  15. * Here's the layout of dirents which is essentially the same as that of ext2
  16. * within a single block. The field de_name_len is the number of bytes
  17. * actually required for the name (no null terminator). The field de_rec_len
  18. * is the number of bytes allocated to the dirent. The offset of the next
  19. * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
  20. * deleted, the preceding dirent inherits its allocated space, ie
  21. * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
  22. * by adding de_rec_len to the current dirent, this essentially causes the
  23. * deleted dirent to get jumped over when iterating through all the dirents.
  24. *
  25. * When deleting the first dirent in a block, there is no previous dirent so
  26. * the field de_ino is set to zero to designate it as deleted. When allocating
  27. * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
  28. * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
  29. * dirent is allocated. Otherwise it must go through all the 'used' dirents
  30. * searching for one in which the amount of total space minus the amount of
  31. * used space will provide enough space for the new dirent.
  32. *
  33. * There are two types of blocks in which dirents reside. In a stuffed dinode,
  34. * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
  35. * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
  36. * beginning of the leaf block. The dirents reside in leaves when
  37. *
  38. * dip->i_di.di_flags & GFS2_DIF_EXHASH is true
  39. *
  40. * Otherwise, the dirents are "linear", within a single stuffed dinode block.
  41. *
  42. * When the dirents are in leaves, the actual contents of the directory file are
  43. * used as an array of 64-bit block pointers pointing to the leaf blocks. The
  44. * dirents are NOT in the directory file itself. There can be more than one
  45. * block pointer in the array that points to the same leaf. In fact, when a
  46. * directory is first converted from linear to exhash, all of the pointers
  47. * point to the same leaf.
  48. *
  49. * When a leaf is completely full, the size of the hash table can be
  50. * doubled unless it is already at the maximum size which is hard coded into
  51. * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
  52. * but never before the maximum hash table size has been reached.
  53. */
  54. #include <linux/sched.h>
  55. #include <linux/slab.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/buffer_head.h>
  58. #include <linux/sort.h>
  59. #include <linux/gfs2_ondisk.h>
  60. #include <linux/crc32.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/lm_interface.h>
  63. #include "gfs2.h"
  64. #include "incore.h"
  65. #include "dir.h"
  66. #include "glock.h"
  67. #include "inode.h"
  68. #include "meta_io.h"
  69. #include "quota.h"
  70. #include "rgrp.h"
  71. #include "trans.h"
  72. #include "bmap.h"
  73. #include "util.h"
  74. #define IS_LEAF 1 /* Hashed (leaf) directory */
  75. #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
  76. #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
  77. #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
  78. typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
  79. u64 leaf_no, void *data);
  80. typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
  81. const struct qstr *name, void *opaque);
  82. int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
  83. struct buffer_head **bhp)
  84. {
  85. struct buffer_head *bh;
  86. bh = gfs2_meta_new(ip->i_gl, block);
  87. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  88. gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
  89. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  90. *bhp = bh;
  91. return 0;
  92. }
  93. static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
  94. struct buffer_head **bhp)
  95. {
  96. struct buffer_head *bh;
  97. int error;
  98. error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
  99. if (error)
  100. return error;
  101. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
  102. brelse(bh);
  103. return -EIO;
  104. }
  105. *bhp = bh;
  106. return 0;
  107. }
  108. static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
  109. unsigned int offset, unsigned int size)
  110. {
  111. struct buffer_head *dibh;
  112. int error;
  113. error = gfs2_meta_inode_buffer(ip, &dibh);
  114. if (error)
  115. return error;
  116. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  117. memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
  118. if (ip->i_di.di_size < offset + size)
  119. ip->i_di.di_size = offset + size;
  120. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  121. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  122. brelse(dibh);
  123. return size;
  124. }
  125. /**
  126. * gfs2_dir_write_data - Write directory information to the inode
  127. * @ip: The GFS2 inode
  128. * @buf: The buffer containing information to be written
  129. * @offset: The file offset to start writing at
  130. * @size: The amount of data to write
  131. *
  132. * Returns: The number of bytes correctly written or error code
  133. */
  134. static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
  135. u64 offset, unsigned int size)
  136. {
  137. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  138. struct buffer_head *dibh;
  139. u64 lblock, dblock;
  140. u32 extlen = 0;
  141. unsigned int o;
  142. int copied = 0;
  143. int error = 0;
  144. if (!size)
  145. return 0;
  146. if (gfs2_is_stuffed(ip) &&
  147. offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  148. return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
  149. size);
  150. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  151. return -EINVAL;
  152. if (gfs2_is_stuffed(ip)) {
  153. error = gfs2_unstuff_dinode(ip, NULL);
  154. if (error)
  155. return error;
  156. }
  157. lblock = offset;
  158. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  159. while (copied < size) {
  160. unsigned int amount;
  161. struct buffer_head *bh;
  162. int new = 0;
  163. amount = size - copied;
  164. if (amount > sdp->sd_sb.sb_bsize - o)
  165. amount = sdp->sd_sb.sb_bsize - o;
  166. if (!extlen) {
  167. new = 1;
  168. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  169. &dblock, &extlen);
  170. if (error)
  171. goto fail;
  172. error = -EIO;
  173. if (gfs2_assert_withdraw(sdp, dblock))
  174. goto fail;
  175. }
  176. if (amount == sdp->sd_jbsize || new)
  177. error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
  178. else
  179. error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
  180. if (error)
  181. goto fail;
  182. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  183. memcpy(bh->b_data + o, buf, amount);
  184. brelse(bh);
  185. buf += amount;
  186. copied += amount;
  187. lblock++;
  188. dblock++;
  189. extlen--;
  190. o = sizeof(struct gfs2_meta_header);
  191. }
  192. out:
  193. error = gfs2_meta_inode_buffer(ip, &dibh);
  194. if (error)
  195. return error;
  196. if (ip->i_di.di_size < offset + copied)
  197. ip->i_di.di_size = offset + copied;
  198. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  199. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  200. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  201. brelse(dibh);
  202. return copied;
  203. fail:
  204. if (copied)
  205. goto out;
  206. return error;
  207. }
  208. static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
  209. u64 offset, unsigned int size)
  210. {
  211. struct buffer_head *dibh;
  212. int error;
  213. error = gfs2_meta_inode_buffer(ip, &dibh);
  214. if (!error) {
  215. offset += sizeof(struct gfs2_dinode);
  216. memcpy(buf, dibh->b_data + offset, size);
  217. brelse(dibh);
  218. }
  219. return (error) ? error : size;
  220. }
  221. /**
  222. * gfs2_dir_read_data - Read a data from a directory inode
  223. * @ip: The GFS2 Inode
  224. * @buf: The buffer to place result into
  225. * @offset: File offset to begin jdata_readng from
  226. * @size: Amount of data to transfer
  227. *
  228. * Returns: The amount of data actually copied or the error
  229. */
  230. static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
  231. unsigned int size, unsigned ra)
  232. {
  233. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  234. u64 lblock, dblock;
  235. u32 extlen = 0;
  236. unsigned int o;
  237. int copied = 0;
  238. int error = 0;
  239. if (offset >= ip->i_di.di_size)
  240. return 0;
  241. if (offset + size > ip->i_di.di_size)
  242. size = ip->i_di.di_size - offset;
  243. if (!size)
  244. return 0;
  245. if (gfs2_is_stuffed(ip))
  246. return gfs2_dir_read_stuffed(ip, buf, offset, size);
  247. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  248. return -EINVAL;
  249. lblock = offset;
  250. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  251. while (copied < size) {
  252. unsigned int amount;
  253. struct buffer_head *bh;
  254. int new;
  255. amount = size - copied;
  256. if (amount > sdp->sd_sb.sb_bsize - o)
  257. amount = sdp->sd_sb.sb_bsize - o;
  258. if (!extlen) {
  259. new = 0;
  260. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  261. &dblock, &extlen);
  262. if (error || !dblock)
  263. goto fail;
  264. BUG_ON(extlen < 1);
  265. if (!ra)
  266. extlen = 1;
  267. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  268. } else {
  269. error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
  270. if (error)
  271. goto fail;
  272. }
  273. error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
  274. if (error) {
  275. brelse(bh);
  276. goto fail;
  277. }
  278. dblock++;
  279. extlen--;
  280. memcpy(buf, bh->b_data + o, amount);
  281. brelse(bh);
  282. buf += amount;
  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. static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
  292. const struct qstr *name, int ret)
  293. {
  294. if (dent->de_inum.no_addr != 0 &&
  295. be32_to_cpu(dent->de_hash) == name->hash &&
  296. be16_to_cpu(dent->de_name_len) == name->len &&
  297. memcmp(dent+1, name->name, name->len) == 0)
  298. return ret;
  299. return 0;
  300. }
  301. static int gfs2_dirent_find(const struct gfs2_dirent *dent,
  302. const struct qstr *name,
  303. void *opaque)
  304. {
  305. return __gfs2_dirent_find(dent, name, 1);
  306. }
  307. static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
  308. const struct qstr *name,
  309. void *opaque)
  310. {
  311. return __gfs2_dirent_find(dent, name, 2);
  312. }
  313. /*
  314. * name->name holds ptr to start of block.
  315. * name->len holds size of block.
  316. */
  317. static int gfs2_dirent_last(const struct gfs2_dirent *dent,
  318. const struct qstr *name,
  319. void *opaque)
  320. {
  321. const char *start = name->name;
  322. const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
  323. if (name->len == (end - start))
  324. return 1;
  325. return 0;
  326. }
  327. static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
  328. const struct qstr *name,
  329. void *opaque)
  330. {
  331. unsigned required = GFS2_DIRENT_SIZE(name->len);
  332. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  333. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  334. if (!dent->de_inum.no_addr)
  335. actual = GFS2_DIRENT_SIZE(0);
  336. if (totlen - actual >= required)
  337. return 1;
  338. return 0;
  339. }
  340. struct dirent_gather {
  341. const struct gfs2_dirent **pdent;
  342. unsigned offset;
  343. };
  344. static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
  345. const struct qstr *name,
  346. void *opaque)
  347. {
  348. struct dirent_gather *g = opaque;
  349. if (dent->de_inum.no_addr) {
  350. g->pdent[g->offset++] = dent;
  351. }
  352. return 0;
  353. }
  354. /*
  355. * Other possible things to check:
  356. * - Inode located within filesystem size (and on valid block)
  357. * - Valid directory entry type
  358. * Not sure how heavy-weight we want to make this... could also check
  359. * hash is correct for example, but that would take a lot of extra time.
  360. * For now the most important thing is to check that the various sizes
  361. * are correct.
  362. */
  363. static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
  364. unsigned int size, unsigned int len, int first)
  365. {
  366. const char *msg = "gfs2_dirent too small";
  367. if (unlikely(size < sizeof(struct gfs2_dirent)))
  368. goto error;
  369. msg = "gfs2_dirent misaligned";
  370. if (unlikely(offset & 0x7))
  371. goto error;
  372. msg = "gfs2_dirent points beyond end of block";
  373. if (unlikely(offset + size > len))
  374. goto error;
  375. msg = "zero inode number";
  376. if (unlikely(!first && !dent->de_inum.no_addr))
  377. goto error;
  378. msg = "name length is greater than space in dirent";
  379. if (dent->de_inum.no_addr &&
  380. unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
  381. size))
  382. goto error;
  383. return 0;
  384. error:
  385. printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
  386. first ? "first in block" : "not first in block");
  387. return -EIO;
  388. }
  389. static int gfs2_dirent_offset(const void *buf)
  390. {
  391. const struct gfs2_meta_header *h = buf;
  392. int offset;
  393. BUG_ON(buf == NULL);
  394. switch(be32_to_cpu(h->mh_type)) {
  395. case GFS2_METATYPE_LF:
  396. offset = sizeof(struct gfs2_leaf);
  397. break;
  398. case GFS2_METATYPE_DI:
  399. offset = sizeof(struct gfs2_dinode);
  400. break;
  401. default:
  402. goto wrong_type;
  403. }
  404. return offset;
  405. wrong_type:
  406. printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
  407. be32_to_cpu(h->mh_type));
  408. return -1;
  409. }
  410. static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
  411. unsigned int len, gfs2_dscan_t scan,
  412. const struct qstr *name,
  413. void *opaque)
  414. {
  415. struct gfs2_dirent *dent, *prev;
  416. unsigned offset;
  417. unsigned size;
  418. int ret = 0;
  419. ret = gfs2_dirent_offset(buf);
  420. if (ret < 0)
  421. goto consist_inode;
  422. offset = ret;
  423. prev = NULL;
  424. dent = buf + offset;
  425. size = be16_to_cpu(dent->de_rec_len);
  426. if (gfs2_check_dirent(dent, offset, size, len, 1))
  427. goto consist_inode;
  428. do {
  429. ret = scan(dent, name, opaque);
  430. if (ret)
  431. break;
  432. offset += size;
  433. if (offset == len)
  434. break;
  435. prev = dent;
  436. dent = buf + offset;
  437. size = be16_to_cpu(dent->de_rec_len);
  438. if (gfs2_check_dirent(dent, offset, size, len, 0))
  439. goto consist_inode;
  440. } while(1);
  441. switch(ret) {
  442. case 0:
  443. return NULL;
  444. case 1:
  445. return dent;
  446. case 2:
  447. return prev ? prev : dent;
  448. default:
  449. BUG_ON(ret > 0);
  450. return ERR_PTR(ret);
  451. }
  452. consist_inode:
  453. gfs2_consist_inode(GFS2_I(inode));
  454. return ERR_PTR(-EIO);
  455. }
  456. /**
  457. * dirent_first - Return the first dirent
  458. * @dip: the directory
  459. * @bh: The buffer
  460. * @dent: Pointer to list of dirents
  461. *
  462. * return first dirent whether bh points to leaf or stuffed dinode
  463. *
  464. * Returns: IS_LEAF, IS_DINODE, or -errno
  465. */
  466. static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
  467. struct gfs2_dirent **dent)
  468. {
  469. struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
  470. if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
  471. if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
  472. return -EIO;
  473. *dent = (struct gfs2_dirent *)(bh->b_data +
  474. sizeof(struct gfs2_leaf));
  475. return IS_LEAF;
  476. } else {
  477. if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
  478. return -EIO;
  479. *dent = (struct gfs2_dirent *)(bh->b_data +
  480. sizeof(struct gfs2_dinode));
  481. return IS_DINODE;
  482. }
  483. }
  484. static int dirent_check_reclen(struct gfs2_inode *dip,
  485. const struct gfs2_dirent *d, const void *end_p)
  486. {
  487. const void *ptr = d;
  488. u16 rec_len = be16_to_cpu(d->de_rec_len);
  489. if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
  490. goto broken;
  491. ptr += rec_len;
  492. if (ptr < end_p)
  493. return rec_len;
  494. if (ptr == end_p)
  495. return -ENOENT;
  496. broken:
  497. gfs2_consist_inode(dip);
  498. return -EIO;
  499. }
  500. /**
  501. * dirent_next - Next dirent
  502. * @dip: the directory
  503. * @bh: The buffer
  504. * @dent: Pointer to list of dirents
  505. *
  506. * Returns: 0 on success, error code otherwise
  507. */
  508. static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
  509. struct gfs2_dirent **dent)
  510. {
  511. struct gfs2_dirent *cur = *dent, *tmp;
  512. char *bh_end = bh->b_data + bh->b_size;
  513. int ret;
  514. ret = dirent_check_reclen(dip, cur, bh_end);
  515. if (ret < 0)
  516. return ret;
  517. tmp = (void *)cur + ret;
  518. ret = dirent_check_reclen(dip, tmp, bh_end);
  519. if (ret == -EIO)
  520. return ret;
  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. u16 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. static 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 = GFS2_I(inode);
  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, u64 leaf_no,
  597. struct buffer_head **bhp)
  598. {
  599. int error;
  600. error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
  601. if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
  602. /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
  603. error = -EIO;
  604. }
  605. return error;
  606. }
  607. /**
  608. * get_leaf_nr - Get a leaf number associated with the index
  609. * @dip: The GFS2 inode
  610. * @index:
  611. * @leaf_out:
  612. *
  613. * Returns: 0 on success, error code otherwise
  614. */
  615. static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
  616. u64 *leaf_out)
  617. {
  618. u64 leaf_no;
  619. int error;
  620. error = gfs2_dir_read_data(dip, (char *)&leaf_no,
  621. index * sizeof(u64),
  622. sizeof(u64), 0);
  623. if (error != sizeof(u64))
  624. return (error < 0) ? error : -EIO;
  625. *leaf_out = be64_to_cpu(leaf_no);
  626. return 0;
  627. }
  628. static int get_first_leaf(struct gfs2_inode *dip, u32 index,
  629. struct buffer_head **bh_out)
  630. {
  631. u64 leaf_no;
  632. int error;
  633. error = get_leaf_nr(dip, index, &leaf_no);
  634. if (!error)
  635. error = get_leaf(dip, leaf_no, bh_out);
  636. return error;
  637. }
  638. static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
  639. const struct qstr *name,
  640. gfs2_dscan_t scan,
  641. struct buffer_head **pbh)
  642. {
  643. struct buffer_head *bh;
  644. struct gfs2_dirent *dent;
  645. struct gfs2_inode *ip = GFS2_I(inode);
  646. int error;
  647. if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
  648. struct gfs2_leaf *leaf;
  649. unsigned hsize = 1 << ip->i_di.di_depth;
  650. unsigned index;
  651. u64 ln;
  652. if (hsize * sizeof(u64) != ip->i_di.di_size) {
  653. gfs2_consist_inode(ip);
  654. return ERR_PTR(-EIO);
  655. }
  656. index = name->hash >> (32 - ip->i_di.di_depth);
  657. error = get_first_leaf(ip, index, &bh);
  658. if (error)
  659. return ERR_PTR(error);
  660. do {
  661. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  662. scan, name, NULL);
  663. if (dent)
  664. goto got_dent;
  665. leaf = (struct gfs2_leaf *)bh->b_data;
  666. ln = be64_to_cpu(leaf->lf_next);
  667. brelse(bh);
  668. if (!ln)
  669. break;
  670. error = get_leaf(ip, ln, &bh);
  671. } while(!error);
  672. return error ? ERR_PTR(error) : NULL;
  673. }
  674. error = gfs2_meta_inode_buffer(ip, &bh);
  675. if (error)
  676. return ERR_PTR(error);
  677. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
  678. got_dent:
  679. if (unlikely(dent == NULL || IS_ERR(dent))) {
  680. brelse(bh);
  681. bh = NULL;
  682. }
  683. *pbh = bh;
  684. return dent;
  685. }
  686. static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
  687. {
  688. struct gfs2_inode *ip = GFS2_I(inode);
  689. u64 bn = gfs2_alloc_meta(ip);
  690. struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
  691. struct gfs2_leaf *leaf;
  692. struct gfs2_dirent *dent;
  693. struct qstr name = { .name = "", .len = 0, .hash = 0 };
  694. if (!bh)
  695. return NULL;
  696. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  697. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  698. leaf = (struct gfs2_leaf *)bh->b_data;
  699. leaf->lf_depth = cpu_to_be16(depth);
  700. leaf->lf_entries = 0;
  701. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  702. leaf->lf_next = 0;
  703. memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
  704. dent = (struct gfs2_dirent *)(leaf+1);
  705. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  706. *pbh = bh;
  707. return leaf;
  708. }
  709. /**
  710. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  711. * @dip: The GFS2 inode
  712. *
  713. * Returns: 0 on success, error code otherwise
  714. */
  715. static int dir_make_exhash(struct inode *inode)
  716. {
  717. struct gfs2_inode *dip = GFS2_I(inode);
  718. struct gfs2_sbd *sdp = GFS2_SB(inode);
  719. struct gfs2_dirent *dent;
  720. struct qstr args;
  721. struct buffer_head *bh, *dibh;
  722. struct gfs2_leaf *leaf;
  723. int y;
  724. u32 x;
  725. u64 *lp, bn;
  726. int error;
  727. error = gfs2_meta_inode_buffer(dip, &dibh);
  728. if (error)
  729. return error;
  730. /* Turn over a new leaf */
  731. leaf = new_leaf(inode, &bh, 0);
  732. if (!leaf)
  733. return -ENOSPC;
  734. bn = bh->b_blocknr;
  735. gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
  736. leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
  737. /* Copy dirents */
  738. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  739. sizeof(struct gfs2_dinode));
  740. /* Find last entry */
  741. x = 0;
  742. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  743. sizeof(struct gfs2_leaf);
  744. args.name = bh->b_data;
  745. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  746. gfs2_dirent_last, &args, NULL);
  747. if (!dent) {
  748. brelse(bh);
  749. brelse(dibh);
  750. return -EIO;
  751. }
  752. if (IS_ERR(dent)) {
  753. brelse(bh);
  754. brelse(dibh);
  755. return PTR_ERR(dent);
  756. }
  757. /* Adjust the last dirent's record length
  758. (Remember that dent still points to the last entry.) */
  759. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  760. sizeof(struct gfs2_dinode) -
  761. sizeof(struct gfs2_leaf));
  762. brelse(bh);
  763. /* We're done with the new leaf block, now setup the new
  764. hash table. */
  765. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  766. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  767. lp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  768. for (x = sdp->sd_hash_ptrs; x--; lp++)
  769. *lp = cpu_to_be64(bn);
  770. dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
  771. dip->i_di.di_blocks++;
  772. dip->i_di.di_flags |= GFS2_DIF_EXHASH;
  773. dip->i_di.di_payload_format = 0;
  774. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  775. dip->i_di.di_depth = y;
  776. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  777. brelse(dibh);
  778. return 0;
  779. }
  780. /**
  781. * dir_split_leaf - Split a leaf block into two
  782. * @dip: The GFS2 inode
  783. * @index:
  784. * @leaf_no:
  785. *
  786. * Returns: 0 on success, error code on failure
  787. */
  788. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  789. {
  790. struct gfs2_inode *dip = GFS2_I(inode);
  791. struct buffer_head *nbh, *obh, *dibh;
  792. struct gfs2_leaf *nleaf, *oleaf;
  793. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  794. u32 start, len, half_len, divider;
  795. u64 bn, *lp, leaf_no;
  796. u32 index;
  797. int x, moved = 0;
  798. int error;
  799. index = name->hash >> (32 - dip->i_di.di_depth);
  800. error = get_leaf_nr(dip, index, &leaf_no);
  801. if (error)
  802. return error;
  803. /* Get the old leaf block */
  804. error = get_leaf(dip, leaf_no, &obh);
  805. if (error)
  806. return error;
  807. oleaf = (struct gfs2_leaf *)obh->b_data;
  808. if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
  809. brelse(obh);
  810. return 1; /* can't split */
  811. }
  812. gfs2_trans_add_bh(dip->i_gl, obh, 1);
  813. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  814. if (!nleaf) {
  815. brelse(obh);
  816. return -ENOSPC;
  817. }
  818. bn = nbh->b_blocknr;
  819. /* Compute the start and len of leaf pointers in the hash table. */
  820. len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
  821. half_len = len >> 1;
  822. if (!half_len) {
  823. printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
  824. gfs2_consist_inode(dip);
  825. error = -EIO;
  826. goto fail_brelse;
  827. }
  828. start = (index & ~(len - 1));
  829. /* Change the pointers.
  830. Don't bother distinguishing stuffed from non-stuffed.
  831. This code is complicated enough already. */
  832. lp = kmalloc(half_len * sizeof(u64), GFP_NOFS | __GFP_NOFAIL);
  833. /* Change the pointers */
  834. for (x = 0; x < half_len; x++)
  835. lp[x] = cpu_to_be64(bn);
  836. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  837. half_len * sizeof(u64));
  838. if (error != half_len * sizeof(u64)) {
  839. if (error >= 0)
  840. error = -EIO;
  841. goto fail_lpfree;
  842. }
  843. kfree(lp);
  844. /* Compute the divider */
  845. divider = (start + half_len) << (32 - dip->i_di.di_depth);
  846. /* Copy the entries */
  847. dirent_first(dip, obh, &dent);
  848. do {
  849. next = dent;
  850. if (dirent_next(dip, obh, &next))
  851. next = NULL;
  852. if (dent->de_inum.no_addr &&
  853. be32_to_cpu(dent->de_hash) < divider) {
  854. struct qstr str;
  855. str.name = (char*)(dent+1);
  856. str.len = be16_to_cpu(dent->de_name_len);
  857. str.hash = be32_to_cpu(dent->de_hash);
  858. new = gfs2_dirent_alloc(inode, nbh, &str);
  859. if (IS_ERR(new)) {
  860. error = PTR_ERR(new);
  861. break;
  862. }
  863. new->de_inum = dent->de_inum; /* No endian worries */
  864. new->de_type = dent->de_type; /* No endian worries */
  865. nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
  866. dirent_del(dip, obh, prev, dent);
  867. if (!oleaf->lf_entries)
  868. gfs2_consist_inode(dip);
  869. oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
  870. if (!prev)
  871. prev = dent;
  872. moved = 1;
  873. } else {
  874. prev = dent;
  875. }
  876. dent = next;
  877. } while (dent);
  878. oleaf->lf_depth = nleaf->lf_depth;
  879. error = gfs2_meta_inode_buffer(dip, &dibh);
  880. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  881. dip->i_di.di_blocks++;
  882. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  883. brelse(dibh);
  884. }
  885. brelse(obh);
  886. brelse(nbh);
  887. return error;
  888. fail_lpfree:
  889. kfree(lp);
  890. fail_brelse:
  891. brelse(obh);
  892. brelse(nbh);
  893. return error;
  894. }
  895. /**
  896. * dir_double_exhash - Double size of ExHash table
  897. * @dip: The GFS2 dinode
  898. *
  899. * Returns: 0 on success, error code on failure
  900. */
  901. static int dir_double_exhash(struct gfs2_inode *dip)
  902. {
  903. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  904. struct buffer_head *dibh;
  905. u32 hsize;
  906. u64 *buf;
  907. u64 *from, *to;
  908. u64 block;
  909. int x;
  910. int error = 0;
  911. hsize = 1 << dip->i_di.di_depth;
  912. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  913. gfs2_consist_inode(dip);
  914. return -EIO;
  915. }
  916. /* Allocate both the "from" and "to" buffers in one big chunk */
  917. buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
  918. for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
  919. error = gfs2_dir_read_data(dip, (char *)buf,
  920. block * sdp->sd_hash_bsize,
  921. sdp->sd_hash_bsize, 1);
  922. if (error != sdp->sd_hash_bsize) {
  923. if (error >= 0)
  924. error = -EIO;
  925. goto fail;
  926. }
  927. from = buf;
  928. to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
  929. for (x = sdp->sd_hash_ptrs; x--; from++) {
  930. *to++ = *from; /* No endianess worries */
  931. *to++ = *from;
  932. }
  933. error = gfs2_dir_write_data(dip,
  934. (char *)buf + sdp->sd_hash_bsize,
  935. block * sdp->sd_sb.sb_bsize,
  936. sdp->sd_sb.sb_bsize);
  937. if (error != sdp->sd_sb.sb_bsize) {
  938. if (error >= 0)
  939. error = -EIO;
  940. goto fail;
  941. }
  942. }
  943. kfree(buf);
  944. error = gfs2_meta_inode_buffer(dip, &dibh);
  945. if (!gfs2_assert_withdraw(sdp, !error)) {
  946. dip->i_di.di_depth++;
  947. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  948. brelse(dibh);
  949. }
  950. return error;
  951. fail:
  952. kfree(buf);
  953. return error;
  954. }
  955. /**
  956. * compare_dents - compare directory entries by hash value
  957. * @a: first dent
  958. * @b: second dent
  959. *
  960. * When comparing the hash entries of @a to @b:
  961. * gt: returns 1
  962. * lt: returns -1
  963. * eq: returns 0
  964. */
  965. static int compare_dents(const void *a, const void *b)
  966. {
  967. const struct gfs2_dirent *dent_a, *dent_b;
  968. u32 hash_a, hash_b;
  969. int ret = 0;
  970. dent_a = *(const struct gfs2_dirent **)a;
  971. hash_a = be32_to_cpu(dent_a->de_hash);
  972. dent_b = *(const struct gfs2_dirent **)b;
  973. hash_b = be32_to_cpu(dent_b->de_hash);
  974. if (hash_a > hash_b)
  975. ret = 1;
  976. else if (hash_a < hash_b)
  977. ret = -1;
  978. else {
  979. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  980. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  981. if (len_a > len_b)
  982. ret = 1;
  983. else if (len_a < len_b)
  984. ret = -1;
  985. else
  986. ret = memcmp(dent_a + 1, dent_b + 1, 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, u64 *offset,
  1008. void *opaque, gfs2_filldir_t filldir,
  1009. const struct gfs2_dirent **darr, u32 entries,
  1010. int *copied)
  1011. {
  1012. const struct gfs2_dirent *dent, *dent_next;
  1013. struct gfs2_inum inum;
  1014. u64 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, (const 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 = GFS2_I(inode);
  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, u64 *offset, void *opaque,
  1133. gfs2_filldir_t filldir)
  1134. {
  1135. struct gfs2_inode *dip = GFS2_I(inode);
  1136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1137. u32 hsize, len = 0;
  1138. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1139. u32 hash, index;
  1140. u64 *lp;
  1141. int copied = 0;
  1142. int error = 0;
  1143. unsigned depth = 0;
  1144. hsize = 1 << dip->i_di.di_depth;
  1145. if (hsize * sizeof(u64) != 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(u64),
  1160. sdp->sd_hash_bsize, 1);
  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, u64 *offset, void *opaque,
  1183. gfs2_filldir_t filldir)
  1184. {
  1185. struct gfs2_inode *dip = GFS2_I(inode);
  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 = GFS2_I(inode);
  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 = GFS2_I(inode);
  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_inode, 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. mark_inode_dirty(&dip->i_inode);
  1412. return error;
  1413. }
  1414. /**
  1415. * gfs2_dir_mvino - Change inode number of directory entry
  1416. * @dip: The GFS2 inode
  1417. * @filename:
  1418. * @new_inode:
  1419. *
  1420. * This routine changes the inode number of a directory entry. It's used
  1421. * by rename to change ".." when a directory is moved.
  1422. * Assumes a glock is held on dvp.
  1423. *
  1424. * Returns: errno
  1425. */
  1426. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1427. struct gfs2_inum *inum, unsigned int new_type)
  1428. {
  1429. struct buffer_head *bh;
  1430. struct gfs2_dirent *dent;
  1431. int error;
  1432. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1433. if (!dent) {
  1434. gfs2_consist_inode(dip);
  1435. return -EIO;
  1436. }
  1437. if (IS_ERR(dent))
  1438. return PTR_ERR(dent);
  1439. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1440. gfs2_inum_out(inum, (char *)&dent->de_inum);
  1441. dent->de_type = cpu_to_be16(new_type);
  1442. if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1443. brelse(bh);
  1444. error = gfs2_meta_inode_buffer(dip, &bh);
  1445. if (error)
  1446. return error;
  1447. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1448. }
  1449. dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
  1450. gfs2_dinode_out(&dip->i_di, bh->b_data);
  1451. brelse(bh);
  1452. return 0;
  1453. }
  1454. /**
  1455. * foreach_leaf - call a function for each leaf in a directory
  1456. * @dip: the directory
  1457. * @lc: the function to call for each each
  1458. * @data: private data to pass to it
  1459. *
  1460. * Returns: errno
  1461. */
  1462. static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
  1463. {
  1464. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1465. struct buffer_head *bh;
  1466. struct gfs2_leaf *leaf;
  1467. u32 hsize, len;
  1468. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1469. u32 index = 0;
  1470. u64 *lp;
  1471. u64 leaf_no;
  1472. int error = 0;
  1473. hsize = 1 << dip->i_di.di_depth;
  1474. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  1475. gfs2_consist_inode(dip);
  1476. return -EIO;
  1477. }
  1478. lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
  1479. if (!lp)
  1480. return -ENOMEM;
  1481. while (index < hsize) {
  1482. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1483. ht_offset = index - lp_offset;
  1484. if (ht_offset_cur != ht_offset) {
  1485. error = gfs2_dir_read_data(dip, (char *)lp,
  1486. ht_offset * sizeof(u64),
  1487. sdp->sd_hash_bsize, 1);
  1488. if (error != sdp->sd_hash_bsize) {
  1489. if (error >= 0)
  1490. error = -EIO;
  1491. goto out;
  1492. }
  1493. ht_offset_cur = ht_offset;
  1494. }
  1495. leaf_no = be64_to_cpu(lp[lp_offset]);
  1496. if (leaf_no) {
  1497. error = get_leaf(dip, leaf_no, &bh);
  1498. if (error)
  1499. goto out;
  1500. leaf = (struct gfs2_leaf *)bh->b_data;
  1501. len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
  1502. brelse(bh);
  1503. error = lc(dip, index, len, leaf_no, data);
  1504. if (error)
  1505. goto out;
  1506. index = (index & ~(len - 1)) + len;
  1507. } else
  1508. index++;
  1509. }
  1510. if (index != hsize) {
  1511. gfs2_consist_inode(dip);
  1512. error = -EIO;
  1513. }
  1514. out:
  1515. kfree(lp);
  1516. return error;
  1517. }
  1518. /**
  1519. * leaf_dealloc - Deallocate a directory leaf
  1520. * @dip: the directory
  1521. * @index: the hash table offset in the directory
  1522. * @len: the number of pointers to this leaf
  1523. * @leaf_no: the leaf number
  1524. * @data: not used
  1525. *
  1526. * Returns: errno
  1527. */
  1528. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1529. u64 leaf_no, void *data)
  1530. {
  1531. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1532. struct gfs2_leaf *tmp_leaf;
  1533. struct gfs2_rgrp_list rlist;
  1534. struct buffer_head *bh, *dibh;
  1535. u64 blk, nblk;
  1536. unsigned int rg_blocks = 0, l_blocks = 0;
  1537. char *ht;
  1538. unsigned int x, size = len * sizeof(u64);
  1539. int error;
  1540. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1541. ht = kzalloc(size, GFP_KERNEL);
  1542. if (!ht)
  1543. return -ENOMEM;
  1544. gfs2_alloc_get(dip);
  1545. error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1546. if (error)
  1547. goto out;
  1548. error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
  1549. if (error)
  1550. goto out_qs;
  1551. /* Count the number of leaves */
  1552. for (blk = leaf_no; blk; blk = nblk) {
  1553. error = get_leaf(dip, blk, &bh);
  1554. if (error)
  1555. goto out_rlist;
  1556. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1557. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1558. brelse(bh);
  1559. gfs2_rlist_add(sdp, &rlist, blk);
  1560. l_blocks++;
  1561. }
  1562. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  1563. for (x = 0; x < rlist.rl_rgrps; x++) {
  1564. struct gfs2_rgrpd *rgd;
  1565. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1566. rg_blocks += rgd->rd_ri.ri_length;
  1567. }
  1568. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1569. if (error)
  1570. goto out_rlist;
  1571. error = gfs2_trans_begin(sdp,
  1572. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1573. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1574. if (error)
  1575. goto out_rg_gunlock;
  1576. for (blk = leaf_no; blk; blk = nblk) {
  1577. error = get_leaf(dip, blk, &bh);
  1578. if (error)
  1579. goto out_end_trans;
  1580. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1581. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1582. brelse(bh);
  1583. gfs2_free_meta(dip, blk, 1);
  1584. if (!dip->i_di.di_blocks)
  1585. gfs2_consist_inode(dip);
  1586. dip->i_di.di_blocks--;
  1587. }
  1588. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1589. if (error != size) {
  1590. if (error >= 0)
  1591. error = -EIO;
  1592. goto out_end_trans;
  1593. }
  1594. error = gfs2_meta_inode_buffer(dip, &dibh);
  1595. if (error)
  1596. goto out_end_trans;
  1597. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  1598. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  1599. brelse(dibh);
  1600. out_end_trans:
  1601. gfs2_trans_end(sdp);
  1602. out_rg_gunlock:
  1603. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1604. out_rlist:
  1605. gfs2_rlist_free(&rlist);
  1606. gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
  1607. out_qs:
  1608. gfs2_quota_unhold(dip);
  1609. out:
  1610. gfs2_alloc_put(dip);
  1611. kfree(ht);
  1612. return error;
  1613. }
  1614. /**
  1615. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1616. * @dip: the directory
  1617. *
  1618. * Dealloc all on-disk directory leaves to FREEMETA state
  1619. * Change on-disk inode type to "regular file"
  1620. *
  1621. * Returns: errno
  1622. */
  1623. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1624. {
  1625. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1626. struct buffer_head *bh;
  1627. int error;
  1628. /* Dealloc on-disk leaves to FREEMETA state */
  1629. error = foreach_leaf(dip, leaf_dealloc, NULL);
  1630. if (error)
  1631. return error;
  1632. /* Make this a regular file in case we crash.
  1633. (We don't want to free these blocks a second time.) */
  1634. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1635. if (error)
  1636. return error;
  1637. error = gfs2_meta_inode_buffer(dip, &bh);
  1638. if (!error) {
  1639. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1640. ((struct gfs2_dinode *)bh->b_data)->di_mode =
  1641. cpu_to_be32(S_IFREG);
  1642. brelse(bh);
  1643. }
  1644. gfs2_trans_end(sdp);
  1645. return error;
  1646. }
  1647. /**
  1648. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1649. * @ip: the file being written to
  1650. * @filname: the filename that's going to be added
  1651. *
  1652. * Returns: 1 if alloc required, 0 if not, -ve on error
  1653. */
  1654. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
  1655. {
  1656. struct gfs2_dirent *dent;
  1657. struct buffer_head *bh;
  1658. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1659. if (!dent) {
  1660. return 1;
  1661. }
  1662. if (IS_ERR(dent))
  1663. return PTR_ERR(dent);
  1664. brelse(bh);
  1665. return 0;
  1666. }