dir.c 49 KB

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