namei.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  1. /*
  2. * linux/fs/ext4/namei.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/jbd2.h>
  29. #include <linux/time.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/quotaops.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/bio.h>
  36. #include "ext4.h"
  37. #include "ext4_jbd2.h"
  38. #include "namei.h"
  39. #include "xattr.h"
  40. #include "acl.h"
  41. /*
  42. * define how far ahead to read directories while searching them.
  43. */
  44. #define NAMEI_RA_CHUNKS 2
  45. #define NAMEI_RA_BLOCKS 4
  46. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  47. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  48. static struct buffer_head *ext4_append(handle_t *handle,
  49. struct inode *inode,
  50. ext4_lblk_t *block, int *err)
  51. {
  52. struct buffer_head *bh;
  53. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  54. bh = ext4_bread(handle, inode, *block, 1, err);
  55. if (bh) {
  56. inode->i_size += inode->i_sb->s_blocksize;
  57. EXT4_I(inode)->i_disksize = inode->i_size;
  58. *err = ext4_journal_get_write_access(handle, bh);
  59. if (*err) {
  60. brelse(bh);
  61. bh = NULL;
  62. }
  63. }
  64. return bh;
  65. }
  66. #ifndef assert
  67. #define assert(test) J_ASSERT(test)
  68. #endif
  69. #ifndef swap
  70. #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
  71. #endif
  72. #ifdef DX_DEBUG
  73. #define dxtrace(command) command
  74. #else
  75. #define dxtrace(command)
  76. #endif
  77. struct fake_dirent
  78. {
  79. __le32 inode;
  80. __le16 rec_len;
  81. u8 name_len;
  82. u8 file_type;
  83. };
  84. struct dx_countlimit
  85. {
  86. __le16 limit;
  87. __le16 count;
  88. };
  89. struct dx_entry
  90. {
  91. __le32 hash;
  92. __le32 block;
  93. };
  94. /*
  95. * dx_root_info is laid out so that if it should somehow get overlaid by a
  96. * dirent the two low bits of the hash version will be zero. Therefore, the
  97. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  98. */
  99. struct dx_root
  100. {
  101. struct fake_dirent dot;
  102. char dot_name[4];
  103. struct fake_dirent dotdot;
  104. char dotdot_name[4];
  105. struct dx_root_info
  106. {
  107. __le32 reserved_zero;
  108. u8 hash_version;
  109. u8 info_length; /* 8 */
  110. u8 indirect_levels;
  111. u8 unused_flags;
  112. }
  113. info;
  114. struct dx_entry entries[0];
  115. };
  116. struct dx_node
  117. {
  118. struct fake_dirent fake;
  119. struct dx_entry entries[0];
  120. };
  121. struct dx_frame
  122. {
  123. struct buffer_head *bh;
  124. struct dx_entry *entries;
  125. struct dx_entry *at;
  126. };
  127. struct dx_map_entry
  128. {
  129. u32 hash;
  130. u16 offs;
  131. u16 size;
  132. };
  133. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  134. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  135. static inline unsigned dx_get_hash(struct dx_entry *entry);
  136. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  137. static unsigned dx_get_count(struct dx_entry *entries);
  138. static unsigned dx_get_limit(struct dx_entry *entries);
  139. static void dx_set_count(struct dx_entry *entries, unsigned value);
  140. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  141. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  142. static unsigned dx_node_limit(struct inode *dir);
  143. static struct dx_frame *dx_probe(const struct qstr *d_name,
  144. struct inode *dir,
  145. struct dx_hash_info *hinfo,
  146. struct dx_frame *frame,
  147. int *err);
  148. static void dx_release(struct dx_frame *frames);
  149. static int dx_make_map(struct ext4_dir_entry_2 *de, int size,
  150. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  151. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  152. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  153. struct dx_map_entry *offsets, int count);
  154. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size);
  155. static void dx_insert_block(struct dx_frame *frame,
  156. u32 hash, ext4_lblk_t block);
  157. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  158. struct dx_frame *frame,
  159. struct dx_frame *frames,
  160. __u32 *start_hash);
  161. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  162. const struct qstr *d_name,
  163. struct ext4_dir_entry_2 **res_dir,
  164. int *err);
  165. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  166. struct inode *inode);
  167. /*
  168. * p is at least 6 bytes before the end of page
  169. */
  170. static inline struct ext4_dir_entry_2 *
  171. ext4_next_entry(struct ext4_dir_entry_2 *p)
  172. {
  173. return (struct ext4_dir_entry_2 *)((char *)p +
  174. ext4_rec_len_from_disk(p->rec_len));
  175. }
  176. /*
  177. * Future: use high four bits of block for coalesce-on-delete flags
  178. * Mask them off for now.
  179. */
  180. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  181. {
  182. return le32_to_cpu(entry->block) & 0x00ffffff;
  183. }
  184. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  185. {
  186. entry->block = cpu_to_le32(value);
  187. }
  188. static inline unsigned dx_get_hash(struct dx_entry *entry)
  189. {
  190. return le32_to_cpu(entry->hash);
  191. }
  192. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  193. {
  194. entry->hash = cpu_to_le32(value);
  195. }
  196. static inline unsigned dx_get_count(struct dx_entry *entries)
  197. {
  198. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  199. }
  200. static inline unsigned dx_get_limit(struct dx_entry *entries)
  201. {
  202. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  203. }
  204. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  205. {
  206. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  207. }
  208. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  209. {
  210. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  211. }
  212. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  213. {
  214. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  215. EXT4_DIR_REC_LEN(2) - infosize;
  216. return entry_space / sizeof(struct dx_entry);
  217. }
  218. static inline unsigned dx_node_limit(struct inode *dir)
  219. {
  220. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  221. return entry_space / sizeof(struct dx_entry);
  222. }
  223. /*
  224. * Debug
  225. */
  226. #ifdef DX_DEBUG
  227. static void dx_show_index(char * label, struct dx_entry *entries)
  228. {
  229. int i, n = dx_get_count (entries);
  230. printk(KERN_DEBUG "%s index ", label);
  231. for (i = 0; i < n; i++) {
  232. printk("%x->%lu ", i ? dx_get_hash(entries + i) :
  233. 0, (unsigned long)dx_get_block(entries + i));
  234. }
  235. printk("\n");
  236. }
  237. struct stats
  238. {
  239. unsigned names;
  240. unsigned space;
  241. unsigned bcount;
  242. };
  243. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
  244. int size, int show_names)
  245. {
  246. unsigned names = 0, space = 0;
  247. char *base = (char *) de;
  248. struct dx_hash_info h = *hinfo;
  249. printk("names: ");
  250. while ((char *) de < base + size)
  251. {
  252. if (de->inode)
  253. {
  254. if (show_names)
  255. {
  256. int len = de->name_len;
  257. char *name = de->name;
  258. while (len--) printk("%c", *name++);
  259. ext4fs_dirhash(de->name, de->name_len, &h);
  260. printk(":%x.%u ", h.hash,
  261. ((char *) de - base));
  262. }
  263. space += EXT4_DIR_REC_LEN(de->name_len);
  264. names++;
  265. }
  266. de = ext4_next_entry(de);
  267. }
  268. printk("(%i)\n", names);
  269. return (struct stats) { names, space, 1 };
  270. }
  271. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  272. struct dx_entry *entries, int levels)
  273. {
  274. unsigned blocksize = dir->i_sb->s_blocksize;
  275. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  276. unsigned bcount = 0;
  277. struct buffer_head *bh;
  278. int err;
  279. printk("%i indexed blocks...\n", count);
  280. for (i = 0; i < count; i++, entries++)
  281. {
  282. ext4_lblk_t block = dx_get_block(entries);
  283. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  284. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  285. struct stats stats;
  286. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  287. if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
  288. stats = levels?
  289. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  290. dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
  291. names += stats.names;
  292. space += stats.space;
  293. bcount += stats.bcount;
  294. brelse(bh);
  295. }
  296. if (bcount)
  297. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  298. levels ? "" : " ", names, space/bcount,
  299. (space/bcount)*100/blocksize);
  300. return (struct stats) { names, space, bcount};
  301. }
  302. #endif /* DX_DEBUG */
  303. /*
  304. * Probe for a directory leaf block to search.
  305. *
  306. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  307. * error in the directory index, and the caller should fall back to
  308. * searching the directory normally. The callers of dx_probe **MUST**
  309. * check for this error code, and make sure it never gets reflected
  310. * back to userspace.
  311. */
  312. static struct dx_frame *
  313. dx_probe(const struct qstr *d_name, struct inode *dir,
  314. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  315. {
  316. unsigned count, indirect;
  317. struct dx_entry *at, *entries, *p, *q, *m;
  318. struct dx_root *root;
  319. struct buffer_head *bh;
  320. struct dx_frame *frame = frame_in;
  321. u32 hash;
  322. frame->bh = NULL;
  323. if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
  324. goto fail;
  325. root = (struct dx_root *) bh->b_data;
  326. if (root->info.hash_version != DX_HASH_TEA &&
  327. root->info.hash_version != DX_HASH_HALF_MD4 &&
  328. root->info.hash_version != DX_HASH_LEGACY) {
  329. ext4_warning(dir->i_sb, __func__,
  330. "Unrecognised inode hash code %d",
  331. root->info.hash_version);
  332. brelse(bh);
  333. *err = ERR_BAD_DX_DIR;
  334. goto fail;
  335. }
  336. hinfo->hash_version = root->info.hash_version;
  337. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  338. if (d_name)
  339. ext4fs_dirhash(d_name->name, d_name->len, hinfo);
  340. hash = hinfo->hash;
  341. if (root->info.unused_flags & 1) {
  342. ext4_warning(dir->i_sb, __func__,
  343. "Unimplemented inode hash flags: %#06x",
  344. root->info.unused_flags);
  345. brelse(bh);
  346. *err = ERR_BAD_DX_DIR;
  347. goto fail;
  348. }
  349. if ((indirect = root->info.indirect_levels) > 1) {
  350. ext4_warning(dir->i_sb, __func__,
  351. "Unimplemented inode hash depth: %#06x",
  352. root->info.indirect_levels);
  353. brelse(bh);
  354. *err = ERR_BAD_DX_DIR;
  355. goto fail;
  356. }
  357. entries = (struct dx_entry *) (((char *)&root->info) +
  358. root->info.info_length);
  359. if (dx_get_limit(entries) != dx_root_limit(dir,
  360. root->info.info_length)) {
  361. ext4_warning(dir->i_sb, __func__,
  362. "dx entry: limit != root limit");
  363. brelse(bh);
  364. *err = ERR_BAD_DX_DIR;
  365. goto fail;
  366. }
  367. dxtrace(printk("Look up %x", hash));
  368. while (1)
  369. {
  370. count = dx_get_count(entries);
  371. if (!count || count > dx_get_limit(entries)) {
  372. ext4_warning(dir->i_sb, __func__,
  373. "dx entry: no count or count > limit");
  374. brelse(bh);
  375. *err = ERR_BAD_DX_DIR;
  376. goto fail2;
  377. }
  378. p = entries + 1;
  379. q = entries + count - 1;
  380. while (p <= q)
  381. {
  382. m = p + (q - p)/2;
  383. dxtrace(printk("."));
  384. if (dx_get_hash(m) > hash)
  385. q = m - 1;
  386. else
  387. p = m + 1;
  388. }
  389. if (0) // linear search cross check
  390. {
  391. unsigned n = count - 1;
  392. at = entries;
  393. while (n--)
  394. {
  395. dxtrace(printk(","));
  396. if (dx_get_hash(++at) > hash)
  397. {
  398. at--;
  399. break;
  400. }
  401. }
  402. assert (at == p - 1);
  403. }
  404. at = p - 1;
  405. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  406. frame->bh = bh;
  407. frame->entries = entries;
  408. frame->at = at;
  409. if (!indirect--) return frame;
  410. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  411. goto fail2;
  412. at = entries = ((struct dx_node *) bh->b_data)->entries;
  413. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  414. ext4_warning(dir->i_sb, __func__,
  415. "dx entry: limit != node limit");
  416. brelse(bh);
  417. *err = ERR_BAD_DX_DIR;
  418. goto fail2;
  419. }
  420. frame++;
  421. frame->bh = NULL;
  422. }
  423. fail2:
  424. while (frame >= frame_in) {
  425. brelse(frame->bh);
  426. frame--;
  427. }
  428. fail:
  429. if (*err == ERR_BAD_DX_DIR)
  430. ext4_warning(dir->i_sb, __func__,
  431. "Corrupt dir inode %ld, running e2fsck is "
  432. "recommended.", dir->i_ino);
  433. return NULL;
  434. }
  435. static void dx_release (struct dx_frame *frames)
  436. {
  437. if (frames[0].bh == NULL)
  438. return;
  439. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  440. brelse(frames[1].bh);
  441. brelse(frames[0].bh);
  442. }
  443. /*
  444. * This function increments the frame pointer to search the next leaf
  445. * block, and reads in the necessary intervening nodes if the search
  446. * should be necessary. Whether or not the search is necessary is
  447. * controlled by the hash parameter. If the hash value is even, then
  448. * the search is only continued if the next block starts with that
  449. * hash value. This is used if we are searching for a specific file.
  450. *
  451. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  452. *
  453. * This function returns 1 if the caller should continue to search,
  454. * or 0 if it should not. If there is an error reading one of the
  455. * index blocks, it will a negative error code.
  456. *
  457. * If start_hash is non-null, it will be filled in with the starting
  458. * hash of the next page.
  459. */
  460. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  461. struct dx_frame *frame,
  462. struct dx_frame *frames,
  463. __u32 *start_hash)
  464. {
  465. struct dx_frame *p;
  466. struct buffer_head *bh;
  467. int err, num_frames = 0;
  468. __u32 bhash;
  469. p = frame;
  470. /*
  471. * Find the next leaf page by incrementing the frame pointer.
  472. * If we run out of entries in the interior node, loop around and
  473. * increment pointer in the parent node. When we break out of
  474. * this loop, num_frames indicates the number of interior
  475. * nodes need to be read.
  476. */
  477. while (1) {
  478. if (++(p->at) < p->entries + dx_get_count(p->entries))
  479. break;
  480. if (p == frames)
  481. return 0;
  482. num_frames++;
  483. p--;
  484. }
  485. /*
  486. * If the hash is 1, then continue only if the next page has a
  487. * continuation hash of any value. This is used for readdir
  488. * handling. Otherwise, check to see if the hash matches the
  489. * desired contiuation hash. If it doesn't, return since
  490. * there's no point to read in the successive index pages.
  491. */
  492. bhash = dx_get_hash(p->at);
  493. if (start_hash)
  494. *start_hash = bhash;
  495. if ((hash & 1) == 0) {
  496. if ((bhash & ~1) != hash)
  497. return 0;
  498. }
  499. /*
  500. * If the hash is HASH_NB_ALWAYS, we always go to the next
  501. * block so no check is necessary
  502. */
  503. while (num_frames--) {
  504. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  505. 0, &err)))
  506. return err; /* Failure */
  507. p++;
  508. brelse(p->bh);
  509. p->bh = bh;
  510. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  511. }
  512. return 1;
  513. }
  514. /*
  515. * This function fills a red-black tree with information from a
  516. * directory block. It returns the number directory entries loaded
  517. * into the tree. If there is an error it is returned in err.
  518. */
  519. static int htree_dirblock_to_tree(struct file *dir_file,
  520. struct inode *dir, ext4_lblk_t block,
  521. struct dx_hash_info *hinfo,
  522. __u32 start_hash, __u32 start_minor_hash)
  523. {
  524. struct buffer_head *bh;
  525. struct ext4_dir_entry_2 *de, *top;
  526. int err, count = 0;
  527. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  528. (unsigned long)block));
  529. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  530. return err;
  531. de = (struct ext4_dir_entry_2 *) bh->b_data;
  532. top = (struct ext4_dir_entry_2 *) ((char *) de +
  533. dir->i_sb->s_blocksize -
  534. EXT4_DIR_REC_LEN(0));
  535. for (; de < top; de = ext4_next_entry(de)) {
  536. if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
  537. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  538. +((char *)de - bh->b_data))) {
  539. /* On error, skip the f_pos to the next block. */
  540. dir_file->f_pos = (dir_file->f_pos |
  541. (dir->i_sb->s_blocksize - 1)) + 1;
  542. brelse(bh);
  543. return count;
  544. }
  545. ext4fs_dirhash(de->name, de->name_len, hinfo);
  546. if ((hinfo->hash < start_hash) ||
  547. ((hinfo->hash == start_hash) &&
  548. (hinfo->minor_hash < start_minor_hash)))
  549. continue;
  550. if (de->inode == 0)
  551. continue;
  552. if ((err = ext4_htree_store_dirent(dir_file,
  553. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  554. brelse(bh);
  555. return err;
  556. }
  557. count++;
  558. }
  559. brelse(bh);
  560. return count;
  561. }
  562. /*
  563. * This function fills a red-black tree with information from a
  564. * directory. We start scanning the directory in hash order, starting
  565. * at start_hash and start_minor_hash.
  566. *
  567. * This function returns the number of entries inserted into the tree,
  568. * or a negative error code.
  569. */
  570. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  571. __u32 start_minor_hash, __u32 *next_hash)
  572. {
  573. struct dx_hash_info hinfo;
  574. struct ext4_dir_entry_2 *de;
  575. struct dx_frame frames[2], *frame;
  576. struct inode *dir;
  577. ext4_lblk_t block;
  578. int count = 0;
  579. int ret, err;
  580. __u32 hashval;
  581. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  582. start_hash, start_minor_hash));
  583. dir = dir_file->f_path.dentry->d_inode;
  584. if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
  585. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  586. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  587. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  588. start_hash, start_minor_hash);
  589. *next_hash = ~0;
  590. return count;
  591. }
  592. hinfo.hash = start_hash;
  593. hinfo.minor_hash = 0;
  594. frame = dx_probe(NULL, dir, &hinfo, frames, &err);
  595. if (!frame)
  596. return err;
  597. /* Add '.' and '..' from the htree header */
  598. if (!start_hash && !start_minor_hash) {
  599. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  600. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  601. goto errout;
  602. count++;
  603. }
  604. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  605. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  606. de = ext4_next_entry(de);
  607. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  608. goto errout;
  609. count++;
  610. }
  611. while (1) {
  612. block = dx_get_block(frame->at);
  613. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  614. start_hash, start_minor_hash);
  615. if (ret < 0) {
  616. err = ret;
  617. goto errout;
  618. }
  619. count += ret;
  620. hashval = ~0;
  621. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  622. frame, frames, &hashval);
  623. *next_hash = hashval;
  624. if (ret < 0) {
  625. err = ret;
  626. goto errout;
  627. }
  628. /*
  629. * Stop if: (a) there are no more entries, or
  630. * (b) we have inserted at least one entry and the
  631. * next hash value is not a continuation
  632. */
  633. if ((ret == 0) ||
  634. (count && ((hashval & 1) == 0)))
  635. break;
  636. }
  637. dx_release(frames);
  638. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  639. "next hash: %x\n", count, *next_hash));
  640. return count;
  641. errout:
  642. dx_release(frames);
  643. return (err);
  644. }
  645. /*
  646. * Directory block splitting, compacting
  647. */
  648. /*
  649. * Create map of hash values, offsets, and sizes, stored at end of block.
  650. * Returns number of entries mapped.
  651. */
  652. static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
  653. struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
  654. {
  655. int count = 0;
  656. char *base = (char *) de;
  657. struct dx_hash_info h = *hinfo;
  658. while ((char *) de < base + size)
  659. {
  660. if (de->name_len && de->inode) {
  661. ext4fs_dirhash(de->name, de->name_len, &h);
  662. map_tail--;
  663. map_tail->hash = h.hash;
  664. map_tail->offs = (u16) ((char *) de - base);
  665. map_tail->size = le16_to_cpu(de->rec_len);
  666. count++;
  667. cond_resched();
  668. }
  669. /* XXX: do we need to check rec_len == 0 case? -Chris */
  670. de = ext4_next_entry(de);
  671. }
  672. return count;
  673. }
  674. /* Sort map by hash value */
  675. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  676. {
  677. struct dx_map_entry *p, *q, *top = map + count - 1;
  678. int more;
  679. /* Combsort until bubble sort doesn't suck */
  680. while (count > 2) {
  681. count = count*10/13;
  682. if (count - 9 < 2) /* 9, 10 -> 11 */
  683. count = 11;
  684. for (p = top, q = p - count; q >= map; p--, q--)
  685. if (p->hash < q->hash)
  686. swap(*p, *q);
  687. }
  688. /* Garden variety bubble sort */
  689. do {
  690. more = 0;
  691. q = top;
  692. while (q-- > map) {
  693. if (q[1].hash >= q[0].hash)
  694. continue;
  695. swap(*(q+1), *q);
  696. more = 1;
  697. }
  698. } while(more);
  699. }
  700. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  701. {
  702. struct dx_entry *entries = frame->entries;
  703. struct dx_entry *old = frame->at, *new = old + 1;
  704. int count = dx_get_count(entries);
  705. assert(count < dx_get_limit(entries));
  706. assert(old < entries + count);
  707. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  708. dx_set_hash(new, hash);
  709. dx_set_block(new, block);
  710. dx_set_count(entries, count + 1);
  711. }
  712. static void ext4_update_dx_flag(struct inode *inode)
  713. {
  714. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  715. EXT4_FEATURE_COMPAT_DIR_INDEX))
  716. EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
  717. }
  718. /*
  719. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  720. *
  721. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  722. * `de != NULL' is guaranteed by caller.
  723. */
  724. static inline int ext4_match (int len, const char * const name,
  725. struct ext4_dir_entry_2 * de)
  726. {
  727. if (len != de->name_len)
  728. return 0;
  729. if (!de->inode)
  730. return 0;
  731. return !memcmp(name, de->name, len);
  732. }
  733. /*
  734. * Returns 0 if not found, -1 on failure, and 1 on success
  735. */
  736. static inline int search_dirblock(struct buffer_head *bh,
  737. struct inode *dir,
  738. const struct qstr *d_name,
  739. unsigned long offset,
  740. struct ext4_dir_entry_2 ** res_dir)
  741. {
  742. struct ext4_dir_entry_2 * de;
  743. char * dlimit;
  744. int de_len;
  745. const char *name = d_name->name;
  746. int namelen = d_name->len;
  747. de = (struct ext4_dir_entry_2 *) bh->b_data;
  748. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  749. while ((char *) de < dlimit) {
  750. /* this code is executed quadratically often */
  751. /* do minimal checking `by hand' */
  752. if ((char *) de + namelen <= dlimit &&
  753. ext4_match (namelen, name, de)) {
  754. /* found a match - just to be sure, do a full check */
  755. if (!ext4_check_dir_entry("ext4_find_entry",
  756. dir, de, bh, offset))
  757. return -1;
  758. *res_dir = de;
  759. return 1;
  760. }
  761. /* prevent looping on a bad block */
  762. de_len = ext4_rec_len_from_disk(de->rec_len);
  763. if (de_len <= 0)
  764. return -1;
  765. offset += de_len;
  766. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  767. }
  768. return 0;
  769. }
  770. /*
  771. * ext4_find_entry()
  772. *
  773. * finds an entry in the specified directory with the wanted name. It
  774. * returns the cache buffer in which the entry was found, and the entry
  775. * itself (as a parameter - res_dir). It does NOT read the inode of the
  776. * entry - you'll have to do that yourself if you want to.
  777. *
  778. * The returned buffer_head has ->b_count elevated. The caller is expected
  779. * to brelse() it when appropriate.
  780. */
  781. static struct buffer_head * ext4_find_entry (struct inode *dir,
  782. const struct qstr *d_name,
  783. struct ext4_dir_entry_2 ** res_dir)
  784. {
  785. struct super_block *sb;
  786. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  787. struct buffer_head *bh, *ret = NULL;
  788. ext4_lblk_t start, block, b;
  789. int ra_max = 0; /* Number of bh's in the readahead
  790. buffer, bh_use[] */
  791. int ra_ptr = 0; /* Current index into readahead
  792. buffer */
  793. int num = 0;
  794. ext4_lblk_t nblocks;
  795. int i, err;
  796. int namelen;
  797. *res_dir = NULL;
  798. sb = dir->i_sb;
  799. namelen = d_name->len;
  800. if (namelen > EXT4_NAME_LEN)
  801. return NULL;
  802. if (is_dx(dir)) {
  803. bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
  804. /*
  805. * On success, or if the error was file not found,
  806. * return. Otherwise, fall back to doing a search the
  807. * old fashioned way.
  808. */
  809. if (bh || (err != ERR_BAD_DX_DIR))
  810. return bh;
  811. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  812. "falling back\n"));
  813. }
  814. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  815. start = EXT4_I(dir)->i_dir_start_lookup;
  816. if (start >= nblocks)
  817. start = 0;
  818. block = start;
  819. restart:
  820. do {
  821. /*
  822. * We deal with the read-ahead logic here.
  823. */
  824. if (ra_ptr >= ra_max) {
  825. /* Refill the readahead buffer */
  826. ra_ptr = 0;
  827. b = block;
  828. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  829. /*
  830. * Terminate if we reach the end of the
  831. * directory and must wrap, or if our
  832. * search has finished at this block.
  833. */
  834. if (b >= nblocks || (num && block == start)) {
  835. bh_use[ra_max] = NULL;
  836. break;
  837. }
  838. num++;
  839. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  840. bh_use[ra_max] = bh;
  841. if (bh)
  842. ll_rw_block(READ_META, 1, &bh);
  843. }
  844. }
  845. if ((bh = bh_use[ra_ptr++]) == NULL)
  846. goto next;
  847. wait_on_buffer(bh);
  848. if (!buffer_uptodate(bh)) {
  849. /* read error, skip block & hope for the best */
  850. ext4_error(sb, __func__, "reading directory #%lu "
  851. "offset %lu", dir->i_ino,
  852. (unsigned long)block);
  853. brelse(bh);
  854. goto next;
  855. }
  856. i = search_dirblock(bh, dir, d_name,
  857. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  858. if (i == 1) {
  859. EXT4_I(dir)->i_dir_start_lookup = block;
  860. ret = bh;
  861. goto cleanup_and_exit;
  862. } else {
  863. brelse(bh);
  864. if (i < 0)
  865. goto cleanup_and_exit;
  866. }
  867. next:
  868. if (++block >= nblocks)
  869. block = 0;
  870. } while (block != start);
  871. /*
  872. * If the directory has grown while we were searching, then
  873. * search the last part of the directory before giving up.
  874. */
  875. block = nblocks;
  876. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  877. if (block < nblocks) {
  878. start = 0;
  879. goto restart;
  880. }
  881. cleanup_and_exit:
  882. /* Clean up the read-ahead blocks */
  883. for (; ra_ptr < ra_max; ra_ptr++)
  884. brelse(bh_use[ra_ptr]);
  885. return ret;
  886. }
  887. static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
  888. struct ext4_dir_entry_2 **res_dir, int *err)
  889. {
  890. struct super_block * sb;
  891. struct dx_hash_info hinfo;
  892. u32 hash;
  893. struct dx_frame frames[2], *frame;
  894. struct ext4_dir_entry_2 *de, *top;
  895. struct buffer_head *bh;
  896. ext4_lblk_t block;
  897. int retval;
  898. int namelen = d_name->len;
  899. const u8 *name = d_name->name;
  900. sb = dir->i_sb;
  901. /* NFS may look up ".." - look at dx_root directory block */
  902. if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
  903. if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
  904. return NULL;
  905. } else {
  906. frame = frames;
  907. frame->bh = NULL; /* for dx_release() */
  908. frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
  909. dx_set_block(frame->at, 0); /* dx_root block is 0 */
  910. }
  911. hash = hinfo.hash;
  912. do {
  913. block = dx_get_block(frame->at);
  914. if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
  915. goto errout;
  916. de = (struct ext4_dir_entry_2 *) bh->b_data;
  917. top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
  918. EXT4_DIR_REC_LEN(0));
  919. for (; de < top; de = ext4_next_entry(de)) {
  920. int off = (block << EXT4_BLOCK_SIZE_BITS(sb))
  921. + ((char *) de - bh->b_data);
  922. if (!ext4_check_dir_entry(__func__, dir, de, bh, off)) {
  923. brelse(bh);
  924. *err = ERR_BAD_DX_DIR;
  925. goto errout;
  926. }
  927. if (ext4_match(namelen, name, de)) {
  928. *res_dir = de;
  929. dx_release(frames);
  930. return bh;
  931. }
  932. }
  933. brelse(bh);
  934. /* Check to see if we should continue to search */
  935. retval = ext4_htree_next_block(dir, hash, frame,
  936. frames, NULL);
  937. if (retval < 0) {
  938. ext4_warning(sb, __func__,
  939. "error reading index page in directory #%lu",
  940. dir->i_ino);
  941. *err = retval;
  942. goto errout;
  943. }
  944. } while (retval == 1);
  945. *err = -ENOENT;
  946. errout:
  947. dxtrace(printk(KERN_DEBUG "%s not found\n", name));
  948. dx_release (frames);
  949. return NULL;
  950. }
  951. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  952. {
  953. struct inode *inode;
  954. struct ext4_dir_entry_2 *de;
  955. struct buffer_head *bh;
  956. if (dentry->d_name.len > EXT4_NAME_LEN)
  957. return ERR_PTR(-ENAMETOOLONG);
  958. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  959. inode = NULL;
  960. if (bh) {
  961. unsigned long ino = le32_to_cpu(de->inode);
  962. brelse(bh);
  963. if (!ext4_valid_inum(dir->i_sb, ino)) {
  964. ext4_error(dir->i_sb, "ext4_lookup",
  965. "bad inode number: %lu", ino);
  966. return ERR_PTR(-EIO);
  967. }
  968. inode = ext4_iget(dir->i_sb, ino);
  969. if (IS_ERR(inode))
  970. return ERR_CAST(inode);
  971. }
  972. return d_splice_alias(inode, dentry);
  973. }
  974. struct dentry *ext4_get_parent(struct dentry *child)
  975. {
  976. unsigned long ino;
  977. struct dentry *parent;
  978. struct inode *inode;
  979. static const struct qstr dotdot = {
  980. .name = "..",
  981. .len = 2,
  982. };
  983. struct ext4_dir_entry_2 * de;
  984. struct buffer_head *bh;
  985. bh = ext4_find_entry(child->d_inode, &dotdot, &de);
  986. inode = NULL;
  987. if (!bh)
  988. return ERR_PTR(-ENOENT);
  989. ino = le32_to_cpu(de->inode);
  990. brelse(bh);
  991. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  992. ext4_error(child->d_inode->i_sb, "ext4_get_parent",
  993. "bad inode number: %lu", ino);
  994. return ERR_PTR(-EIO);
  995. }
  996. inode = ext4_iget(child->d_inode->i_sb, ino);
  997. if (IS_ERR(inode))
  998. return ERR_CAST(inode);
  999. parent = d_alloc_anon(inode);
  1000. if (!parent) {
  1001. iput(inode);
  1002. parent = ERR_PTR(-ENOMEM);
  1003. }
  1004. return parent;
  1005. }
  1006. #define S_SHIFT 12
  1007. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  1008. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  1009. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  1010. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  1011. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  1012. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  1013. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  1014. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  1015. };
  1016. static inline void ext4_set_de_type(struct super_block *sb,
  1017. struct ext4_dir_entry_2 *de,
  1018. umode_t mode) {
  1019. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1020. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1021. }
  1022. /*
  1023. * Move count entries from end of map between two memory locations.
  1024. * Returns pointer to last entry moved.
  1025. */
  1026. static struct ext4_dir_entry_2 *
  1027. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
  1028. {
  1029. unsigned rec_len = 0;
  1030. while (count--) {
  1031. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
  1032. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1033. memcpy (to, de, rec_len);
  1034. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1035. ext4_rec_len_to_disk(rec_len);
  1036. de->inode = 0;
  1037. map++;
  1038. to += rec_len;
  1039. }
  1040. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1041. }
  1042. /*
  1043. * Compact each dir entry in the range to the minimal rec_len.
  1044. * Returns pointer to last entry in range.
  1045. */
  1046. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
  1047. {
  1048. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1049. unsigned rec_len = 0;
  1050. prev = to = de;
  1051. while ((char*)de < base + size) {
  1052. next = ext4_next_entry(de);
  1053. if (de->inode && de->name_len) {
  1054. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1055. if (de > to)
  1056. memmove(to, de, rec_len);
  1057. to->rec_len = ext4_rec_len_to_disk(rec_len);
  1058. prev = to;
  1059. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1060. }
  1061. de = next;
  1062. }
  1063. return prev;
  1064. }
  1065. /*
  1066. * Split a full leaf block to make room for a new dir entry.
  1067. * Allocate a new block, and move entries so that they are approx. equally full.
  1068. * Returns pointer to de in block into which the new entry will be inserted.
  1069. */
  1070. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1071. struct buffer_head **bh,struct dx_frame *frame,
  1072. struct dx_hash_info *hinfo, int *error)
  1073. {
  1074. unsigned blocksize = dir->i_sb->s_blocksize;
  1075. unsigned count, continued;
  1076. struct buffer_head *bh2;
  1077. ext4_lblk_t newblock;
  1078. u32 hash2;
  1079. struct dx_map_entry *map;
  1080. char *data1 = (*bh)->b_data, *data2;
  1081. unsigned split, move, size, i;
  1082. struct ext4_dir_entry_2 *de = NULL, *de2;
  1083. int err = 0;
  1084. bh2 = ext4_append (handle, dir, &newblock, &err);
  1085. if (!(bh2)) {
  1086. brelse(*bh);
  1087. *bh = NULL;
  1088. goto errout;
  1089. }
  1090. BUFFER_TRACE(*bh, "get_write_access");
  1091. err = ext4_journal_get_write_access(handle, *bh);
  1092. if (err)
  1093. goto journal_error;
  1094. BUFFER_TRACE(frame->bh, "get_write_access");
  1095. err = ext4_journal_get_write_access(handle, frame->bh);
  1096. if (err)
  1097. goto journal_error;
  1098. data2 = bh2->b_data;
  1099. /* create map in the end of data2 block */
  1100. map = (struct dx_map_entry *) (data2 + blocksize);
  1101. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1102. blocksize, hinfo, map);
  1103. map -= count;
  1104. dx_sort_map(map, count);
  1105. /* Split the existing block in the middle, size-wise */
  1106. size = 0;
  1107. move = 0;
  1108. for (i = count-1; i >= 0; i--) {
  1109. /* is more than half of this entry in 2nd half of the block? */
  1110. if (size + map[i].size/2 > blocksize/2)
  1111. break;
  1112. size += map[i].size;
  1113. move++;
  1114. }
  1115. /* map index at which we will split */
  1116. split = count - move;
  1117. hash2 = map[split].hash;
  1118. continued = hash2 == map[split - 1].hash;
  1119. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1120. (unsigned long)dx_get_block(frame->at),
  1121. hash2, split, count-split));
  1122. /* Fancy dance to stay within two buffers */
  1123. de2 = dx_move_dirents(data1, data2, map + split, count - split);
  1124. de = dx_pack_dirents(data1, blocksize);
  1125. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
  1126. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2);
  1127. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1128. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1129. /* Which block gets the new entry? */
  1130. if (hinfo->hash >= hash2)
  1131. {
  1132. swap(*bh, bh2);
  1133. de = de2;
  1134. }
  1135. dx_insert_block(frame, hash2 + continued, newblock);
  1136. err = ext4_journal_dirty_metadata(handle, bh2);
  1137. if (err)
  1138. goto journal_error;
  1139. err = ext4_journal_dirty_metadata(handle, frame->bh);
  1140. if (err)
  1141. goto journal_error;
  1142. brelse(bh2);
  1143. dxtrace(dx_show_index("frame", frame->entries));
  1144. return de;
  1145. journal_error:
  1146. brelse(*bh);
  1147. brelse(bh2);
  1148. *bh = NULL;
  1149. ext4_std_error(dir->i_sb, err);
  1150. errout:
  1151. *error = err;
  1152. return NULL;
  1153. }
  1154. /*
  1155. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1156. * it points to a directory entry which is guaranteed to be large
  1157. * enough for new directory entry. If de is NULL, then
  1158. * add_dirent_to_buf will attempt search the directory block for
  1159. * space. It will return -ENOSPC if no space is available, and -EIO
  1160. * and -EEXIST if directory entry already exists.
  1161. *
  1162. * NOTE! bh is NOT released in the case where ENOSPC is returned. In
  1163. * all other cases bh is released.
  1164. */
  1165. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1166. struct inode *inode, struct ext4_dir_entry_2 *de,
  1167. struct buffer_head *bh)
  1168. {
  1169. struct inode *dir = dentry->d_parent->d_inode;
  1170. const char *name = dentry->d_name.name;
  1171. int namelen = dentry->d_name.len;
  1172. unsigned long offset = 0;
  1173. unsigned short reclen;
  1174. int nlen, rlen, err;
  1175. char *top;
  1176. reclen = EXT4_DIR_REC_LEN(namelen);
  1177. if (!de) {
  1178. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1179. top = bh->b_data + dir->i_sb->s_blocksize - reclen;
  1180. while ((char *) de <= top) {
  1181. if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
  1182. bh, offset)) {
  1183. brelse(bh);
  1184. return -EIO;
  1185. }
  1186. if (ext4_match(namelen, name, de)) {
  1187. brelse(bh);
  1188. return -EEXIST;
  1189. }
  1190. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1191. rlen = ext4_rec_len_from_disk(de->rec_len);
  1192. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1193. break;
  1194. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1195. offset += rlen;
  1196. }
  1197. if ((char *) de > top)
  1198. return -ENOSPC;
  1199. }
  1200. BUFFER_TRACE(bh, "get_write_access");
  1201. err = ext4_journal_get_write_access(handle, bh);
  1202. if (err) {
  1203. ext4_std_error(dir->i_sb, err);
  1204. brelse(bh);
  1205. return err;
  1206. }
  1207. /* By now the buffer is marked for journaling */
  1208. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1209. rlen = ext4_rec_len_from_disk(de->rec_len);
  1210. if (de->inode) {
  1211. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1212. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen);
  1213. de->rec_len = ext4_rec_len_to_disk(nlen);
  1214. de = de1;
  1215. }
  1216. de->file_type = EXT4_FT_UNKNOWN;
  1217. if (inode) {
  1218. de->inode = cpu_to_le32(inode->i_ino);
  1219. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1220. } else
  1221. de->inode = 0;
  1222. de->name_len = namelen;
  1223. memcpy(de->name, name, namelen);
  1224. /*
  1225. * XXX shouldn't update any times until successful
  1226. * completion of syscall, but too many callers depend
  1227. * on this.
  1228. *
  1229. * XXX similarly, too many callers depend on
  1230. * ext4_new_inode() setting the times, but error
  1231. * recovery deletes the inode, so the worst that can
  1232. * happen is that the times are slightly out of date
  1233. * and/or different from the directory change time.
  1234. */
  1235. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1236. ext4_update_dx_flag(dir);
  1237. dir->i_version++;
  1238. ext4_mark_inode_dirty(handle, dir);
  1239. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  1240. err = ext4_journal_dirty_metadata(handle, bh);
  1241. if (err)
  1242. ext4_std_error(dir->i_sb, err);
  1243. brelse(bh);
  1244. return 0;
  1245. }
  1246. /*
  1247. * This converts a one block unindexed directory to a 3 block indexed
  1248. * directory, and adds the dentry to the indexed directory.
  1249. */
  1250. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1251. struct inode *inode, struct buffer_head *bh)
  1252. {
  1253. struct inode *dir = dentry->d_parent->d_inode;
  1254. const char *name = dentry->d_name.name;
  1255. int namelen = dentry->d_name.len;
  1256. struct buffer_head *bh2;
  1257. struct dx_root *root;
  1258. struct dx_frame frames[2], *frame;
  1259. struct dx_entry *entries;
  1260. struct ext4_dir_entry_2 *de, *de2;
  1261. char *data1, *top;
  1262. unsigned len;
  1263. int retval;
  1264. unsigned blocksize;
  1265. struct dx_hash_info hinfo;
  1266. ext4_lblk_t block;
  1267. struct fake_dirent *fde;
  1268. blocksize = dir->i_sb->s_blocksize;
  1269. dxtrace(printk(KERN_DEBUG "Creating index\n"));
  1270. retval = ext4_journal_get_write_access(handle, bh);
  1271. if (retval) {
  1272. ext4_std_error(dir->i_sb, retval);
  1273. brelse(bh);
  1274. return retval;
  1275. }
  1276. root = (struct dx_root *) bh->b_data;
  1277. bh2 = ext4_append(handle, dir, &block, &retval);
  1278. if (!(bh2)) {
  1279. brelse(bh);
  1280. return retval;
  1281. }
  1282. EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
  1283. data1 = bh2->b_data;
  1284. /* The 0th block becomes the root, move the dirents out */
  1285. fde = &root->dotdot;
  1286. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1287. ext4_rec_len_from_disk(fde->rec_len));
  1288. len = ((char *) root) + blocksize - (char *) de;
  1289. memcpy (data1, de, len);
  1290. de = (struct ext4_dir_entry_2 *) data1;
  1291. top = data1 + len;
  1292. while ((char *)(de2 = ext4_next_entry(de)) < top)
  1293. de = de2;
  1294. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
  1295. /* Initialize the root; the dot dirents already exist */
  1296. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1297. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2));
  1298. memset (&root->info, 0, sizeof(root->info));
  1299. root->info.info_length = sizeof(root->info);
  1300. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1301. entries = root->entries;
  1302. dx_set_block(entries, 1);
  1303. dx_set_count(entries, 1);
  1304. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1305. /* Initialize as for dx_probe */
  1306. hinfo.hash_version = root->info.hash_version;
  1307. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1308. ext4fs_dirhash(name, namelen, &hinfo);
  1309. frame = frames;
  1310. frame->entries = entries;
  1311. frame->at = entries;
  1312. frame->bh = bh;
  1313. bh = bh2;
  1314. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1315. dx_release (frames);
  1316. if (!(de))
  1317. return retval;
  1318. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1319. }
  1320. /*
  1321. * ext4_add_entry()
  1322. *
  1323. * adds a file entry to the specified directory, using the same
  1324. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1325. *
  1326. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1327. * may not sleep between calling this and putting something into
  1328. * the entry, as someone else might have used it while you slept.
  1329. */
  1330. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1331. struct inode *inode)
  1332. {
  1333. struct inode *dir = dentry->d_parent->d_inode;
  1334. unsigned long offset;
  1335. struct buffer_head *bh;
  1336. struct ext4_dir_entry_2 *de;
  1337. struct super_block *sb;
  1338. int retval;
  1339. int dx_fallback=0;
  1340. unsigned blocksize;
  1341. ext4_lblk_t block, blocks;
  1342. sb = dir->i_sb;
  1343. blocksize = sb->s_blocksize;
  1344. if (!dentry->d_name.len)
  1345. return -EINVAL;
  1346. if (is_dx(dir)) {
  1347. retval = ext4_dx_add_entry(handle, dentry, inode);
  1348. if (!retval || (retval != ERR_BAD_DX_DIR))
  1349. return retval;
  1350. EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
  1351. dx_fallback++;
  1352. ext4_mark_inode_dirty(handle, dir);
  1353. }
  1354. blocks = dir->i_size >> sb->s_blocksize_bits;
  1355. for (block = 0, offset = 0; block < blocks; block++) {
  1356. bh = ext4_bread(handle, dir, block, 0, &retval);
  1357. if(!bh)
  1358. return retval;
  1359. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1360. if (retval != -ENOSPC)
  1361. return retval;
  1362. if (blocks == 1 && !dx_fallback &&
  1363. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1364. return make_indexed_dir(handle, dentry, inode, bh);
  1365. brelse(bh);
  1366. }
  1367. bh = ext4_append(handle, dir, &block, &retval);
  1368. if (!bh)
  1369. return retval;
  1370. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1371. de->inode = 0;
  1372. de->rec_len = ext4_rec_len_to_disk(blocksize);
  1373. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1374. }
  1375. /*
  1376. * Returns 0 for success, or a negative error value
  1377. */
  1378. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1379. struct inode *inode)
  1380. {
  1381. struct dx_frame frames[2], *frame;
  1382. struct dx_entry *entries, *at;
  1383. struct dx_hash_info hinfo;
  1384. struct buffer_head *bh;
  1385. struct inode *dir = dentry->d_parent->d_inode;
  1386. struct super_block *sb = dir->i_sb;
  1387. struct ext4_dir_entry_2 *de;
  1388. int err;
  1389. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1390. if (!frame)
  1391. return err;
  1392. entries = frame->entries;
  1393. at = frame->at;
  1394. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1395. goto cleanup;
  1396. BUFFER_TRACE(bh, "get_write_access");
  1397. err = ext4_journal_get_write_access(handle, bh);
  1398. if (err)
  1399. goto journal_error;
  1400. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1401. if (err != -ENOSPC) {
  1402. bh = NULL;
  1403. goto cleanup;
  1404. }
  1405. /* Block full, should compress but for now just split */
  1406. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1407. dx_get_count(entries), dx_get_limit(entries)));
  1408. /* Need to split index? */
  1409. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1410. ext4_lblk_t newblock;
  1411. unsigned icount = dx_get_count(entries);
  1412. int levels = frame - frames;
  1413. struct dx_entry *entries2;
  1414. struct dx_node *node2;
  1415. struct buffer_head *bh2;
  1416. if (levels && (dx_get_count(frames->entries) ==
  1417. dx_get_limit(frames->entries))) {
  1418. ext4_warning(sb, __func__,
  1419. "Directory index full!");
  1420. err = -ENOSPC;
  1421. goto cleanup;
  1422. }
  1423. bh2 = ext4_append (handle, dir, &newblock, &err);
  1424. if (!(bh2))
  1425. goto cleanup;
  1426. node2 = (struct dx_node *)(bh2->b_data);
  1427. entries2 = node2->entries;
  1428. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize);
  1429. node2->fake.inode = 0;
  1430. BUFFER_TRACE(frame->bh, "get_write_access");
  1431. err = ext4_journal_get_write_access(handle, frame->bh);
  1432. if (err)
  1433. goto journal_error;
  1434. if (levels) {
  1435. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1436. unsigned hash2 = dx_get_hash(entries + icount1);
  1437. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1438. icount1, icount2));
  1439. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1440. err = ext4_journal_get_write_access(handle,
  1441. frames[0].bh);
  1442. if (err)
  1443. goto journal_error;
  1444. memcpy((char *) entries2, (char *) (entries + icount1),
  1445. icount2 * sizeof(struct dx_entry));
  1446. dx_set_count(entries, icount1);
  1447. dx_set_count(entries2, icount2);
  1448. dx_set_limit(entries2, dx_node_limit(dir));
  1449. /* Which index block gets the new entry? */
  1450. if (at - entries >= icount1) {
  1451. frame->at = at = at - entries - icount1 + entries2;
  1452. frame->entries = entries = entries2;
  1453. swap(frame->bh, bh2);
  1454. }
  1455. dx_insert_block(frames + 0, hash2, newblock);
  1456. dxtrace(dx_show_index("node", frames[1].entries));
  1457. dxtrace(dx_show_index("node",
  1458. ((struct dx_node *) bh2->b_data)->entries));
  1459. err = ext4_journal_dirty_metadata(handle, bh2);
  1460. if (err)
  1461. goto journal_error;
  1462. brelse (bh2);
  1463. } else {
  1464. dxtrace(printk(KERN_DEBUG
  1465. "Creating second level index...\n"));
  1466. memcpy((char *) entries2, (char *) entries,
  1467. icount * sizeof(struct dx_entry));
  1468. dx_set_limit(entries2, dx_node_limit(dir));
  1469. /* Set up root */
  1470. dx_set_count(entries, 1);
  1471. dx_set_block(entries + 0, newblock);
  1472. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1473. /* Add new access path frame */
  1474. frame = frames + 1;
  1475. frame->at = at = at - entries + entries2;
  1476. frame->entries = entries = entries2;
  1477. frame->bh = bh2;
  1478. err = ext4_journal_get_write_access(handle,
  1479. frame->bh);
  1480. if (err)
  1481. goto journal_error;
  1482. }
  1483. ext4_journal_dirty_metadata(handle, frames[0].bh);
  1484. }
  1485. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1486. if (!de)
  1487. goto cleanup;
  1488. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1489. bh = NULL;
  1490. goto cleanup;
  1491. journal_error:
  1492. ext4_std_error(dir->i_sb, err);
  1493. cleanup:
  1494. if (bh)
  1495. brelse(bh);
  1496. dx_release(frames);
  1497. return err;
  1498. }
  1499. /*
  1500. * ext4_delete_entry deletes a directory entry by merging it with the
  1501. * previous entry
  1502. */
  1503. static int ext4_delete_entry(handle_t *handle,
  1504. struct inode *dir,
  1505. struct ext4_dir_entry_2 *de_del,
  1506. struct buffer_head *bh)
  1507. {
  1508. struct ext4_dir_entry_2 *de, *pde;
  1509. int i;
  1510. i = 0;
  1511. pde = NULL;
  1512. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1513. while (i < bh->b_size) {
  1514. if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
  1515. return -EIO;
  1516. if (de == de_del) {
  1517. BUFFER_TRACE(bh, "get_write_access");
  1518. ext4_journal_get_write_access(handle, bh);
  1519. if (pde)
  1520. pde->rec_len = ext4_rec_len_to_disk(
  1521. ext4_rec_len_from_disk(pde->rec_len) +
  1522. ext4_rec_len_from_disk(de->rec_len));
  1523. else
  1524. de->inode = 0;
  1525. dir->i_version++;
  1526. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  1527. ext4_journal_dirty_metadata(handle, bh);
  1528. return 0;
  1529. }
  1530. i += ext4_rec_len_from_disk(de->rec_len);
  1531. pde = de;
  1532. de = ext4_next_entry(de);
  1533. }
  1534. return -ENOENT;
  1535. }
  1536. /*
  1537. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1538. * since this indicates that nlinks count was previously 1.
  1539. */
  1540. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1541. {
  1542. inc_nlink(inode);
  1543. if (is_dx(inode) && inode->i_nlink > 1) {
  1544. /* limit is 16-bit i_links_count */
  1545. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1546. inode->i_nlink = 1;
  1547. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1548. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1549. }
  1550. }
  1551. }
  1552. /*
  1553. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1554. * directory has >EXT4_LINK_MAX subdirs.
  1555. */
  1556. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1557. {
  1558. drop_nlink(inode);
  1559. if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
  1560. inc_nlink(inode);
  1561. }
  1562. static int ext4_add_nondir(handle_t *handle,
  1563. struct dentry *dentry, struct inode *inode)
  1564. {
  1565. int err = ext4_add_entry(handle, dentry, inode);
  1566. if (!err) {
  1567. ext4_mark_inode_dirty(handle, inode);
  1568. d_instantiate(dentry, inode);
  1569. return 0;
  1570. }
  1571. drop_nlink(inode);
  1572. iput(inode);
  1573. return err;
  1574. }
  1575. /*
  1576. * By the time this is called, we already have created
  1577. * the directory cache entry for the new file, but it
  1578. * is so far negative - it has no inode.
  1579. *
  1580. * If the create succeeds, we fill in the inode information
  1581. * with d_instantiate().
  1582. */
  1583. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1584. struct nameidata *nd)
  1585. {
  1586. handle_t *handle;
  1587. struct inode *inode;
  1588. int err, retries = 0;
  1589. retry:
  1590. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1591. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1592. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1593. if (IS_ERR(handle))
  1594. return PTR_ERR(handle);
  1595. if (IS_DIRSYNC(dir))
  1596. handle->h_sync = 1;
  1597. inode = ext4_new_inode (handle, dir, mode);
  1598. err = PTR_ERR(inode);
  1599. if (!IS_ERR(inode)) {
  1600. inode->i_op = &ext4_file_inode_operations;
  1601. inode->i_fop = &ext4_file_operations;
  1602. ext4_set_aops(inode);
  1603. err = ext4_add_nondir(handle, dentry, inode);
  1604. }
  1605. ext4_journal_stop(handle);
  1606. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1607. goto retry;
  1608. return err;
  1609. }
  1610. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1611. int mode, dev_t rdev)
  1612. {
  1613. handle_t *handle;
  1614. struct inode *inode;
  1615. int err, retries = 0;
  1616. if (!new_valid_dev(rdev))
  1617. return -EINVAL;
  1618. retry:
  1619. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1620. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1621. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1622. if (IS_ERR(handle))
  1623. return PTR_ERR(handle);
  1624. if (IS_DIRSYNC(dir))
  1625. handle->h_sync = 1;
  1626. inode = ext4_new_inode(handle, dir, mode);
  1627. err = PTR_ERR(inode);
  1628. if (!IS_ERR(inode)) {
  1629. init_special_inode(inode, inode->i_mode, rdev);
  1630. #ifdef CONFIG_EXT4_FS_XATTR
  1631. inode->i_op = &ext4_special_inode_operations;
  1632. #endif
  1633. err = ext4_add_nondir(handle, dentry, inode);
  1634. }
  1635. ext4_journal_stop(handle);
  1636. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1637. goto retry;
  1638. return err;
  1639. }
  1640. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1641. {
  1642. handle_t *handle;
  1643. struct inode *inode;
  1644. struct buffer_head *dir_block;
  1645. struct ext4_dir_entry_2 *de;
  1646. int err, retries = 0;
  1647. if (EXT4_DIR_LINK_MAX(dir))
  1648. return -EMLINK;
  1649. retry:
  1650. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1651. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1652. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1653. if (IS_ERR(handle))
  1654. return PTR_ERR(handle);
  1655. if (IS_DIRSYNC(dir))
  1656. handle->h_sync = 1;
  1657. inode = ext4_new_inode(handle, dir, S_IFDIR | mode);
  1658. err = PTR_ERR(inode);
  1659. if (IS_ERR(inode))
  1660. goto out_stop;
  1661. inode->i_op = &ext4_dir_inode_operations;
  1662. inode->i_fop = &ext4_dir_operations;
  1663. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1664. dir_block = ext4_bread(handle, inode, 0, 1, &err);
  1665. if (!dir_block)
  1666. goto out_clear_inode;
  1667. BUFFER_TRACE(dir_block, "get_write_access");
  1668. ext4_journal_get_write_access(handle, dir_block);
  1669. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1670. de->inode = cpu_to_le32(inode->i_ino);
  1671. de->name_len = 1;
  1672. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len));
  1673. strcpy(de->name, ".");
  1674. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1675. de = ext4_next_entry(de);
  1676. de->inode = cpu_to_le32(dir->i_ino);
  1677. de->rec_len = ext4_rec_len_to_disk(inode->i_sb->s_blocksize -
  1678. EXT4_DIR_REC_LEN(1));
  1679. de->name_len = 2;
  1680. strcpy(de->name, "..");
  1681. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1682. inode->i_nlink = 2;
  1683. BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
  1684. ext4_journal_dirty_metadata(handle, dir_block);
  1685. brelse(dir_block);
  1686. ext4_mark_inode_dirty(handle, inode);
  1687. err = ext4_add_entry(handle, dentry, inode);
  1688. if (err) {
  1689. out_clear_inode:
  1690. clear_nlink(inode);
  1691. ext4_mark_inode_dirty(handle, inode);
  1692. iput(inode);
  1693. goto out_stop;
  1694. }
  1695. ext4_inc_count(handle, dir);
  1696. ext4_update_dx_flag(dir);
  1697. ext4_mark_inode_dirty(handle, dir);
  1698. d_instantiate(dentry, inode);
  1699. out_stop:
  1700. ext4_journal_stop(handle);
  1701. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1702. goto retry;
  1703. return err;
  1704. }
  1705. /*
  1706. * routine to check that the specified directory is empty (for rmdir)
  1707. */
  1708. static int empty_dir(struct inode *inode)
  1709. {
  1710. unsigned long offset;
  1711. struct buffer_head *bh;
  1712. struct ext4_dir_entry_2 *de, *de1;
  1713. struct super_block *sb;
  1714. int err = 0;
  1715. sb = inode->i_sb;
  1716. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1717. !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
  1718. if (err)
  1719. ext4_error(inode->i_sb, __func__,
  1720. "error %d reading directory #%lu offset 0",
  1721. err, inode->i_ino);
  1722. else
  1723. ext4_warning(inode->i_sb, __func__,
  1724. "bad directory (dir #%lu) - no data block",
  1725. inode->i_ino);
  1726. return 1;
  1727. }
  1728. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1729. de1 = ext4_next_entry(de);
  1730. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1731. !le32_to_cpu(de1->inode) ||
  1732. strcmp(".", de->name) ||
  1733. strcmp("..", de1->name)) {
  1734. ext4_warning(inode->i_sb, "empty_dir",
  1735. "bad directory (dir #%lu) - no `.' or `..'",
  1736. inode->i_ino);
  1737. brelse(bh);
  1738. return 1;
  1739. }
  1740. offset = ext4_rec_len_from_disk(de->rec_len) +
  1741. ext4_rec_len_from_disk(de1->rec_len);
  1742. de = ext4_next_entry(de1);
  1743. while (offset < inode->i_size) {
  1744. if (!bh ||
  1745. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1746. err = 0;
  1747. brelse(bh);
  1748. bh = ext4_bread(NULL, inode,
  1749. offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
  1750. if (!bh) {
  1751. if (err)
  1752. ext4_error(sb, __func__,
  1753. "error %d reading directory"
  1754. " #%lu offset %lu",
  1755. err, inode->i_ino, offset);
  1756. offset += sb->s_blocksize;
  1757. continue;
  1758. }
  1759. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1760. }
  1761. if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
  1762. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1763. sb->s_blocksize);
  1764. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1765. continue;
  1766. }
  1767. if (le32_to_cpu(de->inode)) {
  1768. brelse(bh);
  1769. return 0;
  1770. }
  1771. offset += ext4_rec_len_from_disk(de->rec_len);
  1772. de = ext4_next_entry(de);
  1773. }
  1774. brelse(bh);
  1775. return 1;
  1776. }
  1777. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1778. * such inodes, starting at the superblock, in case we crash before the
  1779. * file is closed/deleted, or in case the inode truncate spans multiple
  1780. * transactions and the last transaction is not recovered after a crash.
  1781. *
  1782. * At filesystem recovery time, we walk this list deleting unlinked
  1783. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1784. */
  1785. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1786. {
  1787. struct super_block *sb = inode->i_sb;
  1788. struct ext4_iloc iloc;
  1789. int err = 0, rc;
  1790. lock_super(sb);
  1791. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1792. goto out_unlock;
  1793. /* Orphan handling is only valid for files with data blocks
  1794. * being truncated, or files being unlinked. */
  1795. /* @@@ FIXME: Observation from aviro:
  1796. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1797. * here (on lock_super()), so race with ext4_link() which might bump
  1798. * ->i_nlink. For, say it, character device. Not a regular file,
  1799. * not a directory, not a symlink and ->i_nlink > 0.
  1800. */
  1801. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1802. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1803. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1804. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1805. if (err)
  1806. goto out_unlock;
  1807. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1808. if (err)
  1809. goto out_unlock;
  1810. /* Insert this inode at the head of the on-disk orphan list... */
  1811. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1812. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1813. err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  1814. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1815. if (!err)
  1816. err = rc;
  1817. /* Only add to the head of the in-memory list if all the
  1818. * previous operations succeeded. If the orphan_add is going to
  1819. * fail (possibly taking the journal offline), we can't risk
  1820. * leaving the inode on the orphan list: stray orphan-list
  1821. * entries can cause panics at unmount time.
  1822. *
  1823. * This is safe: on error we're going to ignore the orphan list
  1824. * anyway on the next recovery. */
  1825. if (!err)
  1826. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1827. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1828. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1829. inode->i_ino, NEXT_ORPHAN(inode));
  1830. out_unlock:
  1831. unlock_super(sb);
  1832. ext4_std_error(inode->i_sb, err);
  1833. return err;
  1834. }
  1835. /*
  1836. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1837. * of such inodes stored on disk, because it is finally being cleaned up.
  1838. */
  1839. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1840. {
  1841. struct list_head *prev;
  1842. struct ext4_inode_info *ei = EXT4_I(inode);
  1843. struct ext4_sb_info *sbi;
  1844. unsigned long ino_next;
  1845. struct ext4_iloc iloc;
  1846. int err = 0;
  1847. lock_super(inode->i_sb);
  1848. if (list_empty(&ei->i_orphan)) {
  1849. unlock_super(inode->i_sb);
  1850. return 0;
  1851. }
  1852. ino_next = NEXT_ORPHAN(inode);
  1853. prev = ei->i_orphan.prev;
  1854. sbi = EXT4_SB(inode->i_sb);
  1855. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1856. list_del_init(&ei->i_orphan);
  1857. /* If we're on an error path, we may not have a valid
  1858. * transaction handle with which to update the orphan list on
  1859. * disk, but we still need to remove the inode from the linked
  1860. * list in memory. */
  1861. if (!handle)
  1862. goto out;
  1863. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1864. if (err)
  1865. goto out_err;
  1866. if (prev == &sbi->s_orphan) {
  1867. jbd_debug(4, "superblock will point to %lu\n", ino_next);
  1868. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1869. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1870. if (err)
  1871. goto out_brelse;
  1872. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1873. err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
  1874. } else {
  1875. struct ext4_iloc iloc2;
  1876. struct inode *i_prev =
  1877. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1878. jbd_debug(4, "orphan inode %lu will point to %lu\n",
  1879. i_prev->i_ino, ino_next);
  1880. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1881. if (err)
  1882. goto out_brelse;
  1883. NEXT_ORPHAN(i_prev) = ino_next;
  1884. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1885. }
  1886. if (err)
  1887. goto out_brelse;
  1888. NEXT_ORPHAN(inode) = 0;
  1889. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1890. out_err:
  1891. ext4_std_error(inode->i_sb, err);
  1892. out:
  1893. unlock_super(inode->i_sb);
  1894. return err;
  1895. out_brelse:
  1896. brelse(iloc.bh);
  1897. goto out_err;
  1898. }
  1899. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  1900. {
  1901. int retval;
  1902. struct inode *inode;
  1903. struct buffer_head *bh;
  1904. struct ext4_dir_entry_2 *de;
  1905. handle_t *handle;
  1906. /* Initialize quotas before so that eventual writes go in
  1907. * separate transaction */
  1908. DQUOT_INIT(dentry->d_inode);
  1909. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1910. if (IS_ERR(handle))
  1911. return PTR_ERR(handle);
  1912. retval = -ENOENT;
  1913. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1914. if (!bh)
  1915. goto end_rmdir;
  1916. if (IS_DIRSYNC(dir))
  1917. handle->h_sync = 1;
  1918. inode = dentry->d_inode;
  1919. retval = -EIO;
  1920. if (le32_to_cpu(de->inode) != inode->i_ino)
  1921. goto end_rmdir;
  1922. retval = -ENOTEMPTY;
  1923. if (!empty_dir(inode))
  1924. goto end_rmdir;
  1925. retval = ext4_delete_entry(handle, dir, de, bh);
  1926. if (retval)
  1927. goto end_rmdir;
  1928. if (!EXT4_DIR_LINK_EMPTY(inode))
  1929. ext4_warning(inode->i_sb, "ext4_rmdir",
  1930. "empty directory has too many links (%d)",
  1931. inode->i_nlink);
  1932. inode->i_version++;
  1933. clear_nlink(inode);
  1934. /* There's no need to set i_disksize: the fact that i_nlink is
  1935. * zero will ensure that the right thing happens during any
  1936. * recovery. */
  1937. inode->i_size = 0;
  1938. ext4_orphan_add(handle, inode);
  1939. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  1940. ext4_mark_inode_dirty(handle, inode);
  1941. ext4_dec_count(handle, dir);
  1942. ext4_update_dx_flag(dir);
  1943. ext4_mark_inode_dirty(handle, dir);
  1944. end_rmdir:
  1945. ext4_journal_stop(handle);
  1946. brelse(bh);
  1947. return retval;
  1948. }
  1949. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  1950. {
  1951. int retval;
  1952. struct inode *inode;
  1953. struct buffer_head *bh;
  1954. struct ext4_dir_entry_2 *de;
  1955. handle_t *handle;
  1956. /* Initialize quotas before so that eventual writes go
  1957. * in separate transaction */
  1958. DQUOT_INIT(dentry->d_inode);
  1959. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1960. if (IS_ERR(handle))
  1961. return PTR_ERR(handle);
  1962. if (IS_DIRSYNC(dir))
  1963. handle->h_sync = 1;
  1964. retval = -ENOENT;
  1965. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1966. if (!bh)
  1967. goto end_unlink;
  1968. inode = dentry->d_inode;
  1969. retval = -EIO;
  1970. if (le32_to_cpu(de->inode) != inode->i_ino)
  1971. goto end_unlink;
  1972. if (!inode->i_nlink) {
  1973. ext4_warning(inode->i_sb, "ext4_unlink",
  1974. "Deleting nonexistent file (%lu), %d",
  1975. inode->i_ino, inode->i_nlink);
  1976. inode->i_nlink = 1;
  1977. }
  1978. retval = ext4_delete_entry(handle, dir, de, bh);
  1979. if (retval)
  1980. goto end_unlink;
  1981. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  1982. ext4_update_dx_flag(dir);
  1983. ext4_mark_inode_dirty(handle, dir);
  1984. drop_nlink(inode);
  1985. if (!inode->i_nlink)
  1986. ext4_orphan_add(handle, inode);
  1987. inode->i_ctime = ext4_current_time(inode);
  1988. ext4_mark_inode_dirty(handle, inode);
  1989. retval = 0;
  1990. end_unlink:
  1991. ext4_journal_stop(handle);
  1992. brelse(bh);
  1993. return retval;
  1994. }
  1995. static int ext4_symlink(struct inode *dir,
  1996. struct dentry *dentry, const char *symname)
  1997. {
  1998. handle_t *handle;
  1999. struct inode *inode;
  2000. int l, err, retries = 0;
  2001. l = strlen(symname)+1;
  2002. if (l > dir->i_sb->s_blocksize)
  2003. return -ENAMETOOLONG;
  2004. retry:
  2005. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2006. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
  2007. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  2008. if (IS_ERR(handle))
  2009. return PTR_ERR(handle);
  2010. if (IS_DIRSYNC(dir))
  2011. handle->h_sync = 1;
  2012. inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO);
  2013. err = PTR_ERR(inode);
  2014. if (IS_ERR(inode))
  2015. goto out_stop;
  2016. if (l > sizeof(EXT4_I(inode)->i_data)) {
  2017. inode->i_op = &ext4_symlink_inode_operations;
  2018. ext4_set_aops(inode);
  2019. /*
  2020. * page_symlink() calls into ext4_prepare/commit_write.
  2021. * We have a transaction open. All is sweetness. It also sets
  2022. * i_size in generic_commit_write().
  2023. */
  2024. err = __page_symlink(inode, symname, l,
  2025. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  2026. if (err) {
  2027. clear_nlink(inode);
  2028. ext4_mark_inode_dirty(handle, inode);
  2029. iput(inode);
  2030. goto out_stop;
  2031. }
  2032. } else {
  2033. /* clear the extent format for fast symlink */
  2034. EXT4_I(inode)->i_flags &= ~EXT4_EXTENTS_FL;
  2035. inode->i_op = &ext4_fast_symlink_inode_operations;
  2036. memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
  2037. inode->i_size = l-1;
  2038. }
  2039. EXT4_I(inode)->i_disksize = inode->i_size;
  2040. err = ext4_add_nondir(handle, dentry, inode);
  2041. out_stop:
  2042. ext4_journal_stop(handle);
  2043. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2044. goto retry;
  2045. return err;
  2046. }
  2047. static int ext4_link(struct dentry *old_dentry,
  2048. struct inode *dir, struct dentry *dentry)
  2049. {
  2050. handle_t *handle;
  2051. struct inode *inode = old_dentry->d_inode;
  2052. int err, retries = 0;
  2053. if (EXT4_DIR_LINK_MAX(inode))
  2054. return -EMLINK;
  2055. /*
  2056. * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
  2057. * otherwise has the potential to corrupt the orphan inode list.
  2058. */
  2059. if (inode->i_nlink == 0)
  2060. return -ENOENT;
  2061. retry:
  2062. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2063. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  2064. if (IS_ERR(handle))
  2065. return PTR_ERR(handle);
  2066. if (IS_DIRSYNC(dir))
  2067. handle->h_sync = 1;
  2068. inode->i_ctime = ext4_current_time(inode);
  2069. ext4_inc_count(handle, inode);
  2070. atomic_inc(&inode->i_count);
  2071. err = ext4_add_nondir(handle, dentry, inode);
  2072. ext4_journal_stop(handle);
  2073. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2074. goto retry;
  2075. return err;
  2076. }
  2077. #define PARENT_INO(buffer) \
  2078. (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer))->inode)
  2079. /*
  2080. * Anybody can rename anything with this: the permission checks are left to the
  2081. * higher-level routines.
  2082. */
  2083. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  2084. struct inode *new_dir, struct dentry *new_dentry)
  2085. {
  2086. handle_t *handle;
  2087. struct inode *old_inode, *new_inode;
  2088. struct buffer_head *old_bh, *new_bh, *dir_bh;
  2089. struct ext4_dir_entry_2 *old_de, *new_de;
  2090. int retval;
  2091. old_bh = new_bh = dir_bh = NULL;
  2092. /* Initialize quotas before so that eventual writes go
  2093. * in separate transaction */
  2094. if (new_dentry->d_inode)
  2095. DQUOT_INIT(new_dentry->d_inode);
  2096. handle = ext4_journal_start(old_dir, 2 *
  2097. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2098. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2099. if (IS_ERR(handle))
  2100. return PTR_ERR(handle);
  2101. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2102. handle->h_sync = 1;
  2103. old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
  2104. /*
  2105. * Check for inode number is _not_ due to possible IO errors.
  2106. * We might rmdir the source, keep it as pwd of some process
  2107. * and merrily kill the link to whatever was created under the
  2108. * same name. Goodbye sticky bit ;-<
  2109. */
  2110. old_inode = old_dentry->d_inode;
  2111. retval = -ENOENT;
  2112. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2113. goto end_rename;
  2114. new_inode = new_dentry->d_inode;
  2115. new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
  2116. if (new_bh) {
  2117. if (!new_inode) {
  2118. brelse(new_bh);
  2119. new_bh = NULL;
  2120. }
  2121. }
  2122. if (S_ISDIR(old_inode->i_mode)) {
  2123. if (new_inode) {
  2124. retval = -ENOTEMPTY;
  2125. if (!empty_dir(new_inode))
  2126. goto end_rename;
  2127. }
  2128. retval = -EIO;
  2129. dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
  2130. if (!dir_bh)
  2131. goto end_rename;
  2132. if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
  2133. goto end_rename;
  2134. retval = -EMLINK;
  2135. if (!new_inode && new_dir != old_dir &&
  2136. new_dir->i_nlink >= EXT4_LINK_MAX)
  2137. goto end_rename;
  2138. }
  2139. if (!new_bh) {
  2140. retval = ext4_add_entry(handle, new_dentry, old_inode);
  2141. if (retval)
  2142. goto end_rename;
  2143. } else {
  2144. BUFFER_TRACE(new_bh, "get write access");
  2145. ext4_journal_get_write_access(handle, new_bh);
  2146. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2147. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2148. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2149. new_de->file_type = old_de->file_type;
  2150. new_dir->i_version++;
  2151. new_dir->i_ctime = new_dir->i_mtime =
  2152. ext4_current_time(new_dir);
  2153. ext4_mark_inode_dirty(handle, new_dir);
  2154. BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
  2155. ext4_journal_dirty_metadata(handle, new_bh);
  2156. brelse(new_bh);
  2157. new_bh = NULL;
  2158. }
  2159. /*
  2160. * Like most other Unix systems, set the ctime for inodes on a
  2161. * rename.
  2162. */
  2163. old_inode->i_ctime = ext4_current_time(old_inode);
  2164. ext4_mark_inode_dirty(handle, old_inode);
  2165. /*
  2166. * ok, that's it
  2167. */
  2168. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2169. old_de->name_len != old_dentry->d_name.len ||
  2170. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2171. (retval = ext4_delete_entry(handle, old_dir,
  2172. old_de, old_bh)) == -ENOENT) {
  2173. /* old_de could have moved from under us during htree split, so
  2174. * make sure that we are deleting the right entry. We might
  2175. * also be pointing to a stale entry in the unused part of
  2176. * old_bh so just checking inum and the name isn't enough. */
  2177. struct buffer_head *old_bh2;
  2178. struct ext4_dir_entry_2 *old_de2;
  2179. old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
  2180. if (old_bh2) {
  2181. retval = ext4_delete_entry(handle, old_dir,
  2182. old_de2, old_bh2);
  2183. brelse(old_bh2);
  2184. }
  2185. }
  2186. if (retval) {
  2187. ext4_warning(old_dir->i_sb, "ext4_rename",
  2188. "Deleting old file (%lu), %d, error=%d",
  2189. old_dir->i_ino, old_dir->i_nlink, retval);
  2190. }
  2191. if (new_inode) {
  2192. ext4_dec_count(handle, new_inode);
  2193. new_inode->i_ctime = ext4_current_time(new_inode);
  2194. }
  2195. old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
  2196. ext4_update_dx_flag(old_dir);
  2197. if (dir_bh) {
  2198. BUFFER_TRACE(dir_bh, "get_write_access");
  2199. ext4_journal_get_write_access(handle, dir_bh);
  2200. PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
  2201. BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
  2202. ext4_journal_dirty_metadata(handle, dir_bh);
  2203. ext4_dec_count(handle, old_dir);
  2204. if (new_inode) {
  2205. /* checked empty_dir above, can't have another parent,
  2206. * ext4_dec_count() won't work for many-linked dirs */
  2207. new_inode->i_nlink = 0;
  2208. } else {
  2209. ext4_inc_count(handle, new_dir);
  2210. ext4_update_dx_flag(new_dir);
  2211. ext4_mark_inode_dirty(handle, new_dir);
  2212. }
  2213. }
  2214. ext4_mark_inode_dirty(handle, old_dir);
  2215. if (new_inode) {
  2216. ext4_mark_inode_dirty(handle, new_inode);
  2217. if (!new_inode->i_nlink)
  2218. ext4_orphan_add(handle, new_inode);
  2219. }
  2220. retval = 0;
  2221. end_rename:
  2222. brelse(dir_bh);
  2223. brelse(old_bh);
  2224. brelse(new_bh);
  2225. ext4_journal_stop(handle);
  2226. return retval;
  2227. }
  2228. /*
  2229. * directories can handle most operations...
  2230. */
  2231. const struct inode_operations ext4_dir_inode_operations = {
  2232. .create = ext4_create,
  2233. .lookup = ext4_lookup,
  2234. .link = ext4_link,
  2235. .unlink = ext4_unlink,
  2236. .symlink = ext4_symlink,
  2237. .mkdir = ext4_mkdir,
  2238. .rmdir = ext4_rmdir,
  2239. .mknod = ext4_mknod,
  2240. .rename = ext4_rename,
  2241. .setattr = ext4_setattr,
  2242. #ifdef CONFIG_EXT4_FS_XATTR
  2243. .setxattr = generic_setxattr,
  2244. .getxattr = generic_getxattr,
  2245. .listxattr = ext4_listxattr,
  2246. .removexattr = generic_removexattr,
  2247. #endif
  2248. .permission = ext4_permission,
  2249. };
  2250. const struct inode_operations ext4_special_inode_operations = {
  2251. .setattr = ext4_setattr,
  2252. #ifdef CONFIG_EXT4_FS_XATTR
  2253. .setxattr = generic_setxattr,
  2254. .getxattr = generic_getxattr,
  2255. .listxattr = ext4_listxattr,
  2256. .removexattr = generic_removexattr,
  2257. #endif
  2258. .permission = ext4_permission,
  2259. };