namei.c 66 KB

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