dir.c 46 KB

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