namei.c 67 KB

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