namei.c 67 KB

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