namei.c 81 KB

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