dir.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  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. if (is_vmalloc_addr(hc))
  311. vfree(hc);
  312. else
  313. kfree(hc);
  314. return ERR_PTR(ret);
  315. }
  316. spin_lock(&inode->i_lock);
  317. if (ip->i_hash_cache) {
  318. if (is_vmalloc_addr(hc))
  319. vfree(hc);
  320. else
  321. kfree(hc);
  322. } else {
  323. ip->i_hash_cache = hc;
  324. }
  325. spin_unlock(&inode->i_lock);
  326. return ip->i_hash_cache;
  327. }
  328. /**
  329. * gfs2_dir_hash_inval - Invalidate dir hash
  330. * @ip: The directory inode
  331. *
  332. * Must be called with an exclusive glock, or during glock invalidation.
  333. */
  334. void gfs2_dir_hash_inval(struct gfs2_inode *ip)
  335. {
  336. __be64 *hc = ip->i_hash_cache;
  337. ip->i_hash_cache = NULL;
  338. if (is_vmalloc_addr(hc))
  339. vfree(hc);
  340. else
  341. kfree(hc);
  342. }
  343. static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
  344. {
  345. return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
  346. }
  347. static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
  348. const struct qstr *name, int ret)
  349. {
  350. if (!gfs2_dirent_sentinel(dent) &&
  351. be32_to_cpu(dent->de_hash) == name->hash &&
  352. be16_to_cpu(dent->de_name_len) == name->len &&
  353. memcmp(dent+1, name->name, name->len) == 0)
  354. return ret;
  355. return 0;
  356. }
  357. static int gfs2_dirent_find(const struct gfs2_dirent *dent,
  358. const struct qstr *name,
  359. void *opaque)
  360. {
  361. return __gfs2_dirent_find(dent, name, 1);
  362. }
  363. static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
  364. const struct qstr *name,
  365. void *opaque)
  366. {
  367. return __gfs2_dirent_find(dent, name, 2);
  368. }
  369. /*
  370. * name->name holds ptr to start of block.
  371. * name->len holds size of block.
  372. */
  373. static int gfs2_dirent_last(const struct gfs2_dirent *dent,
  374. const struct qstr *name,
  375. void *opaque)
  376. {
  377. const char *start = name->name;
  378. const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
  379. if (name->len == (end - start))
  380. return 1;
  381. return 0;
  382. }
  383. static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
  384. const struct qstr *name,
  385. void *opaque)
  386. {
  387. unsigned required = GFS2_DIRENT_SIZE(name->len);
  388. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  389. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  390. if (gfs2_dirent_sentinel(dent))
  391. actual = 0;
  392. if (totlen - actual >= required)
  393. return 1;
  394. return 0;
  395. }
  396. struct dirent_gather {
  397. const struct gfs2_dirent **pdent;
  398. unsigned offset;
  399. };
  400. static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
  401. const struct qstr *name,
  402. void *opaque)
  403. {
  404. struct dirent_gather *g = opaque;
  405. if (!gfs2_dirent_sentinel(dent)) {
  406. g->pdent[g->offset++] = dent;
  407. }
  408. return 0;
  409. }
  410. /*
  411. * Other possible things to check:
  412. * - Inode located within filesystem size (and on valid block)
  413. * - Valid directory entry type
  414. * Not sure how heavy-weight we want to make this... could also check
  415. * hash is correct for example, but that would take a lot of extra time.
  416. * For now the most important thing is to check that the various sizes
  417. * are correct.
  418. */
  419. static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
  420. unsigned int size, unsigned int len, int first)
  421. {
  422. const char *msg = "gfs2_dirent too small";
  423. if (unlikely(size < sizeof(struct gfs2_dirent)))
  424. goto error;
  425. msg = "gfs2_dirent misaligned";
  426. if (unlikely(offset & 0x7))
  427. goto error;
  428. msg = "gfs2_dirent points beyond end of block";
  429. if (unlikely(offset + size > len))
  430. goto error;
  431. msg = "zero inode number";
  432. if (unlikely(!first && gfs2_dirent_sentinel(dent)))
  433. goto error;
  434. msg = "name length is greater than space in dirent";
  435. if (!gfs2_dirent_sentinel(dent) &&
  436. unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
  437. size))
  438. goto error;
  439. return 0;
  440. error:
  441. pr_warn("%s: %s (%s)\n",
  442. __func__, msg, first ? "first in block" : "not first in block");
  443. return -EIO;
  444. }
  445. static int gfs2_dirent_offset(const void *buf)
  446. {
  447. const struct gfs2_meta_header *h = buf;
  448. int offset;
  449. BUG_ON(buf == NULL);
  450. switch(be32_to_cpu(h->mh_type)) {
  451. case GFS2_METATYPE_LF:
  452. offset = sizeof(struct gfs2_leaf);
  453. break;
  454. case GFS2_METATYPE_DI:
  455. offset = sizeof(struct gfs2_dinode);
  456. break;
  457. default:
  458. goto wrong_type;
  459. }
  460. return offset;
  461. wrong_type:
  462. pr_warn("%s: wrong block type %u\n", __func__, 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. /* pr_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. struct timespec tv = CURRENT_TIME;
  722. error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  723. if (error)
  724. return NULL;
  725. bh = gfs2_meta_new(ip->i_gl, bn);
  726. if (!bh)
  727. return NULL;
  728. gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
  729. gfs2_trans_add_meta(ip->i_gl, bh);
  730. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  731. leaf = (struct gfs2_leaf *)bh->b_data;
  732. leaf->lf_depth = cpu_to_be16(depth);
  733. leaf->lf_entries = 0;
  734. leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
  735. leaf->lf_next = 0;
  736. leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
  737. leaf->lf_dist = cpu_to_be32(1);
  738. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  739. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  740. memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
  741. dent = (struct gfs2_dirent *)(leaf+1);
  742. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  743. *pbh = bh;
  744. return leaf;
  745. }
  746. /**
  747. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  748. * @dip: The GFS2 inode
  749. *
  750. * Returns: 0 on success, error code otherwise
  751. */
  752. static int dir_make_exhash(struct inode *inode)
  753. {
  754. struct gfs2_inode *dip = GFS2_I(inode);
  755. struct gfs2_sbd *sdp = GFS2_SB(inode);
  756. struct gfs2_dirent *dent;
  757. struct qstr args;
  758. struct buffer_head *bh, *dibh;
  759. struct gfs2_leaf *leaf;
  760. int y;
  761. u32 x;
  762. __be64 *lp;
  763. u64 bn;
  764. int error;
  765. error = gfs2_meta_inode_buffer(dip, &dibh);
  766. if (error)
  767. return error;
  768. /* Turn over a new leaf */
  769. leaf = new_leaf(inode, &bh, 0);
  770. if (!leaf)
  771. return -ENOSPC;
  772. bn = bh->b_blocknr;
  773. gfs2_assert(sdp, dip->i_entries < (1 << 16));
  774. leaf->lf_entries = cpu_to_be16(dip->i_entries);
  775. /* Copy dirents */
  776. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  777. sizeof(struct gfs2_dinode));
  778. /* Find last entry */
  779. x = 0;
  780. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  781. sizeof(struct gfs2_leaf);
  782. args.name = bh->b_data;
  783. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  784. gfs2_dirent_last, &args, NULL);
  785. if (!dent) {
  786. brelse(bh);
  787. brelse(dibh);
  788. return -EIO;
  789. }
  790. if (IS_ERR(dent)) {
  791. brelse(bh);
  792. brelse(dibh);
  793. return PTR_ERR(dent);
  794. }
  795. /* Adjust the last dirent's record length
  796. (Remember that dent still points to the last entry.) */
  797. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  798. sizeof(struct gfs2_dinode) -
  799. sizeof(struct gfs2_leaf));
  800. brelse(bh);
  801. /* We're done with the new leaf block, now setup the new
  802. hash table. */
  803. gfs2_trans_add_meta(dip->i_gl, dibh);
  804. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  805. lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  806. for (x = sdp->sd_hash_ptrs; x--; lp++)
  807. *lp = cpu_to_be64(bn);
  808. i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
  809. gfs2_add_inode_blocks(&dip->i_inode, 1);
  810. dip->i_diskflags |= GFS2_DIF_EXHASH;
  811. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  812. dip->i_depth = y;
  813. gfs2_dinode_out(dip, dibh->b_data);
  814. brelse(dibh);
  815. return 0;
  816. }
  817. /**
  818. * dir_split_leaf - Split a leaf block into two
  819. * @dip: The GFS2 inode
  820. * @index:
  821. * @leaf_no:
  822. *
  823. * Returns: 0 on success, error code on failure
  824. */
  825. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  826. {
  827. struct gfs2_inode *dip = GFS2_I(inode);
  828. struct buffer_head *nbh, *obh, *dibh;
  829. struct gfs2_leaf *nleaf, *oleaf;
  830. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  831. u32 start, len, half_len, divider;
  832. u64 bn, leaf_no;
  833. __be64 *lp;
  834. u32 index;
  835. int x, moved = 0;
  836. int error;
  837. index = name->hash >> (32 - dip->i_depth);
  838. error = get_leaf_nr(dip, index, &leaf_no);
  839. if (error)
  840. return error;
  841. /* Get the old leaf block */
  842. error = get_leaf(dip, leaf_no, &obh);
  843. if (error)
  844. return error;
  845. oleaf = (struct gfs2_leaf *)obh->b_data;
  846. if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
  847. brelse(obh);
  848. return 1; /* can't split */
  849. }
  850. gfs2_trans_add_meta(dip->i_gl, obh);
  851. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  852. if (!nleaf) {
  853. brelse(obh);
  854. return -ENOSPC;
  855. }
  856. bn = nbh->b_blocknr;
  857. /* Compute the start and len of leaf pointers in the hash table. */
  858. len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
  859. half_len = len >> 1;
  860. if (!half_len) {
  861. pr_warn("i_depth %u lf_depth %u index %u\n",
  862. dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
  863. gfs2_consist_inode(dip);
  864. error = -EIO;
  865. goto fail_brelse;
  866. }
  867. start = (index & ~(len - 1));
  868. /* Change the pointers.
  869. Don't bother distinguishing stuffed from non-stuffed.
  870. This code is complicated enough already. */
  871. lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
  872. if (!lp) {
  873. error = -ENOMEM;
  874. goto fail_brelse;
  875. }
  876. /* Change the pointers */
  877. for (x = 0; x < half_len; x++)
  878. lp[x] = cpu_to_be64(bn);
  879. gfs2_dir_hash_inval(dip);
  880. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  881. half_len * sizeof(u64));
  882. if (error != half_len * sizeof(u64)) {
  883. if (error >= 0)
  884. error = -EIO;
  885. goto fail_lpfree;
  886. }
  887. kfree(lp);
  888. /* Compute the divider */
  889. divider = (start + half_len) << (32 - dip->i_depth);
  890. /* Copy the entries */
  891. dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
  892. do {
  893. next = dent;
  894. if (dirent_next(dip, obh, &next))
  895. next = NULL;
  896. if (!gfs2_dirent_sentinel(dent) &&
  897. be32_to_cpu(dent->de_hash) < divider) {
  898. struct qstr str;
  899. str.name = (char*)(dent+1);
  900. str.len = be16_to_cpu(dent->de_name_len);
  901. str.hash = be32_to_cpu(dent->de_hash);
  902. new = gfs2_dirent_alloc(inode, nbh, &str);
  903. if (IS_ERR(new)) {
  904. error = PTR_ERR(new);
  905. break;
  906. }
  907. new->de_inum = dent->de_inum; /* No endian worries */
  908. new->de_type = dent->de_type; /* No endian worries */
  909. be16_add_cpu(&nleaf->lf_entries, 1);
  910. dirent_del(dip, obh, prev, dent);
  911. if (!oleaf->lf_entries)
  912. gfs2_consist_inode(dip);
  913. be16_add_cpu(&oleaf->lf_entries, -1);
  914. if (!prev)
  915. prev = dent;
  916. moved = 1;
  917. } else {
  918. prev = dent;
  919. }
  920. dent = next;
  921. } while (dent);
  922. oleaf->lf_depth = nleaf->lf_depth;
  923. error = gfs2_meta_inode_buffer(dip, &dibh);
  924. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  925. gfs2_trans_add_meta(dip->i_gl, dibh);
  926. gfs2_add_inode_blocks(&dip->i_inode, 1);
  927. gfs2_dinode_out(dip, dibh->b_data);
  928. brelse(dibh);
  929. }
  930. brelse(obh);
  931. brelse(nbh);
  932. return error;
  933. fail_lpfree:
  934. kfree(lp);
  935. fail_brelse:
  936. brelse(obh);
  937. brelse(nbh);
  938. return error;
  939. }
  940. /**
  941. * dir_double_exhash - Double size of ExHash table
  942. * @dip: The GFS2 dinode
  943. *
  944. * Returns: 0 on success, error code on failure
  945. */
  946. static int dir_double_exhash(struct gfs2_inode *dip)
  947. {
  948. struct buffer_head *dibh;
  949. u32 hsize;
  950. u32 hsize_bytes;
  951. __be64 *hc;
  952. __be64 *hc2, *h;
  953. int x;
  954. int error = 0;
  955. hsize = 1 << dip->i_depth;
  956. hsize_bytes = hsize * sizeof(__be64);
  957. hc = gfs2_dir_get_hash_table(dip);
  958. if (IS_ERR(hc))
  959. return PTR_ERR(hc);
  960. hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN);
  961. if (hc2 == NULL)
  962. hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
  963. if (!hc2)
  964. return -ENOMEM;
  965. h = hc2;
  966. error = gfs2_meta_inode_buffer(dip, &dibh);
  967. if (error)
  968. goto out_kfree;
  969. for (x = 0; x < hsize; x++) {
  970. *h++ = *hc;
  971. *h++ = *hc;
  972. hc++;
  973. }
  974. error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
  975. if (error != (hsize_bytes * 2))
  976. goto fail;
  977. gfs2_dir_hash_inval(dip);
  978. dip->i_hash_cache = hc2;
  979. dip->i_depth++;
  980. gfs2_dinode_out(dip, dibh->b_data);
  981. brelse(dibh);
  982. return 0;
  983. fail:
  984. /* Replace original hash table & size */
  985. gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
  986. i_size_write(&dip->i_inode, hsize_bytes);
  987. gfs2_dinode_out(dip, dibh->b_data);
  988. brelse(dibh);
  989. out_kfree:
  990. if (is_vmalloc_addr(hc2))
  991. vfree(hc2);
  992. else
  993. kfree(hc2);
  994. return error;
  995. }
  996. /**
  997. * compare_dents - compare directory entries by hash value
  998. * @a: first dent
  999. * @b: second dent
  1000. *
  1001. * When comparing the hash entries of @a to @b:
  1002. * gt: returns 1
  1003. * lt: returns -1
  1004. * eq: returns 0
  1005. */
  1006. static int compare_dents(const void *a, const void *b)
  1007. {
  1008. const struct gfs2_dirent *dent_a, *dent_b;
  1009. u32 hash_a, hash_b;
  1010. int ret = 0;
  1011. dent_a = *(const struct gfs2_dirent **)a;
  1012. hash_a = be32_to_cpu(dent_a->de_hash);
  1013. dent_b = *(const struct gfs2_dirent **)b;
  1014. hash_b = be32_to_cpu(dent_b->de_hash);
  1015. if (hash_a > hash_b)
  1016. ret = 1;
  1017. else if (hash_a < hash_b)
  1018. ret = -1;
  1019. else {
  1020. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  1021. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  1022. if (len_a > len_b)
  1023. ret = 1;
  1024. else if (len_a < len_b)
  1025. ret = -1;
  1026. else
  1027. ret = memcmp(dent_a + 1, dent_b + 1, len_a);
  1028. }
  1029. return ret;
  1030. }
  1031. /**
  1032. * do_filldir_main - read out directory entries
  1033. * @dip: The GFS2 inode
  1034. * @ctx: what to feed the entries to
  1035. * @darr: an array of struct gfs2_dirent pointers to read
  1036. * @entries: the number of entries in darr
  1037. * @copied: pointer to int that's non-zero if a entry has been copied out
  1038. *
  1039. * Jump through some hoops to make sure that if there are hash collsions,
  1040. * they are read out at the beginning of a buffer. We want to minimize
  1041. * the possibility that they will fall into different readdir buffers or
  1042. * that someone will want to seek to that location.
  1043. *
  1044. * Returns: errno, >0 if the actor tells you to stop
  1045. */
  1046. static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
  1047. const struct gfs2_dirent **darr, u32 entries,
  1048. int *copied)
  1049. {
  1050. const struct gfs2_dirent *dent, *dent_next;
  1051. u64 off, off_next;
  1052. unsigned int x, y;
  1053. int run = 0;
  1054. sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
  1055. dent_next = darr[0];
  1056. off_next = be32_to_cpu(dent_next->de_hash);
  1057. off_next = gfs2_disk_hash2offset(off_next);
  1058. for (x = 0, y = 1; x < entries; x++, y++) {
  1059. dent = dent_next;
  1060. off = off_next;
  1061. if (y < entries) {
  1062. dent_next = darr[y];
  1063. off_next = be32_to_cpu(dent_next->de_hash);
  1064. off_next = gfs2_disk_hash2offset(off_next);
  1065. if (off < ctx->pos)
  1066. continue;
  1067. ctx->pos = off;
  1068. if (off_next == off) {
  1069. if (*copied && !run)
  1070. return 1;
  1071. run = 1;
  1072. } else
  1073. run = 0;
  1074. } else {
  1075. if (off < ctx->pos)
  1076. continue;
  1077. ctx->pos = off;
  1078. }
  1079. if (!dir_emit(ctx, (const char *)(dent + 1),
  1080. be16_to_cpu(dent->de_name_len),
  1081. be64_to_cpu(dent->de_inum.no_addr),
  1082. be16_to_cpu(dent->de_type)))
  1083. return 1;
  1084. *copied = 1;
  1085. }
  1086. /* Increment the ctx->pos by one, so the next time we come into the
  1087. do_filldir fxn, we get the next entry instead of the last one in the
  1088. current leaf */
  1089. ctx->pos++;
  1090. return 0;
  1091. }
  1092. static void *gfs2_alloc_sort_buffer(unsigned size)
  1093. {
  1094. void *ptr = NULL;
  1095. if (size < KMALLOC_MAX_SIZE)
  1096. ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
  1097. if (!ptr)
  1098. ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
  1099. return ptr;
  1100. }
  1101. static void gfs2_free_sort_buffer(void *ptr)
  1102. {
  1103. if (is_vmalloc_addr(ptr))
  1104. vfree(ptr);
  1105. else
  1106. kfree(ptr);
  1107. }
  1108. static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
  1109. int *copied, unsigned *depth,
  1110. u64 leaf_no)
  1111. {
  1112. struct gfs2_inode *ip = GFS2_I(inode);
  1113. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1114. struct buffer_head *bh;
  1115. struct gfs2_leaf *lf;
  1116. unsigned entries = 0, entries2 = 0;
  1117. unsigned leaves = 0;
  1118. const struct gfs2_dirent **darr, *dent;
  1119. struct dirent_gather g;
  1120. struct buffer_head **larr;
  1121. int leaf = 0;
  1122. int error, i;
  1123. u64 lfn = leaf_no;
  1124. do {
  1125. error = get_leaf(ip, lfn, &bh);
  1126. if (error)
  1127. goto out;
  1128. lf = (struct gfs2_leaf *)bh->b_data;
  1129. if (leaves == 0)
  1130. *depth = be16_to_cpu(lf->lf_depth);
  1131. entries += be16_to_cpu(lf->lf_entries);
  1132. leaves++;
  1133. lfn = be64_to_cpu(lf->lf_next);
  1134. brelse(bh);
  1135. } while(lfn);
  1136. if (!entries)
  1137. return 0;
  1138. error = -ENOMEM;
  1139. /*
  1140. * The extra 99 entries are not normally used, but are a buffer
  1141. * zone in case the number of entries in the leaf is corrupt.
  1142. * 99 is the maximum number of entries that can fit in a single
  1143. * leaf block.
  1144. */
  1145. larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
  1146. if (!larr)
  1147. goto out;
  1148. darr = (const struct gfs2_dirent **)(larr + leaves);
  1149. g.pdent = darr;
  1150. g.offset = 0;
  1151. lfn = leaf_no;
  1152. do {
  1153. error = get_leaf(ip, lfn, &bh);
  1154. if (error)
  1155. goto out_free;
  1156. lf = (struct gfs2_leaf *)bh->b_data;
  1157. lfn = be64_to_cpu(lf->lf_next);
  1158. if (lf->lf_entries) {
  1159. entries2 += be16_to_cpu(lf->lf_entries);
  1160. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1161. gfs2_dirent_gather, NULL, &g);
  1162. error = PTR_ERR(dent);
  1163. if (IS_ERR(dent))
  1164. goto out_free;
  1165. if (entries2 != g.offset) {
  1166. fs_warn(sdp, "Number of entries corrupt in dir "
  1167. "leaf %llu, entries2 (%u) != "
  1168. "g.offset (%u)\n",
  1169. (unsigned long long)bh->b_blocknr,
  1170. entries2, g.offset);
  1171. error = -EIO;
  1172. goto out_free;
  1173. }
  1174. error = 0;
  1175. larr[leaf++] = bh;
  1176. } else {
  1177. brelse(bh);
  1178. }
  1179. } while(lfn);
  1180. BUG_ON(entries2 != entries);
  1181. error = do_filldir_main(ip, ctx, darr, entries, copied);
  1182. out_free:
  1183. for(i = 0; i < leaf; i++)
  1184. brelse(larr[i]);
  1185. gfs2_free_sort_buffer(larr);
  1186. out:
  1187. return error;
  1188. }
  1189. /**
  1190. * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
  1191. *
  1192. * Note: we can't calculate each index like dir_e_read can because we don't
  1193. * have the leaf, and therefore we don't have the depth, and therefore we
  1194. * don't have the length. So we have to just read enough ahead to make up
  1195. * for the loss of information.
  1196. */
  1197. static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
  1198. struct file_ra_state *f_ra)
  1199. {
  1200. struct gfs2_inode *ip = GFS2_I(inode);
  1201. struct gfs2_glock *gl = ip->i_gl;
  1202. struct buffer_head *bh;
  1203. u64 blocknr = 0, last;
  1204. unsigned count;
  1205. /* First check if we've already read-ahead for the whole range. */
  1206. if (index + MAX_RA_BLOCKS < f_ra->start)
  1207. return;
  1208. f_ra->start = max((pgoff_t)index, f_ra->start);
  1209. for (count = 0; count < MAX_RA_BLOCKS; count++) {
  1210. if (f_ra->start >= hsize) /* if exceeded the hash table */
  1211. break;
  1212. last = blocknr;
  1213. blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
  1214. f_ra->start++;
  1215. if (blocknr == last)
  1216. continue;
  1217. bh = gfs2_getbuf(gl, blocknr, 1);
  1218. if (trylock_buffer(bh)) {
  1219. if (buffer_uptodate(bh)) {
  1220. unlock_buffer(bh);
  1221. brelse(bh);
  1222. continue;
  1223. }
  1224. bh->b_end_io = end_buffer_read_sync;
  1225. submit_bh(READA | REQ_META, bh);
  1226. continue;
  1227. }
  1228. brelse(bh);
  1229. }
  1230. }
  1231. /**
  1232. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1233. * @dip: dinode pointer
  1234. * @ctx: actor to feed the entries to
  1235. *
  1236. * Returns: errno
  1237. */
  1238. static int dir_e_read(struct inode *inode, struct dir_context *ctx,
  1239. struct file_ra_state *f_ra)
  1240. {
  1241. struct gfs2_inode *dip = GFS2_I(inode);
  1242. u32 hsize, len = 0;
  1243. u32 hash, index;
  1244. __be64 *lp;
  1245. int copied = 0;
  1246. int error = 0;
  1247. unsigned depth = 0;
  1248. hsize = 1 << dip->i_depth;
  1249. hash = gfs2_dir_offset2hash(ctx->pos);
  1250. index = hash >> (32 - dip->i_depth);
  1251. if (dip->i_hash_cache == NULL)
  1252. f_ra->start = 0;
  1253. lp = gfs2_dir_get_hash_table(dip);
  1254. if (IS_ERR(lp))
  1255. return PTR_ERR(lp);
  1256. gfs2_dir_readahead(inode, hsize, index, f_ra);
  1257. while (index < hsize) {
  1258. error = gfs2_dir_read_leaf(inode, ctx,
  1259. &copied, &depth,
  1260. be64_to_cpu(lp[index]));
  1261. if (error)
  1262. break;
  1263. len = 1 << (dip->i_depth - depth);
  1264. index = (index & ~(len - 1)) + len;
  1265. }
  1266. if (error > 0)
  1267. error = 0;
  1268. return error;
  1269. }
  1270. int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
  1271. struct file_ra_state *f_ra)
  1272. {
  1273. struct gfs2_inode *dip = GFS2_I(inode);
  1274. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1275. struct dirent_gather g;
  1276. const struct gfs2_dirent **darr, *dent;
  1277. struct buffer_head *dibh;
  1278. int copied = 0;
  1279. int error;
  1280. if (!dip->i_entries)
  1281. return 0;
  1282. if (dip->i_diskflags & GFS2_DIF_EXHASH)
  1283. return dir_e_read(inode, ctx, f_ra);
  1284. if (!gfs2_is_stuffed(dip)) {
  1285. gfs2_consist_inode(dip);
  1286. return -EIO;
  1287. }
  1288. error = gfs2_meta_inode_buffer(dip, &dibh);
  1289. if (error)
  1290. return error;
  1291. error = -ENOMEM;
  1292. /* 96 is max number of dirents which can be stuffed into an inode */
  1293. darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
  1294. if (darr) {
  1295. g.pdent = darr;
  1296. g.offset = 0;
  1297. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1298. gfs2_dirent_gather, NULL, &g);
  1299. if (IS_ERR(dent)) {
  1300. error = PTR_ERR(dent);
  1301. goto out;
  1302. }
  1303. if (dip->i_entries != g.offset) {
  1304. fs_warn(sdp, "Number of entries corrupt in dir %llu, "
  1305. "ip->i_entries (%u) != g.offset (%u)\n",
  1306. (unsigned long long)dip->i_no_addr,
  1307. dip->i_entries,
  1308. g.offset);
  1309. error = -EIO;
  1310. goto out;
  1311. }
  1312. error = do_filldir_main(dip, ctx, darr,
  1313. dip->i_entries, &copied);
  1314. out:
  1315. kfree(darr);
  1316. }
  1317. if (error > 0)
  1318. error = 0;
  1319. brelse(dibh);
  1320. return error;
  1321. }
  1322. /**
  1323. * gfs2_dir_search - Search a directory
  1324. * @dip: The GFS2 dir inode
  1325. * @name: The name we are looking up
  1326. * @fail_on_exist: Fail if the name exists rather than looking it up
  1327. *
  1328. * This routine searches a directory for a file or another directory.
  1329. * Assumes a glock is held on dip.
  1330. *
  1331. * Returns: errno
  1332. */
  1333. struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
  1334. bool fail_on_exist)
  1335. {
  1336. struct buffer_head *bh;
  1337. struct gfs2_dirent *dent;
  1338. u64 addr, formal_ino;
  1339. u16 dtype;
  1340. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1341. if (dent) {
  1342. if (IS_ERR(dent))
  1343. return ERR_CAST(dent);
  1344. dtype = be16_to_cpu(dent->de_type);
  1345. addr = be64_to_cpu(dent->de_inum.no_addr);
  1346. formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
  1347. brelse(bh);
  1348. if (fail_on_exist)
  1349. return ERR_PTR(-EEXIST);
  1350. return gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, 0);
  1351. }
  1352. return ERR_PTR(-ENOENT);
  1353. }
  1354. int gfs2_dir_check(struct inode *dir, const struct qstr *name,
  1355. const struct gfs2_inode *ip)
  1356. {
  1357. struct buffer_head *bh;
  1358. struct gfs2_dirent *dent;
  1359. int ret = -ENOENT;
  1360. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1361. if (dent) {
  1362. if (IS_ERR(dent))
  1363. return PTR_ERR(dent);
  1364. if (ip) {
  1365. if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
  1366. goto out;
  1367. if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
  1368. ip->i_no_formal_ino)
  1369. goto out;
  1370. if (unlikely(IF2DT(ip->i_inode.i_mode) !=
  1371. be16_to_cpu(dent->de_type))) {
  1372. gfs2_consist_inode(GFS2_I(dir));
  1373. ret = -EIO;
  1374. goto out;
  1375. }
  1376. }
  1377. ret = 0;
  1378. out:
  1379. brelse(bh);
  1380. }
  1381. return ret;
  1382. }
  1383. /**
  1384. * dir_new_leaf - Add a new leaf onto hash chain
  1385. * @inode: The directory
  1386. * @name: The name we are adding
  1387. *
  1388. * This adds a new dir leaf onto an existing leaf when there is not
  1389. * enough space to add a new dir entry. This is a last resort after
  1390. * we've expanded the hash table to max size and also split existing
  1391. * leaf blocks, so it will only occur for very large directories.
  1392. *
  1393. * The dist parameter is set to 1 for leaf blocks directly attached
  1394. * to the hash table, 2 for one layer of indirection, 3 for two layers
  1395. * etc. We are thus able to tell the difference between an old leaf
  1396. * with dist set to zero (i.e. "don't know") and a new one where we
  1397. * set this information for debug/fsck purposes.
  1398. *
  1399. * Returns: 0 on success, or -ve on error
  1400. */
  1401. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1402. {
  1403. struct buffer_head *bh, *obh;
  1404. struct gfs2_inode *ip = GFS2_I(inode);
  1405. struct gfs2_leaf *leaf, *oleaf;
  1406. u32 dist = 1;
  1407. int error;
  1408. u32 index;
  1409. u64 bn;
  1410. index = name->hash >> (32 - ip->i_depth);
  1411. error = get_first_leaf(ip, index, &obh);
  1412. if (error)
  1413. return error;
  1414. do {
  1415. dist++;
  1416. oleaf = (struct gfs2_leaf *)obh->b_data;
  1417. bn = be64_to_cpu(oleaf->lf_next);
  1418. if (!bn)
  1419. break;
  1420. brelse(obh);
  1421. error = get_leaf(ip, bn, &obh);
  1422. if (error)
  1423. return error;
  1424. } while(1);
  1425. gfs2_trans_add_meta(ip->i_gl, obh);
  1426. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1427. if (!leaf) {
  1428. brelse(obh);
  1429. return -ENOSPC;
  1430. }
  1431. leaf->lf_dist = cpu_to_be32(dist);
  1432. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1433. brelse(bh);
  1434. brelse(obh);
  1435. error = gfs2_meta_inode_buffer(ip, &bh);
  1436. if (error)
  1437. return error;
  1438. gfs2_trans_add_meta(ip->i_gl, bh);
  1439. gfs2_add_inode_blocks(&ip->i_inode, 1);
  1440. gfs2_dinode_out(ip, bh->b_data);
  1441. brelse(bh);
  1442. return 0;
  1443. }
  1444. static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
  1445. {
  1446. u64 where = ip->i_no_addr + 1;
  1447. if (ip->i_eattr == where)
  1448. return 1;
  1449. return 0;
  1450. }
  1451. /**
  1452. * gfs2_dir_add - Add new filename into directory
  1453. * @inode: The directory inode
  1454. * @name: The new name
  1455. * @nip: The GFS2 inode to be linked in to the directory
  1456. * @da: The directory addition info
  1457. *
  1458. * If the call to gfs2_diradd_alloc_required resulted in there being
  1459. * no need to allocate any new directory blocks, then it will contain
  1460. * a pointer to the directory entry and the bh in which it resides. We
  1461. * can use that without having to repeat the search. If there was no
  1462. * free space, then we must now create more space.
  1463. *
  1464. * Returns: 0 on success, error code on failure
  1465. */
  1466. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1467. const struct gfs2_inode *nip, struct gfs2_diradd *da)
  1468. {
  1469. struct gfs2_inode *ip = GFS2_I(inode);
  1470. struct buffer_head *bh = da->bh;
  1471. struct gfs2_dirent *dent = da->dent;
  1472. struct timespec tv;
  1473. struct gfs2_leaf *leaf;
  1474. int error;
  1475. while(1) {
  1476. if (da->bh == NULL) {
  1477. dent = gfs2_dirent_search(inode, name,
  1478. gfs2_dirent_find_space, &bh);
  1479. }
  1480. if (dent) {
  1481. if (IS_ERR(dent))
  1482. return PTR_ERR(dent);
  1483. dent = gfs2_init_dirent(inode, dent, name, bh);
  1484. gfs2_inum_out(nip, dent);
  1485. dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
  1486. dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
  1487. tv = CURRENT_TIME;
  1488. if (ip->i_diskflags & GFS2_DIF_EXHASH) {
  1489. leaf = (struct gfs2_leaf *)bh->b_data;
  1490. be16_add_cpu(&leaf->lf_entries, 1);
  1491. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  1492. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  1493. }
  1494. da->dent = NULL;
  1495. da->bh = NULL;
  1496. brelse(bh);
  1497. ip->i_entries++;
  1498. ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
  1499. if (S_ISDIR(nip->i_inode.i_mode))
  1500. inc_nlink(&ip->i_inode);
  1501. mark_inode_dirty(inode);
  1502. error = 0;
  1503. break;
  1504. }
  1505. if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
  1506. error = dir_make_exhash(inode);
  1507. if (error)
  1508. break;
  1509. continue;
  1510. }
  1511. error = dir_split_leaf(inode, name);
  1512. if (error == 0)
  1513. continue;
  1514. if (error < 0)
  1515. break;
  1516. if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
  1517. error = dir_double_exhash(ip);
  1518. if (error)
  1519. break;
  1520. error = dir_split_leaf(inode, name);
  1521. if (error < 0)
  1522. break;
  1523. if (error == 0)
  1524. continue;
  1525. }
  1526. error = dir_new_leaf(inode, name);
  1527. if (!error)
  1528. continue;
  1529. error = -ENOSPC;
  1530. break;
  1531. }
  1532. return error;
  1533. }
  1534. /**
  1535. * gfs2_dir_del - Delete a directory entry
  1536. * @dip: The GFS2 inode
  1537. * @filename: The filename
  1538. *
  1539. * Returns: 0 on success, error code on failure
  1540. */
  1541. int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
  1542. {
  1543. const struct qstr *name = &dentry->d_name;
  1544. struct gfs2_dirent *dent, *prev = NULL;
  1545. struct buffer_head *bh;
  1546. struct timespec tv = CURRENT_TIME;
  1547. /* Returns _either_ the entry (if its first in block) or the
  1548. previous entry otherwise */
  1549. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1550. if (!dent) {
  1551. gfs2_consist_inode(dip);
  1552. return -EIO;
  1553. }
  1554. if (IS_ERR(dent)) {
  1555. gfs2_consist_inode(dip);
  1556. return PTR_ERR(dent);
  1557. }
  1558. /* If not first in block, adjust pointers accordingly */
  1559. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1560. prev = dent;
  1561. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1562. }
  1563. dirent_del(dip, bh, prev, dent);
  1564. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1565. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1566. u16 entries = be16_to_cpu(leaf->lf_entries);
  1567. if (!entries)
  1568. gfs2_consist_inode(dip);
  1569. leaf->lf_entries = cpu_to_be16(--entries);
  1570. leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
  1571. leaf->lf_sec = cpu_to_be64(tv.tv_sec);
  1572. }
  1573. brelse(bh);
  1574. if (!dip->i_entries)
  1575. gfs2_consist_inode(dip);
  1576. dip->i_entries--;
  1577. dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
  1578. if (S_ISDIR(dentry->d_inode->i_mode))
  1579. drop_nlink(&dip->i_inode);
  1580. mark_inode_dirty(&dip->i_inode);
  1581. return 0;
  1582. }
  1583. /**
  1584. * gfs2_dir_mvino - Change inode number of directory entry
  1585. * @dip: The GFS2 inode
  1586. * @filename:
  1587. * @new_inode:
  1588. *
  1589. * This routine changes the inode number of a directory entry. It's used
  1590. * by rename to change ".." when a directory is moved.
  1591. * Assumes a glock is held on dvp.
  1592. *
  1593. * Returns: errno
  1594. */
  1595. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1596. const struct gfs2_inode *nip, unsigned int new_type)
  1597. {
  1598. struct buffer_head *bh;
  1599. struct gfs2_dirent *dent;
  1600. int error;
  1601. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1602. if (!dent) {
  1603. gfs2_consist_inode(dip);
  1604. return -EIO;
  1605. }
  1606. if (IS_ERR(dent))
  1607. return PTR_ERR(dent);
  1608. gfs2_trans_add_meta(dip->i_gl, bh);
  1609. gfs2_inum_out(nip, dent);
  1610. dent->de_type = cpu_to_be16(new_type);
  1611. if (dip->i_diskflags & GFS2_DIF_EXHASH) {
  1612. brelse(bh);
  1613. error = gfs2_meta_inode_buffer(dip, &bh);
  1614. if (error)
  1615. return error;
  1616. gfs2_trans_add_meta(dip->i_gl, bh);
  1617. }
  1618. dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
  1619. gfs2_dinode_out(dip, bh->b_data);
  1620. brelse(bh);
  1621. return 0;
  1622. }
  1623. /**
  1624. * leaf_dealloc - Deallocate a directory leaf
  1625. * @dip: the directory
  1626. * @index: the hash table offset in the directory
  1627. * @len: the number of pointers to this leaf
  1628. * @leaf_no: the leaf number
  1629. * @leaf_bh: buffer_head for the starting leaf
  1630. * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
  1631. *
  1632. * Returns: errno
  1633. */
  1634. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1635. u64 leaf_no, struct buffer_head *leaf_bh,
  1636. int last_dealloc)
  1637. {
  1638. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1639. struct gfs2_leaf *tmp_leaf;
  1640. struct gfs2_rgrp_list rlist;
  1641. struct buffer_head *bh, *dibh;
  1642. u64 blk, nblk;
  1643. unsigned int rg_blocks = 0, l_blocks = 0;
  1644. char *ht;
  1645. unsigned int x, size = len * sizeof(u64);
  1646. int error;
  1647. error = gfs2_rindex_update(sdp);
  1648. if (error)
  1649. return error;
  1650. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1651. ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
  1652. if (ht == NULL)
  1653. ht = vzalloc(size);
  1654. if (!ht)
  1655. return -ENOMEM;
  1656. error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1657. if (error)
  1658. goto out;
  1659. /* Count the number of leaves */
  1660. bh = leaf_bh;
  1661. for (blk = leaf_no; blk; blk = nblk) {
  1662. if (blk != leaf_no) {
  1663. error = get_leaf(dip, blk, &bh);
  1664. if (error)
  1665. goto out_rlist;
  1666. }
  1667. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1668. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1669. if (blk != leaf_no)
  1670. brelse(bh);
  1671. gfs2_rlist_add(dip, &rlist, blk);
  1672. l_blocks++;
  1673. }
  1674. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  1675. for (x = 0; x < rlist.rl_rgrps; x++) {
  1676. struct gfs2_rgrpd *rgd;
  1677. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1678. rg_blocks += rgd->rd_length;
  1679. }
  1680. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1681. if (error)
  1682. goto out_rlist;
  1683. error = gfs2_trans_begin(sdp,
  1684. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1685. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1686. if (error)
  1687. goto out_rg_gunlock;
  1688. bh = leaf_bh;
  1689. for (blk = leaf_no; blk; blk = nblk) {
  1690. if (blk != leaf_no) {
  1691. error = get_leaf(dip, blk, &bh);
  1692. if (error)
  1693. goto out_end_trans;
  1694. }
  1695. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1696. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1697. if (blk != leaf_no)
  1698. brelse(bh);
  1699. gfs2_free_meta(dip, blk, 1);
  1700. gfs2_add_inode_blocks(&dip->i_inode, -1);
  1701. }
  1702. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1703. if (error != size) {
  1704. if (error >= 0)
  1705. error = -EIO;
  1706. goto out_end_trans;
  1707. }
  1708. error = gfs2_meta_inode_buffer(dip, &dibh);
  1709. if (error)
  1710. goto out_end_trans;
  1711. gfs2_trans_add_meta(dip->i_gl, dibh);
  1712. /* On the last dealloc, make this a regular file in case we crash.
  1713. (We don't want to free these blocks a second time.) */
  1714. if (last_dealloc)
  1715. dip->i_inode.i_mode = S_IFREG;
  1716. gfs2_dinode_out(dip, dibh->b_data);
  1717. brelse(dibh);
  1718. out_end_trans:
  1719. gfs2_trans_end(sdp);
  1720. out_rg_gunlock:
  1721. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1722. out_rlist:
  1723. gfs2_rlist_free(&rlist);
  1724. gfs2_quota_unhold(dip);
  1725. out:
  1726. if (is_vmalloc_addr(ht))
  1727. vfree(ht);
  1728. else
  1729. kfree(ht);
  1730. return error;
  1731. }
  1732. /**
  1733. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1734. * @dip: the directory
  1735. *
  1736. * Dealloc all on-disk directory leaves to FREEMETA state
  1737. * Change on-disk inode type to "regular file"
  1738. *
  1739. * Returns: errno
  1740. */
  1741. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1742. {
  1743. struct buffer_head *bh;
  1744. struct gfs2_leaf *leaf;
  1745. u32 hsize, len;
  1746. u32 index = 0, next_index;
  1747. __be64 *lp;
  1748. u64 leaf_no;
  1749. int error = 0, last;
  1750. hsize = 1 << dip->i_depth;
  1751. lp = gfs2_dir_get_hash_table(dip);
  1752. if (IS_ERR(lp))
  1753. return PTR_ERR(lp);
  1754. while (index < hsize) {
  1755. leaf_no = be64_to_cpu(lp[index]);
  1756. if (leaf_no) {
  1757. error = get_leaf(dip, leaf_no, &bh);
  1758. if (error)
  1759. goto out;
  1760. leaf = (struct gfs2_leaf *)bh->b_data;
  1761. len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
  1762. next_index = (index & ~(len - 1)) + len;
  1763. last = ((next_index >= hsize) ? 1 : 0);
  1764. error = leaf_dealloc(dip, index, len, leaf_no, bh,
  1765. last);
  1766. brelse(bh);
  1767. if (error)
  1768. goto out;
  1769. index = next_index;
  1770. } else
  1771. index++;
  1772. }
  1773. if (index != hsize) {
  1774. gfs2_consist_inode(dip);
  1775. error = -EIO;
  1776. }
  1777. out:
  1778. return error;
  1779. }
  1780. /**
  1781. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1782. * @ip: the file being written to
  1783. * @filname: the filename that's going to be added
  1784. * @da: The structure to return dir alloc info
  1785. *
  1786. * Returns: 0 if ok, -ve on error
  1787. */
  1788. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
  1789. struct gfs2_diradd *da)
  1790. {
  1791. struct gfs2_inode *ip = GFS2_I(inode);
  1792. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1793. const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
  1794. struct gfs2_dirent *dent;
  1795. struct buffer_head *bh;
  1796. da->nr_blocks = 0;
  1797. da->bh = NULL;
  1798. da->dent = NULL;
  1799. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1800. if (!dent) {
  1801. da->nr_blocks = sdp->sd_max_dirres;
  1802. if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
  1803. (GFS2_DIRENT_SIZE(name->len) < extra))
  1804. da->nr_blocks = 1;
  1805. return 0;
  1806. }
  1807. if (IS_ERR(dent))
  1808. return PTR_ERR(dent);
  1809. da->bh = bh;
  1810. da->dent = dent;
  1811. return 0;
  1812. }