namei.c 71 KB

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