dir.c 47 KB

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