dir.c 52 KB

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