dir.c 47 KB

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