namei.c 66 KB

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