namei.c 68 KB

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