namei.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880
  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/time.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/quotaops.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/bio.h>
  35. #include "ext4.h"
  36. #include "ext4_jbd2.h"
  37. #include "xattr.h"
  38. #include "acl.h"
  39. #include <trace/events/ext4.h>
  40. /*
  41. * define how far ahead to read directories while searching them.
  42. */
  43. #define NAMEI_RA_CHUNKS 2
  44. #define NAMEI_RA_BLOCKS 4
  45. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  46. static struct buffer_head *ext4_append(handle_t *handle,
  47. struct inode *inode,
  48. ext4_lblk_t *block)
  49. {
  50. struct buffer_head *bh;
  51. int err;
  52. if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
  53. ((inode->i_size >> 10) >=
  54. EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
  55. return ERR_PTR(-ENOSPC);
  56. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  57. bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
  58. if (IS_ERR(bh))
  59. return bh;
  60. inode->i_size += inode->i_sb->s_blocksize;
  61. EXT4_I(inode)->i_disksize = inode->i_size;
  62. BUFFER_TRACE(bh, "get_write_access");
  63. err = ext4_journal_get_write_access(handle, bh);
  64. if (err) {
  65. brelse(bh);
  66. ext4_std_error(inode->i_sb, err);
  67. return ERR_PTR(err);
  68. }
  69. return bh;
  70. }
  71. static int ext4_dx_csum_verify(struct inode *inode,
  72. struct ext4_dir_entry *dirent);
  73. typedef enum {
  74. EITHER, INDEX, DIRENT
  75. } dirblock_type_t;
  76. #define ext4_read_dirblock(inode, block, type) \
  77. __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
  78. static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
  79. ext4_lblk_t block,
  80. dirblock_type_t type,
  81. const char *func,
  82. unsigned int line)
  83. {
  84. struct buffer_head *bh;
  85. struct ext4_dir_entry *dirent;
  86. int is_dx_block = 0;
  87. bh = ext4_bread(NULL, inode, block, 0);
  88. if (IS_ERR(bh)) {
  89. __ext4_warning(inode->i_sb, func, line,
  90. "inode #%lu: lblock %lu: comm %s: "
  91. "error %ld reading directory block",
  92. inode->i_ino, (unsigned long)block,
  93. current->comm, PTR_ERR(bh));
  94. return bh;
  95. }
  96. if (!bh) {
  97. ext4_error_inode(inode, func, line, block,
  98. "Directory hole found");
  99. return ERR_PTR(-EFSCORRUPTED);
  100. }
  101. dirent = (struct ext4_dir_entry *) bh->b_data;
  102. /* Determine whether or not we have an index block */
  103. if (is_dx(inode)) {
  104. if (block == 0)
  105. is_dx_block = 1;
  106. else if (ext4_rec_len_from_disk(dirent->rec_len,
  107. inode->i_sb->s_blocksize) ==
  108. inode->i_sb->s_blocksize)
  109. is_dx_block = 1;
  110. }
  111. if (!is_dx_block && type == INDEX) {
  112. ext4_error_inode(inode, func, line, block,
  113. "directory leaf block found instead of index block");
  114. return ERR_PTR(-EFSCORRUPTED);
  115. }
  116. if (!ext4_has_metadata_csum(inode->i_sb) ||
  117. buffer_verified(bh))
  118. return bh;
  119. /*
  120. * An empty leaf block can get mistaken for a index block; for
  121. * this reason, we can only check the index checksum when the
  122. * caller is sure it should be an index block.
  123. */
  124. if (is_dx_block && type == INDEX) {
  125. if (ext4_dx_csum_verify(inode, dirent))
  126. set_buffer_verified(bh);
  127. else {
  128. ext4_error_inode(inode, func, line, block,
  129. "Directory index failed checksum");
  130. brelse(bh);
  131. return ERR_PTR(-EFSBADCRC);
  132. }
  133. }
  134. if (!is_dx_block) {
  135. if (ext4_dirent_csum_verify(inode, dirent))
  136. set_buffer_verified(bh);
  137. else {
  138. ext4_error_inode(inode, func, line, block,
  139. "Directory block failed checksum");
  140. brelse(bh);
  141. return ERR_PTR(-EFSBADCRC);
  142. }
  143. }
  144. return bh;
  145. }
  146. #ifndef assert
  147. #define assert(test) J_ASSERT(test)
  148. #endif
  149. #ifdef DX_DEBUG
  150. #define dxtrace(command) command
  151. #else
  152. #define dxtrace(command)
  153. #endif
  154. struct fake_dirent
  155. {
  156. __le32 inode;
  157. __le16 rec_len;
  158. u8 name_len;
  159. u8 file_type;
  160. };
  161. struct dx_countlimit
  162. {
  163. __le16 limit;
  164. __le16 count;
  165. };
  166. struct dx_entry
  167. {
  168. __le32 hash;
  169. __le32 block;
  170. };
  171. /*
  172. * dx_root_info is laid out so that if it should somehow get overlaid by a
  173. * dirent the two low bits of the hash version will be zero. Therefore, the
  174. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  175. */
  176. struct dx_root
  177. {
  178. struct fake_dirent dot;
  179. char dot_name[4];
  180. struct fake_dirent dotdot;
  181. char dotdot_name[4];
  182. struct dx_root_info
  183. {
  184. __le32 reserved_zero;
  185. u8 hash_version;
  186. u8 info_length; /* 8 */
  187. u8 indirect_levels;
  188. u8 unused_flags;
  189. }
  190. info;
  191. struct dx_entry entries[0];
  192. };
  193. struct dx_node
  194. {
  195. struct fake_dirent fake;
  196. struct dx_entry entries[0];
  197. };
  198. struct dx_frame
  199. {
  200. struct buffer_head *bh;
  201. struct dx_entry *entries;
  202. struct dx_entry *at;
  203. };
  204. struct dx_map_entry
  205. {
  206. u32 hash;
  207. u16 offs;
  208. u16 size;
  209. };
  210. /*
  211. * This goes at the end of each htree block.
  212. */
  213. struct dx_tail {
  214. u32 dt_reserved;
  215. __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
  216. };
  217. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  218. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  219. static inline unsigned dx_get_hash(struct dx_entry *entry);
  220. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  221. static unsigned dx_get_count(struct dx_entry *entries);
  222. static unsigned dx_get_limit(struct dx_entry *entries);
  223. static void dx_set_count(struct dx_entry *entries, unsigned value);
  224. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  225. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  226. static unsigned dx_node_limit(struct inode *dir);
  227. static struct dx_frame *dx_probe(struct ext4_filename *fname,
  228. struct inode *dir,
  229. struct dx_hash_info *hinfo,
  230. struct dx_frame *frame);
  231. static void dx_release(struct dx_frame *frames);
  232. static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
  233. unsigned blocksize, struct dx_hash_info *hinfo,
  234. struct dx_map_entry map[]);
  235. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  236. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  237. struct dx_map_entry *offsets, int count, unsigned blocksize);
  238. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
  239. static void dx_insert_block(struct dx_frame *frame,
  240. u32 hash, ext4_lblk_t block);
  241. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  242. struct dx_frame *frame,
  243. struct dx_frame *frames,
  244. __u32 *start_hash);
  245. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  246. struct ext4_filename *fname,
  247. struct ext4_dir_entry_2 **res_dir);
  248. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  249. struct inode *dir, struct inode *inode);
  250. /* checksumming functions */
  251. void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
  252. unsigned int blocksize)
  253. {
  254. memset(t, 0, sizeof(struct ext4_dir_entry_tail));
  255. t->det_rec_len = ext4_rec_len_to_disk(
  256. sizeof(struct ext4_dir_entry_tail), blocksize);
  257. t->det_reserved_ft = EXT4_FT_DIR_CSUM;
  258. }
  259. /* Walk through a dirent block to find a checksum "dirent" at the tail */
  260. static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
  261. struct ext4_dir_entry *de)
  262. {
  263. struct ext4_dir_entry_tail *t;
  264. #ifdef PARANOID
  265. struct ext4_dir_entry *d, *top;
  266. d = de;
  267. top = (struct ext4_dir_entry *)(((void *)de) +
  268. (EXT4_BLOCK_SIZE(inode->i_sb) -
  269. sizeof(struct ext4_dir_entry_tail)));
  270. while (d < top && d->rec_len)
  271. d = (struct ext4_dir_entry *)(((void *)d) +
  272. le16_to_cpu(d->rec_len));
  273. if (d != top)
  274. return NULL;
  275. t = (struct ext4_dir_entry_tail *)d;
  276. #else
  277. t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
  278. #endif
  279. if (t->det_reserved_zero1 ||
  280. le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
  281. t->det_reserved_zero2 ||
  282. t->det_reserved_ft != EXT4_FT_DIR_CSUM)
  283. return NULL;
  284. return t;
  285. }
  286. static __le32 ext4_dirent_csum(struct inode *inode,
  287. struct ext4_dir_entry *dirent, int size)
  288. {
  289. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  290. struct ext4_inode_info *ei = EXT4_I(inode);
  291. __u32 csum;
  292. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  293. return cpu_to_le32(csum);
  294. }
  295. #define warn_no_space_for_csum(inode) \
  296. __warn_no_space_for_csum((inode), __func__, __LINE__)
  297. static void __warn_no_space_for_csum(struct inode *inode, const char *func,
  298. unsigned int line)
  299. {
  300. __ext4_warning_inode(inode, func, line,
  301. "No space for directory leaf checksum. Please run e2fsck -D.");
  302. }
  303. int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
  304. {
  305. struct ext4_dir_entry_tail *t;
  306. if (!ext4_has_metadata_csum(inode->i_sb))
  307. return 1;
  308. t = get_dirent_tail(inode, dirent);
  309. if (!t) {
  310. warn_no_space_for_csum(inode);
  311. return 0;
  312. }
  313. if (t->det_checksum != ext4_dirent_csum(inode, dirent,
  314. (void *)t - (void *)dirent))
  315. return 0;
  316. return 1;
  317. }
  318. static void ext4_dirent_csum_set(struct inode *inode,
  319. struct ext4_dir_entry *dirent)
  320. {
  321. struct ext4_dir_entry_tail *t;
  322. if (!ext4_has_metadata_csum(inode->i_sb))
  323. return;
  324. t = get_dirent_tail(inode, dirent);
  325. if (!t) {
  326. warn_no_space_for_csum(inode);
  327. return;
  328. }
  329. t->det_checksum = ext4_dirent_csum(inode, dirent,
  330. (void *)t - (void *)dirent);
  331. }
  332. int ext4_handle_dirty_dirent_node(handle_t *handle,
  333. struct inode *inode,
  334. struct buffer_head *bh)
  335. {
  336. ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  337. return ext4_handle_dirty_metadata(handle, inode, bh);
  338. }
  339. static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
  340. struct ext4_dir_entry *dirent,
  341. int *offset)
  342. {
  343. struct ext4_dir_entry *dp;
  344. struct dx_root_info *root;
  345. int count_offset;
  346. if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
  347. count_offset = 8;
  348. else if (le16_to_cpu(dirent->rec_len) == 12) {
  349. dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
  350. if (le16_to_cpu(dp->rec_len) !=
  351. EXT4_BLOCK_SIZE(inode->i_sb) - 12)
  352. return NULL;
  353. root = (struct dx_root_info *)(((void *)dp + 12));
  354. if (root->reserved_zero ||
  355. root->info_length != sizeof(struct dx_root_info))
  356. return NULL;
  357. count_offset = 32;
  358. } else
  359. return NULL;
  360. if (offset)
  361. *offset = count_offset;
  362. return (struct dx_countlimit *)(((void *)dirent) + count_offset);
  363. }
  364. static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
  365. int count_offset, int count, struct dx_tail *t)
  366. {
  367. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  368. struct ext4_inode_info *ei = EXT4_I(inode);
  369. __u32 csum;
  370. __le32 save_csum;
  371. int size;
  372. size = count_offset + (count * sizeof(struct dx_entry));
  373. save_csum = t->dt_checksum;
  374. t->dt_checksum = 0;
  375. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  376. csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
  377. t->dt_checksum = save_csum;
  378. return cpu_to_le32(csum);
  379. }
  380. static int ext4_dx_csum_verify(struct inode *inode,
  381. struct ext4_dir_entry *dirent)
  382. {
  383. struct dx_countlimit *c;
  384. struct dx_tail *t;
  385. int count_offset, limit, count;
  386. if (!ext4_has_metadata_csum(inode->i_sb))
  387. return 1;
  388. c = get_dx_countlimit(inode, dirent, &count_offset);
  389. if (!c) {
  390. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  391. return 1;
  392. }
  393. limit = le16_to_cpu(c->limit);
  394. count = le16_to_cpu(c->count);
  395. if (count_offset + (limit * sizeof(struct dx_entry)) >
  396. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  397. warn_no_space_for_csum(inode);
  398. return 1;
  399. }
  400. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  401. if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
  402. count, t))
  403. return 0;
  404. return 1;
  405. }
  406. static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
  407. {
  408. struct dx_countlimit *c;
  409. struct dx_tail *t;
  410. int count_offset, limit, count;
  411. if (!ext4_has_metadata_csum(inode->i_sb))
  412. return;
  413. c = get_dx_countlimit(inode, dirent, &count_offset);
  414. if (!c) {
  415. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  416. return;
  417. }
  418. limit = le16_to_cpu(c->limit);
  419. count = le16_to_cpu(c->count);
  420. if (count_offset + (limit * sizeof(struct dx_entry)) >
  421. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  422. warn_no_space_for_csum(inode);
  423. return;
  424. }
  425. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  426. t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
  427. }
  428. static inline int ext4_handle_dirty_dx_node(handle_t *handle,
  429. struct inode *inode,
  430. struct buffer_head *bh)
  431. {
  432. ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  433. return ext4_handle_dirty_metadata(handle, inode, bh);
  434. }
  435. /*
  436. * p is at least 6 bytes before the end of page
  437. */
  438. static inline struct ext4_dir_entry_2 *
  439. ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
  440. {
  441. return (struct ext4_dir_entry_2 *)((char *)p +
  442. ext4_rec_len_from_disk(p->rec_len, blocksize));
  443. }
  444. /*
  445. * Future: use high four bits of block for coalesce-on-delete flags
  446. * Mask them off for now.
  447. */
  448. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  449. {
  450. return le32_to_cpu(entry->block) & 0x00ffffff;
  451. }
  452. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  453. {
  454. entry->block = cpu_to_le32(value);
  455. }
  456. static inline unsigned dx_get_hash(struct dx_entry *entry)
  457. {
  458. return le32_to_cpu(entry->hash);
  459. }
  460. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  461. {
  462. entry->hash = cpu_to_le32(value);
  463. }
  464. static inline unsigned dx_get_count(struct dx_entry *entries)
  465. {
  466. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  467. }
  468. static inline unsigned dx_get_limit(struct dx_entry *entries)
  469. {
  470. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  471. }
  472. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  473. {
  474. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  475. }
  476. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  477. {
  478. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  479. }
  480. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  481. {
  482. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  483. EXT4_DIR_REC_LEN(2) - infosize;
  484. if (ext4_has_metadata_csum(dir->i_sb))
  485. entry_space -= sizeof(struct dx_tail);
  486. return entry_space / sizeof(struct dx_entry);
  487. }
  488. static inline unsigned dx_node_limit(struct inode *dir)
  489. {
  490. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  491. if (ext4_has_metadata_csum(dir->i_sb))
  492. entry_space -= sizeof(struct dx_tail);
  493. return entry_space / sizeof(struct dx_entry);
  494. }
  495. /*
  496. * Debug
  497. */
  498. #ifdef DX_DEBUG
  499. static void dx_show_index(char * label, struct dx_entry *entries)
  500. {
  501. int i, n = dx_get_count (entries);
  502. printk(KERN_DEBUG "%s index ", label);
  503. for (i = 0; i < n; i++) {
  504. printk("%x->%lu ", i ? dx_get_hash(entries + i) :
  505. 0, (unsigned long)dx_get_block(entries + i));
  506. }
  507. printk("\n");
  508. }
  509. struct stats
  510. {
  511. unsigned names;
  512. unsigned space;
  513. unsigned bcount;
  514. };
  515. static struct stats dx_show_leaf(struct inode *dir,
  516. struct dx_hash_info *hinfo,
  517. struct ext4_dir_entry_2 *de,
  518. int size, int show_names)
  519. {
  520. unsigned names = 0, space = 0;
  521. char *base = (char *) de;
  522. struct dx_hash_info h = *hinfo;
  523. printk("names: ");
  524. while ((char *) de < base + size)
  525. {
  526. if (de->inode)
  527. {
  528. if (show_names)
  529. {
  530. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  531. int len;
  532. char *name;
  533. struct ext4_str fname_crypto_str
  534. = {.name = NULL, .len = 0};
  535. int res = 0;
  536. name = de->name;
  537. len = de->name_len;
  538. if (ext4_encrypted_inode(inode))
  539. res = ext4_get_encryption_info(dir);
  540. if (res) {
  541. printk(KERN_WARNING "Error setting up"
  542. " fname crypto: %d\n", res);
  543. }
  544. if (ctx == NULL) {
  545. /* Directory is not encrypted */
  546. ext4fs_dirhash(de->name,
  547. de->name_len, &h);
  548. printk("%*.s:(U)%x.%u ", len,
  549. name, h.hash,
  550. (unsigned) ((char *) de
  551. - base));
  552. } else {
  553. /* Directory is encrypted */
  554. res = ext4_fname_crypto_alloc_buffer(
  555. ctx, de->name_len,
  556. &fname_crypto_str);
  557. if (res < 0) {
  558. printk(KERN_WARNING "Error "
  559. "allocating crypto "
  560. "buffer--skipping "
  561. "crypto\n");
  562. ctx = NULL;
  563. }
  564. res = ext4_fname_disk_to_usr(ctx, NULL, de,
  565. &fname_crypto_str);
  566. if (res < 0) {
  567. printk(KERN_WARNING "Error "
  568. "converting filename "
  569. "from disk to usr"
  570. "\n");
  571. name = "??";
  572. len = 2;
  573. } else {
  574. name = fname_crypto_str.name;
  575. len = fname_crypto_str.len;
  576. }
  577. ext4fs_dirhash(de->name, de->name_len,
  578. &h);
  579. printk("%*.s:(E)%x.%u ", len, name,
  580. h.hash, (unsigned) ((char *) de
  581. - base));
  582. ext4_fname_crypto_free_buffer(
  583. &fname_crypto_str);
  584. }
  585. #else
  586. int len = de->name_len;
  587. char *name = de->name;
  588. ext4fs_dirhash(de->name, de->name_len, &h);
  589. printk("%*.s:%x.%u ", len, name, h.hash,
  590. (unsigned) ((char *) de - base));
  591. #endif
  592. }
  593. space += EXT4_DIR_REC_LEN(de->name_len);
  594. names++;
  595. }
  596. de = ext4_next_entry(de, size);
  597. }
  598. printk("(%i)\n", names);
  599. return (struct stats) { names, space, 1 };
  600. }
  601. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  602. struct dx_entry *entries, int levels)
  603. {
  604. unsigned blocksize = dir->i_sb->s_blocksize;
  605. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  606. unsigned bcount = 0;
  607. struct buffer_head *bh;
  608. printk("%i indexed blocks...\n", count);
  609. for (i = 0; i < count; i++, entries++)
  610. {
  611. ext4_lblk_t block = dx_get_block(entries);
  612. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  613. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  614. struct stats stats;
  615. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  616. bh = ext4_bread(NULL,dir, block, 0);
  617. if (!bh || IS_ERR(bh))
  618. continue;
  619. stats = levels?
  620. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  621. dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
  622. bh->b_data, blocksize, 0);
  623. names += stats.names;
  624. space += stats.space;
  625. bcount += stats.bcount;
  626. brelse(bh);
  627. }
  628. if (bcount)
  629. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  630. levels ? "" : " ", names, space/bcount,
  631. (space/bcount)*100/blocksize);
  632. return (struct stats) { names, space, bcount};
  633. }
  634. #endif /* DX_DEBUG */
  635. /*
  636. * Probe for a directory leaf block to search.
  637. *
  638. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  639. * error in the directory index, and the caller should fall back to
  640. * searching the directory normally. The callers of dx_probe **MUST**
  641. * check for this error code, and make sure it never gets reflected
  642. * back to userspace.
  643. */
  644. static struct dx_frame *
  645. dx_probe(struct ext4_filename *fname, struct inode *dir,
  646. struct dx_hash_info *hinfo, struct dx_frame *frame_in)
  647. {
  648. unsigned count, indirect;
  649. struct dx_entry *at, *entries, *p, *q, *m;
  650. struct dx_root *root;
  651. struct dx_frame *frame = frame_in;
  652. struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
  653. u32 hash;
  654. frame->bh = ext4_read_dirblock(dir, 0, INDEX);
  655. if (IS_ERR(frame->bh))
  656. return (struct dx_frame *) frame->bh;
  657. root = (struct dx_root *) frame->bh->b_data;
  658. if (root->info.hash_version != DX_HASH_TEA &&
  659. root->info.hash_version != DX_HASH_HALF_MD4 &&
  660. root->info.hash_version != DX_HASH_LEGACY) {
  661. ext4_warning_inode(dir, "Unrecognised inode hash code %u",
  662. root->info.hash_version);
  663. goto fail;
  664. }
  665. if (fname)
  666. hinfo = &fname->hinfo;
  667. hinfo->hash_version = root->info.hash_version;
  668. if (hinfo->hash_version <= DX_HASH_TEA)
  669. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  670. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  671. if (fname && fname_name(fname))
  672. ext4fs_dirhash(fname_name(fname), fname_len(fname), hinfo);
  673. hash = hinfo->hash;
  674. if (root->info.unused_flags & 1) {
  675. ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
  676. root->info.unused_flags);
  677. goto fail;
  678. }
  679. indirect = root->info.indirect_levels;
  680. if (indirect > 1) {
  681. ext4_warning_inode(dir, "Unimplemented hash depth: %#06x",
  682. root->info.indirect_levels);
  683. goto fail;
  684. }
  685. entries = (struct dx_entry *)(((char *)&root->info) +
  686. root->info.info_length);
  687. if (dx_get_limit(entries) != dx_root_limit(dir,
  688. root->info.info_length)) {
  689. ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
  690. dx_get_limit(entries),
  691. dx_root_limit(dir, root->info.info_length));
  692. goto fail;
  693. }
  694. dxtrace(printk("Look up %x", hash));
  695. while (1) {
  696. count = dx_get_count(entries);
  697. if (!count || count > dx_get_limit(entries)) {
  698. ext4_warning_inode(dir,
  699. "dx entry: count %u beyond limit %u",
  700. count, dx_get_limit(entries));
  701. goto fail;
  702. }
  703. p = entries + 1;
  704. q = entries + count - 1;
  705. while (p <= q) {
  706. m = p + (q - p) / 2;
  707. dxtrace(printk("."));
  708. if (dx_get_hash(m) > hash)
  709. q = m - 1;
  710. else
  711. p = m + 1;
  712. }
  713. if (0) { // linear search cross check
  714. unsigned n = count - 1;
  715. at = entries;
  716. while (n--)
  717. {
  718. dxtrace(printk(","));
  719. if (dx_get_hash(++at) > hash)
  720. {
  721. at--;
  722. break;
  723. }
  724. }
  725. assert (at == p - 1);
  726. }
  727. at = p - 1;
  728. dxtrace(printk(" %x->%u\n", at == entries ? 0 : dx_get_hash(at),
  729. dx_get_block(at)));
  730. frame->entries = entries;
  731. frame->at = at;
  732. if (!indirect--)
  733. return frame;
  734. frame++;
  735. frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
  736. if (IS_ERR(frame->bh)) {
  737. ret_err = (struct dx_frame *) frame->bh;
  738. frame->bh = NULL;
  739. goto fail;
  740. }
  741. entries = ((struct dx_node *) frame->bh->b_data)->entries;
  742. if (dx_get_limit(entries) != dx_node_limit(dir)) {
  743. ext4_warning_inode(dir,
  744. "dx entry: limit %u != node limit %u",
  745. dx_get_limit(entries), dx_node_limit(dir));
  746. goto fail;
  747. }
  748. }
  749. fail:
  750. while (frame >= frame_in) {
  751. brelse(frame->bh);
  752. frame--;
  753. }
  754. if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
  755. ext4_warning_inode(dir,
  756. "Corrupt directory, running e2fsck is recommended");
  757. return ret_err;
  758. }
  759. static void dx_release(struct dx_frame *frames)
  760. {
  761. if (frames[0].bh == NULL)
  762. return;
  763. if (((struct dx_root *)frames[0].bh->b_data)->info.indirect_levels)
  764. brelse(frames[1].bh);
  765. brelse(frames[0].bh);
  766. }
  767. /*
  768. * This function increments the frame pointer to search the next leaf
  769. * block, and reads in the necessary intervening nodes if the search
  770. * should be necessary. Whether or not the search is necessary is
  771. * controlled by the hash parameter. If the hash value is even, then
  772. * the search is only continued if the next block starts with that
  773. * hash value. This is used if we are searching for a specific file.
  774. *
  775. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  776. *
  777. * This function returns 1 if the caller should continue to search,
  778. * or 0 if it should not. If there is an error reading one of the
  779. * index blocks, it will a negative error code.
  780. *
  781. * If start_hash is non-null, it will be filled in with the starting
  782. * hash of the next page.
  783. */
  784. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  785. struct dx_frame *frame,
  786. struct dx_frame *frames,
  787. __u32 *start_hash)
  788. {
  789. struct dx_frame *p;
  790. struct buffer_head *bh;
  791. int num_frames = 0;
  792. __u32 bhash;
  793. p = frame;
  794. /*
  795. * Find the next leaf page by incrementing the frame pointer.
  796. * If we run out of entries in the interior node, loop around and
  797. * increment pointer in the parent node. When we break out of
  798. * this loop, num_frames indicates the number of interior
  799. * nodes need to be read.
  800. */
  801. while (1) {
  802. if (++(p->at) < p->entries + dx_get_count(p->entries))
  803. break;
  804. if (p == frames)
  805. return 0;
  806. num_frames++;
  807. p--;
  808. }
  809. /*
  810. * If the hash is 1, then continue only if the next page has a
  811. * continuation hash of any value. This is used for readdir
  812. * handling. Otherwise, check to see if the hash matches the
  813. * desired contiuation hash. If it doesn't, return since
  814. * there's no point to read in the successive index pages.
  815. */
  816. bhash = dx_get_hash(p->at);
  817. if (start_hash)
  818. *start_hash = bhash;
  819. if ((hash & 1) == 0) {
  820. if ((bhash & ~1) != hash)
  821. return 0;
  822. }
  823. /*
  824. * If the hash is HASH_NB_ALWAYS, we always go to the next
  825. * block so no check is necessary
  826. */
  827. while (num_frames--) {
  828. bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
  829. if (IS_ERR(bh))
  830. return PTR_ERR(bh);
  831. p++;
  832. brelse(p->bh);
  833. p->bh = bh;
  834. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  835. }
  836. return 1;
  837. }
  838. /*
  839. * This function fills a red-black tree with information from a
  840. * directory block. It returns the number directory entries loaded
  841. * into the tree. If there is an error it is returned in err.
  842. */
  843. static int htree_dirblock_to_tree(struct file *dir_file,
  844. struct inode *dir, ext4_lblk_t block,
  845. struct dx_hash_info *hinfo,
  846. __u32 start_hash, __u32 start_minor_hash)
  847. {
  848. struct buffer_head *bh;
  849. struct ext4_dir_entry_2 *de, *top;
  850. int err = 0, count = 0;
  851. struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str;
  852. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  853. (unsigned long)block));
  854. bh = ext4_read_dirblock(dir, block, DIRENT);
  855. if (IS_ERR(bh))
  856. return PTR_ERR(bh);
  857. de = (struct ext4_dir_entry_2 *) bh->b_data;
  858. top = (struct ext4_dir_entry_2 *) ((char *) de +
  859. dir->i_sb->s_blocksize -
  860. EXT4_DIR_REC_LEN(0));
  861. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  862. /* Check if the directory is encrypted */
  863. if (ext4_encrypted_inode(dir)) {
  864. err = ext4_get_encryption_info(dir);
  865. if (err < 0) {
  866. brelse(bh);
  867. return err;
  868. }
  869. err = ext4_fname_crypto_alloc_buffer(dir, EXT4_NAME_LEN,
  870. &fname_crypto_str);
  871. if (err < 0) {
  872. brelse(bh);
  873. return err;
  874. }
  875. }
  876. #endif
  877. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  878. if (ext4_check_dir_entry(dir, NULL, de, bh,
  879. bh->b_data, bh->b_size,
  880. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  881. + ((char *)de - bh->b_data))) {
  882. /* silently ignore the rest of the block */
  883. break;
  884. }
  885. ext4fs_dirhash(de->name, de->name_len, hinfo);
  886. if ((hinfo->hash < start_hash) ||
  887. ((hinfo->hash == start_hash) &&
  888. (hinfo->minor_hash < start_minor_hash)))
  889. continue;
  890. if (de->inode == 0)
  891. continue;
  892. if (!ext4_encrypted_inode(dir)) {
  893. tmp_str.name = de->name;
  894. tmp_str.len = de->name_len;
  895. err = ext4_htree_store_dirent(dir_file,
  896. hinfo->hash, hinfo->minor_hash, de,
  897. &tmp_str);
  898. } else {
  899. int save_len = fname_crypto_str.len;
  900. /* Directory is encrypted */
  901. err = ext4_fname_disk_to_usr(dir, hinfo, de,
  902. &fname_crypto_str);
  903. if (err < 0) {
  904. count = err;
  905. goto errout;
  906. }
  907. err = ext4_htree_store_dirent(dir_file,
  908. hinfo->hash, hinfo->minor_hash, de,
  909. &fname_crypto_str);
  910. fname_crypto_str.len = save_len;
  911. }
  912. if (err != 0) {
  913. count = err;
  914. goto errout;
  915. }
  916. count++;
  917. }
  918. errout:
  919. brelse(bh);
  920. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  921. ext4_fname_crypto_free_buffer(&fname_crypto_str);
  922. #endif
  923. return count;
  924. }
  925. /*
  926. * This function fills a red-black tree with information from a
  927. * directory. We start scanning the directory in hash order, starting
  928. * at start_hash and start_minor_hash.
  929. *
  930. * This function returns the number of entries inserted into the tree,
  931. * or a negative error code.
  932. */
  933. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  934. __u32 start_minor_hash, __u32 *next_hash)
  935. {
  936. struct dx_hash_info hinfo;
  937. struct ext4_dir_entry_2 *de;
  938. struct dx_frame frames[2], *frame;
  939. struct inode *dir;
  940. ext4_lblk_t block;
  941. int count = 0;
  942. int ret, err;
  943. __u32 hashval;
  944. struct ext4_str tmp_str;
  945. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  946. start_hash, start_minor_hash));
  947. dir = file_inode(dir_file);
  948. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  949. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  950. if (hinfo.hash_version <= DX_HASH_TEA)
  951. hinfo.hash_version +=
  952. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  953. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  954. if (ext4_has_inline_data(dir)) {
  955. int has_inline_data = 1;
  956. count = htree_inlinedir_to_tree(dir_file, dir, 0,
  957. &hinfo, start_hash,
  958. start_minor_hash,
  959. &has_inline_data);
  960. if (has_inline_data) {
  961. *next_hash = ~0;
  962. return count;
  963. }
  964. }
  965. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  966. start_hash, start_minor_hash);
  967. *next_hash = ~0;
  968. return count;
  969. }
  970. hinfo.hash = start_hash;
  971. hinfo.minor_hash = 0;
  972. frame = dx_probe(NULL, dir, &hinfo, frames);
  973. if (IS_ERR(frame))
  974. return PTR_ERR(frame);
  975. /* Add '.' and '..' from the htree header */
  976. if (!start_hash && !start_minor_hash) {
  977. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  978. tmp_str.name = de->name;
  979. tmp_str.len = de->name_len;
  980. err = ext4_htree_store_dirent(dir_file, 0, 0,
  981. de, &tmp_str);
  982. if (err != 0)
  983. goto errout;
  984. count++;
  985. }
  986. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  987. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  988. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  989. tmp_str.name = de->name;
  990. tmp_str.len = de->name_len;
  991. err = ext4_htree_store_dirent(dir_file, 2, 0,
  992. de, &tmp_str);
  993. if (err != 0)
  994. goto errout;
  995. count++;
  996. }
  997. while (1) {
  998. block = dx_get_block(frame->at);
  999. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  1000. start_hash, start_minor_hash);
  1001. if (ret < 0) {
  1002. err = ret;
  1003. goto errout;
  1004. }
  1005. count += ret;
  1006. hashval = ~0;
  1007. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  1008. frame, frames, &hashval);
  1009. *next_hash = hashval;
  1010. if (ret < 0) {
  1011. err = ret;
  1012. goto errout;
  1013. }
  1014. /*
  1015. * Stop if: (a) there are no more entries, or
  1016. * (b) we have inserted at least one entry and the
  1017. * next hash value is not a continuation
  1018. */
  1019. if ((ret == 0) ||
  1020. (count && ((hashval & 1) == 0)))
  1021. break;
  1022. }
  1023. dx_release(frames);
  1024. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  1025. "next hash: %x\n", count, *next_hash));
  1026. return count;
  1027. errout:
  1028. dx_release(frames);
  1029. return (err);
  1030. }
  1031. static inline int search_dirblock(struct buffer_head *bh,
  1032. struct inode *dir,
  1033. struct ext4_filename *fname,
  1034. const struct qstr *d_name,
  1035. unsigned int offset,
  1036. struct ext4_dir_entry_2 **res_dir)
  1037. {
  1038. return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
  1039. fname, d_name, offset, res_dir);
  1040. }
  1041. /*
  1042. * Directory block splitting, compacting
  1043. */
  1044. /*
  1045. * Create map of hash values, offsets, and sizes, stored at end of block.
  1046. * Returns number of entries mapped.
  1047. */
  1048. static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
  1049. unsigned blocksize, struct dx_hash_info *hinfo,
  1050. struct dx_map_entry *map_tail)
  1051. {
  1052. int count = 0;
  1053. char *base = (char *) de;
  1054. struct dx_hash_info h = *hinfo;
  1055. while ((char *) de < base + blocksize) {
  1056. if (de->name_len && de->inode) {
  1057. ext4fs_dirhash(de->name, de->name_len, &h);
  1058. map_tail--;
  1059. map_tail->hash = h.hash;
  1060. map_tail->offs = ((char *) de - base)>>2;
  1061. map_tail->size = le16_to_cpu(de->rec_len);
  1062. count++;
  1063. cond_resched();
  1064. }
  1065. /* XXX: do we need to check rec_len == 0 case? -Chris */
  1066. de = ext4_next_entry(de, blocksize);
  1067. }
  1068. return count;
  1069. }
  1070. /* Sort map by hash value */
  1071. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  1072. {
  1073. struct dx_map_entry *p, *q, *top = map + count - 1;
  1074. int more;
  1075. /* Combsort until bubble sort doesn't suck */
  1076. while (count > 2) {
  1077. count = count*10/13;
  1078. if (count - 9 < 2) /* 9, 10 -> 11 */
  1079. count = 11;
  1080. for (p = top, q = p - count; q >= map; p--, q--)
  1081. if (p->hash < q->hash)
  1082. swap(*p, *q);
  1083. }
  1084. /* Garden variety bubble sort */
  1085. do {
  1086. more = 0;
  1087. q = top;
  1088. while (q-- > map) {
  1089. if (q[1].hash >= q[0].hash)
  1090. continue;
  1091. swap(*(q+1), *q);
  1092. more = 1;
  1093. }
  1094. } while(more);
  1095. }
  1096. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  1097. {
  1098. struct dx_entry *entries = frame->entries;
  1099. struct dx_entry *old = frame->at, *new = old + 1;
  1100. int count = dx_get_count(entries);
  1101. assert(count < dx_get_limit(entries));
  1102. assert(old < entries + count);
  1103. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  1104. dx_set_hash(new, hash);
  1105. dx_set_block(new, block);
  1106. dx_set_count(entries, count + 1);
  1107. }
  1108. /*
  1109. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  1110. *
  1111. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  1112. * `de != NULL' is guaranteed by caller.
  1113. */
  1114. static inline int ext4_match(struct ext4_filename *fname,
  1115. struct ext4_dir_entry_2 *de)
  1116. {
  1117. const void *name = fname_name(fname);
  1118. u32 len = fname_len(fname);
  1119. if (!de->inode)
  1120. return 0;
  1121. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1122. if (unlikely(!name)) {
  1123. if (fname->usr_fname->name[0] == '_') {
  1124. int ret;
  1125. if (de->name_len < 16)
  1126. return 0;
  1127. ret = memcmp(de->name + de->name_len - 16,
  1128. fname->crypto_buf.name + 8, 16);
  1129. return (ret == 0) ? 1 : 0;
  1130. }
  1131. name = fname->crypto_buf.name;
  1132. len = fname->crypto_buf.len;
  1133. }
  1134. #endif
  1135. if (de->name_len != len)
  1136. return 0;
  1137. return (memcmp(de->name, name, len) == 0) ? 1 : 0;
  1138. }
  1139. /*
  1140. * Returns 0 if not found, -1 on failure, and 1 on success
  1141. */
  1142. int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
  1143. struct inode *dir, struct ext4_filename *fname,
  1144. const struct qstr *d_name,
  1145. unsigned int offset, struct ext4_dir_entry_2 **res_dir)
  1146. {
  1147. struct ext4_dir_entry_2 * de;
  1148. char * dlimit;
  1149. int de_len;
  1150. int res;
  1151. de = (struct ext4_dir_entry_2 *)search_buf;
  1152. dlimit = search_buf + buf_size;
  1153. while ((char *) de < dlimit) {
  1154. /* this code is executed quadratically often */
  1155. /* do minimal checking `by hand' */
  1156. if ((char *) de + de->name_len <= dlimit) {
  1157. res = ext4_match(fname, de);
  1158. if (res < 0) {
  1159. res = -1;
  1160. goto return_result;
  1161. }
  1162. if (res > 0) {
  1163. /* found a match - just to be sure, do
  1164. * a full check */
  1165. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1166. bh->b_data,
  1167. bh->b_size, offset)) {
  1168. res = -1;
  1169. goto return_result;
  1170. }
  1171. *res_dir = de;
  1172. res = 1;
  1173. goto return_result;
  1174. }
  1175. }
  1176. /* prevent looping on a bad block */
  1177. de_len = ext4_rec_len_from_disk(de->rec_len,
  1178. dir->i_sb->s_blocksize);
  1179. if (de_len <= 0) {
  1180. res = -1;
  1181. goto return_result;
  1182. }
  1183. offset += de_len;
  1184. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  1185. }
  1186. res = 0;
  1187. return_result:
  1188. return res;
  1189. }
  1190. static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
  1191. struct ext4_dir_entry *de)
  1192. {
  1193. struct super_block *sb = dir->i_sb;
  1194. if (!is_dx(dir))
  1195. return 0;
  1196. if (block == 0)
  1197. return 1;
  1198. if (de->inode == 0 &&
  1199. ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
  1200. sb->s_blocksize)
  1201. return 1;
  1202. return 0;
  1203. }
  1204. /*
  1205. * ext4_find_entry()
  1206. *
  1207. * finds an entry in the specified directory with the wanted name. It
  1208. * returns the cache buffer in which the entry was found, and the entry
  1209. * itself (as a parameter - res_dir). It does NOT read the inode of the
  1210. * entry - you'll have to do that yourself if you want to.
  1211. *
  1212. * The returned buffer_head has ->b_count elevated. The caller is expected
  1213. * to brelse() it when appropriate.
  1214. */
  1215. static struct buffer_head * ext4_find_entry (struct inode *dir,
  1216. const struct qstr *d_name,
  1217. struct ext4_dir_entry_2 **res_dir,
  1218. int *inlined)
  1219. {
  1220. struct super_block *sb;
  1221. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  1222. struct buffer_head *bh, *ret = NULL;
  1223. ext4_lblk_t start, block, b;
  1224. const u8 *name = d_name->name;
  1225. int ra_max = 0; /* Number of bh's in the readahead
  1226. buffer, bh_use[] */
  1227. int ra_ptr = 0; /* Current index into readahead
  1228. buffer */
  1229. int num = 0;
  1230. ext4_lblk_t nblocks;
  1231. int i, namelen, retval;
  1232. struct ext4_filename fname;
  1233. *res_dir = NULL;
  1234. sb = dir->i_sb;
  1235. namelen = d_name->len;
  1236. if (namelen > EXT4_NAME_LEN)
  1237. return NULL;
  1238. retval = ext4_fname_setup_filename(dir, d_name, 1, &fname);
  1239. if (retval)
  1240. return ERR_PTR(retval);
  1241. if (ext4_has_inline_data(dir)) {
  1242. int has_inline_data = 1;
  1243. ret = ext4_find_inline_entry(dir, &fname, d_name, res_dir,
  1244. &has_inline_data);
  1245. if (has_inline_data) {
  1246. if (inlined)
  1247. *inlined = 1;
  1248. goto cleanup_and_exit;
  1249. }
  1250. }
  1251. if ((namelen <= 2) && (name[0] == '.') &&
  1252. (name[1] == '.' || name[1] == '\0')) {
  1253. /*
  1254. * "." or ".." will only be in the first block
  1255. * NFS may look up ".."; "." should be handled by the VFS
  1256. */
  1257. block = start = 0;
  1258. nblocks = 1;
  1259. goto restart;
  1260. }
  1261. if (is_dx(dir)) {
  1262. ret = ext4_dx_find_entry(dir, &fname, res_dir);
  1263. /*
  1264. * On success, or if the error was file not found,
  1265. * return. Otherwise, fall back to doing a search the
  1266. * old fashioned way.
  1267. */
  1268. if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
  1269. goto cleanup_and_exit;
  1270. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  1271. "falling back\n"));
  1272. }
  1273. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1274. start = EXT4_I(dir)->i_dir_start_lookup;
  1275. if (start >= nblocks)
  1276. start = 0;
  1277. block = start;
  1278. restart:
  1279. do {
  1280. /*
  1281. * We deal with the read-ahead logic here.
  1282. */
  1283. if (ra_ptr >= ra_max) {
  1284. /* Refill the readahead buffer */
  1285. ra_ptr = 0;
  1286. b = block;
  1287. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  1288. /*
  1289. * Terminate if we reach the end of the
  1290. * directory and must wrap, or if our
  1291. * search has finished at this block.
  1292. */
  1293. if (b >= nblocks || (num && block == start)) {
  1294. bh_use[ra_max] = NULL;
  1295. break;
  1296. }
  1297. num++;
  1298. bh = ext4_getblk(NULL, dir, b++, 0);
  1299. if (IS_ERR(bh)) {
  1300. if (ra_max == 0) {
  1301. ret = bh;
  1302. goto cleanup_and_exit;
  1303. }
  1304. break;
  1305. }
  1306. bh_use[ra_max] = bh;
  1307. if (bh)
  1308. ll_rw_block(READ | REQ_META | REQ_PRIO,
  1309. 1, &bh);
  1310. }
  1311. }
  1312. if ((bh = bh_use[ra_ptr++]) == NULL)
  1313. goto next;
  1314. wait_on_buffer(bh);
  1315. if (!buffer_uptodate(bh)) {
  1316. /* read error, skip block & hope for the best */
  1317. EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
  1318. (unsigned long) block);
  1319. brelse(bh);
  1320. goto next;
  1321. }
  1322. if (!buffer_verified(bh) &&
  1323. !is_dx_internal_node(dir, block,
  1324. (struct ext4_dir_entry *)bh->b_data) &&
  1325. !ext4_dirent_csum_verify(dir,
  1326. (struct ext4_dir_entry *)bh->b_data)) {
  1327. EXT4_ERROR_INODE(dir, "checksumming directory "
  1328. "block %lu", (unsigned long)block);
  1329. brelse(bh);
  1330. goto next;
  1331. }
  1332. set_buffer_verified(bh);
  1333. i = search_dirblock(bh, dir, &fname, d_name,
  1334. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  1335. if (i == 1) {
  1336. EXT4_I(dir)->i_dir_start_lookup = block;
  1337. ret = bh;
  1338. goto cleanup_and_exit;
  1339. } else {
  1340. brelse(bh);
  1341. if (i < 0)
  1342. goto cleanup_and_exit;
  1343. }
  1344. next:
  1345. if (++block >= nblocks)
  1346. block = 0;
  1347. } while (block != start);
  1348. /*
  1349. * If the directory has grown while we were searching, then
  1350. * search the last part of the directory before giving up.
  1351. */
  1352. block = nblocks;
  1353. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1354. if (block < nblocks) {
  1355. start = 0;
  1356. goto restart;
  1357. }
  1358. cleanup_and_exit:
  1359. /* Clean up the read-ahead blocks */
  1360. for (; ra_ptr < ra_max; ra_ptr++)
  1361. brelse(bh_use[ra_ptr]);
  1362. ext4_fname_free_filename(&fname);
  1363. return ret;
  1364. }
  1365. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  1366. struct ext4_filename *fname,
  1367. struct ext4_dir_entry_2 **res_dir)
  1368. {
  1369. struct super_block * sb = dir->i_sb;
  1370. struct dx_frame frames[2], *frame;
  1371. const struct qstr *d_name = fname->usr_fname;
  1372. struct buffer_head *bh;
  1373. ext4_lblk_t block;
  1374. int retval;
  1375. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1376. *res_dir = NULL;
  1377. #endif
  1378. frame = dx_probe(fname, dir, NULL, frames);
  1379. if (IS_ERR(frame))
  1380. return (struct buffer_head *) frame;
  1381. do {
  1382. block = dx_get_block(frame->at);
  1383. bh = ext4_read_dirblock(dir, block, DIRENT);
  1384. if (IS_ERR(bh))
  1385. goto errout;
  1386. retval = search_dirblock(bh, dir, fname, d_name,
  1387. block << EXT4_BLOCK_SIZE_BITS(sb),
  1388. res_dir);
  1389. if (retval == 1)
  1390. goto success;
  1391. brelse(bh);
  1392. if (retval == -1) {
  1393. bh = ERR_PTR(ERR_BAD_DX_DIR);
  1394. goto errout;
  1395. }
  1396. /* Check to see if we should continue to search */
  1397. retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
  1398. frames, NULL);
  1399. if (retval < 0) {
  1400. ext4_warning_inode(dir,
  1401. "error %d reading directory index block",
  1402. retval);
  1403. bh = ERR_PTR(retval);
  1404. goto errout;
  1405. }
  1406. } while (retval == 1);
  1407. bh = NULL;
  1408. errout:
  1409. dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
  1410. success:
  1411. dx_release(frames);
  1412. return bh;
  1413. }
  1414. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1415. {
  1416. struct inode *inode;
  1417. struct ext4_dir_entry_2 *de;
  1418. struct buffer_head *bh;
  1419. if (dentry->d_name.len > EXT4_NAME_LEN)
  1420. return ERR_PTR(-ENAMETOOLONG);
  1421. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  1422. if (IS_ERR(bh))
  1423. return (struct dentry *) bh;
  1424. inode = NULL;
  1425. if (bh) {
  1426. __u32 ino = le32_to_cpu(de->inode);
  1427. brelse(bh);
  1428. if (!ext4_valid_inum(dir->i_sb, ino)) {
  1429. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  1430. return ERR_PTR(-EFSCORRUPTED);
  1431. }
  1432. if (unlikely(ino == dir->i_ino)) {
  1433. EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
  1434. dentry);
  1435. return ERR_PTR(-EFSCORRUPTED);
  1436. }
  1437. inode = ext4_iget_normal(dir->i_sb, ino);
  1438. if (inode == ERR_PTR(-ESTALE)) {
  1439. EXT4_ERROR_INODE(dir,
  1440. "deleted inode referenced: %u",
  1441. ino);
  1442. return ERR_PTR(-EFSCORRUPTED);
  1443. }
  1444. if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
  1445. (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1446. S_ISLNK(inode->i_mode)) &&
  1447. !ext4_is_child_context_consistent_with_parent(dir,
  1448. inode)) {
  1449. iput(inode);
  1450. ext4_warning(inode->i_sb,
  1451. "Inconsistent encryption contexts: %lu/%lu\n",
  1452. (unsigned long) dir->i_ino,
  1453. (unsigned long) inode->i_ino);
  1454. return ERR_PTR(-EPERM);
  1455. }
  1456. }
  1457. return d_splice_alias(inode, dentry);
  1458. }
  1459. struct dentry *ext4_get_parent(struct dentry *child)
  1460. {
  1461. __u32 ino;
  1462. static const struct qstr dotdot = QSTR_INIT("..", 2);
  1463. struct ext4_dir_entry_2 * de;
  1464. struct buffer_head *bh;
  1465. bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
  1466. if (IS_ERR(bh))
  1467. return (struct dentry *) bh;
  1468. if (!bh)
  1469. return ERR_PTR(-ENOENT);
  1470. ino = le32_to_cpu(de->inode);
  1471. brelse(bh);
  1472. if (!ext4_valid_inum(d_inode(child)->i_sb, ino)) {
  1473. EXT4_ERROR_INODE(d_inode(child),
  1474. "bad parent inode number: %u", ino);
  1475. return ERR_PTR(-EFSCORRUPTED);
  1476. }
  1477. return d_obtain_alias(ext4_iget_normal(d_inode(child)->i_sb, ino));
  1478. }
  1479. /*
  1480. * Move count entries from end of map between two memory locations.
  1481. * Returns pointer to last entry moved.
  1482. */
  1483. static struct ext4_dir_entry_2 *
  1484. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1485. unsigned blocksize)
  1486. {
  1487. unsigned rec_len = 0;
  1488. while (count--) {
  1489. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1490. (from + (map->offs<<2));
  1491. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1492. memcpy (to, de, rec_len);
  1493. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1494. ext4_rec_len_to_disk(rec_len, blocksize);
  1495. de->inode = 0;
  1496. map++;
  1497. to += rec_len;
  1498. }
  1499. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1500. }
  1501. /*
  1502. * Compact each dir entry in the range to the minimal rec_len.
  1503. * Returns pointer to last entry in range.
  1504. */
  1505. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1506. {
  1507. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1508. unsigned rec_len = 0;
  1509. prev = to = de;
  1510. while ((char*)de < base + blocksize) {
  1511. next = ext4_next_entry(de, blocksize);
  1512. if (de->inode && de->name_len) {
  1513. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1514. if (de > to)
  1515. memmove(to, de, rec_len);
  1516. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1517. prev = to;
  1518. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1519. }
  1520. de = next;
  1521. }
  1522. return prev;
  1523. }
  1524. /*
  1525. * Split a full leaf block to make room for a new dir entry.
  1526. * Allocate a new block, and move entries so that they are approx. equally full.
  1527. * Returns pointer to de in block into which the new entry will be inserted.
  1528. */
  1529. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1530. struct buffer_head **bh,struct dx_frame *frame,
  1531. struct dx_hash_info *hinfo)
  1532. {
  1533. unsigned blocksize = dir->i_sb->s_blocksize;
  1534. unsigned count, continued;
  1535. struct buffer_head *bh2;
  1536. ext4_lblk_t newblock;
  1537. u32 hash2;
  1538. struct dx_map_entry *map;
  1539. char *data1 = (*bh)->b_data, *data2;
  1540. unsigned split, move, size;
  1541. struct ext4_dir_entry_2 *de = NULL, *de2;
  1542. struct ext4_dir_entry_tail *t;
  1543. int csum_size = 0;
  1544. int err = 0, i;
  1545. if (ext4_has_metadata_csum(dir->i_sb))
  1546. csum_size = sizeof(struct ext4_dir_entry_tail);
  1547. bh2 = ext4_append(handle, dir, &newblock);
  1548. if (IS_ERR(bh2)) {
  1549. brelse(*bh);
  1550. *bh = NULL;
  1551. return (struct ext4_dir_entry_2 *) bh2;
  1552. }
  1553. BUFFER_TRACE(*bh, "get_write_access");
  1554. err = ext4_journal_get_write_access(handle, *bh);
  1555. if (err)
  1556. goto journal_error;
  1557. BUFFER_TRACE(frame->bh, "get_write_access");
  1558. err = ext4_journal_get_write_access(handle, frame->bh);
  1559. if (err)
  1560. goto journal_error;
  1561. data2 = bh2->b_data;
  1562. /* create map in the end of data2 block */
  1563. map = (struct dx_map_entry *) (data2 + blocksize);
  1564. count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
  1565. blocksize, hinfo, map);
  1566. map -= count;
  1567. dx_sort_map(map, count);
  1568. /* Split the existing block in the middle, size-wise */
  1569. size = 0;
  1570. move = 0;
  1571. for (i = count-1; i >= 0; i--) {
  1572. /* is more than half of this entry in 2nd half of the block? */
  1573. if (size + map[i].size/2 > blocksize/2)
  1574. break;
  1575. size += map[i].size;
  1576. move++;
  1577. }
  1578. /* map index at which we will split */
  1579. split = count - move;
  1580. hash2 = map[split].hash;
  1581. continued = hash2 == map[split - 1].hash;
  1582. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1583. (unsigned long)dx_get_block(frame->at),
  1584. hash2, split, count-split));
  1585. /* Fancy dance to stay within two buffers */
  1586. de2 = dx_move_dirents(data1, data2, map + split, count - split,
  1587. blocksize);
  1588. de = dx_pack_dirents(data1, blocksize);
  1589. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1590. (char *) de,
  1591. blocksize);
  1592. de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
  1593. (char *) de2,
  1594. blocksize);
  1595. if (csum_size) {
  1596. t = EXT4_DIRENT_TAIL(data2, blocksize);
  1597. initialize_dirent_tail(t, blocksize);
  1598. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1599. initialize_dirent_tail(t, blocksize);
  1600. }
  1601. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
  1602. blocksize, 1));
  1603. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
  1604. blocksize, 1));
  1605. /* Which block gets the new entry? */
  1606. if (hinfo->hash >= hash2) {
  1607. swap(*bh, bh2);
  1608. de = de2;
  1609. }
  1610. dx_insert_block(frame, hash2 + continued, newblock);
  1611. err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
  1612. if (err)
  1613. goto journal_error;
  1614. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1615. if (err)
  1616. goto journal_error;
  1617. brelse(bh2);
  1618. dxtrace(dx_show_index("frame", frame->entries));
  1619. return de;
  1620. journal_error:
  1621. brelse(*bh);
  1622. brelse(bh2);
  1623. *bh = NULL;
  1624. ext4_std_error(dir->i_sb, err);
  1625. return ERR_PTR(err);
  1626. }
  1627. int ext4_find_dest_de(struct inode *dir, struct inode *inode,
  1628. struct buffer_head *bh,
  1629. void *buf, int buf_size,
  1630. struct ext4_filename *fname,
  1631. struct ext4_dir_entry_2 **dest_de)
  1632. {
  1633. struct ext4_dir_entry_2 *de;
  1634. unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
  1635. int nlen, rlen;
  1636. unsigned int offset = 0;
  1637. char *top;
  1638. int res;
  1639. de = (struct ext4_dir_entry_2 *)buf;
  1640. top = buf + buf_size - reclen;
  1641. while ((char *) de <= top) {
  1642. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1643. buf, buf_size, offset)) {
  1644. res = -EFSCORRUPTED;
  1645. goto return_result;
  1646. }
  1647. /* Provide crypto context and crypto buffer to ext4 match */
  1648. res = ext4_match(fname, de);
  1649. if (res < 0)
  1650. goto return_result;
  1651. if (res > 0) {
  1652. res = -EEXIST;
  1653. goto return_result;
  1654. }
  1655. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1656. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1657. if ((de->inode ? rlen - nlen : rlen) >= reclen)
  1658. break;
  1659. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1660. offset += rlen;
  1661. }
  1662. if ((char *) de > top)
  1663. res = -ENOSPC;
  1664. else {
  1665. *dest_de = de;
  1666. res = 0;
  1667. }
  1668. return_result:
  1669. return res;
  1670. }
  1671. int ext4_insert_dentry(struct inode *dir,
  1672. struct inode *inode,
  1673. struct ext4_dir_entry_2 *de,
  1674. int buf_size,
  1675. struct ext4_filename *fname)
  1676. {
  1677. int nlen, rlen;
  1678. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1679. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1680. if (de->inode) {
  1681. struct ext4_dir_entry_2 *de1 =
  1682. (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1683. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
  1684. de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
  1685. de = de1;
  1686. }
  1687. de->file_type = EXT4_FT_UNKNOWN;
  1688. de->inode = cpu_to_le32(inode->i_ino);
  1689. ext4_set_de_type(inode->i_sb, de, inode->i_mode);
  1690. de->name_len = fname_len(fname);
  1691. memcpy(de->name, fname_name(fname), fname_len(fname));
  1692. return 0;
  1693. }
  1694. /*
  1695. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1696. * it points to a directory entry which is guaranteed to be large
  1697. * enough for new directory entry. If de is NULL, then
  1698. * add_dirent_to_buf will attempt search the directory block for
  1699. * space. It will return -ENOSPC if no space is available, and -EIO
  1700. * and -EEXIST if directory entry already exists.
  1701. */
  1702. static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
  1703. struct inode *dir,
  1704. struct inode *inode, struct ext4_dir_entry_2 *de,
  1705. struct buffer_head *bh)
  1706. {
  1707. unsigned int blocksize = dir->i_sb->s_blocksize;
  1708. int csum_size = 0;
  1709. int err;
  1710. if (ext4_has_metadata_csum(inode->i_sb))
  1711. csum_size = sizeof(struct ext4_dir_entry_tail);
  1712. if (!de) {
  1713. err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
  1714. blocksize - csum_size, fname, &de);
  1715. if (err)
  1716. return err;
  1717. }
  1718. BUFFER_TRACE(bh, "get_write_access");
  1719. err = ext4_journal_get_write_access(handle, bh);
  1720. if (err) {
  1721. ext4_std_error(dir->i_sb, err);
  1722. return err;
  1723. }
  1724. /* By now the buffer is marked for journaling. Due to crypto operations,
  1725. * the following function call may fail */
  1726. err = ext4_insert_dentry(dir, inode, de, blocksize, fname);
  1727. if (err < 0)
  1728. return err;
  1729. /*
  1730. * XXX shouldn't update any times until successful
  1731. * completion of syscall, but too many callers depend
  1732. * on this.
  1733. *
  1734. * XXX similarly, too many callers depend on
  1735. * ext4_new_inode() setting the times, but error
  1736. * recovery deletes the inode, so the worst that can
  1737. * happen is that the times are slightly out of date
  1738. * and/or different from the directory change time.
  1739. */
  1740. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1741. ext4_update_dx_flag(dir);
  1742. dir->i_version++;
  1743. ext4_mark_inode_dirty(handle, dir);
  1744. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1745. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  1746. if (err)
  1747. ext4_std_error(dir->i_sb, err);
  1748. return 0;
  1749. }
  1750. /*
  1751. * This converts a one block unindexed directory to a 3 block indexed
  1752. * directory, and adds the dentry to the indexed directory.
  1753. */
  1754. static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
  1755. struct inode *dir,
  1756. struct inode *inode, struct buffer_head *bh)
  1757. {
  1758. struct buffer_head *bh2;
  1759. struct dx_root *root;
  1760. struct dx_frame frames[2], *frame;
  1761. struct dx_entry *entries;
  1762. struct ext4_dir_entry_2 *de, *de2;
  1763. struct ext4_dir_entry_tail *t;
  1764. char *data1, *top;
  1765. unsigned len;
  1766. int retval;
  1767. unsigned blocksize;
  1768. ext4_lblk_t block;
  1769. struct fake_dirent *fde;
  1770. int csum_size = 0;
  1771. if (ext4_has_metadata_csum(inode->i_sb))
  1772. csum_size = sizeof(struct ext4_dir_entry_tail);
  1773. blocksize = dir->i_sb->s_blocksize;
  1774. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1775. BUFFER_TRACE(bh, "get_write_access");
  1776. retval = ext4_journal_get_write_access(handle, bh);
  1777. if (retval) {
  1778. ext4_std_error(dir->i_sb, retval);
  1779. brelse(bh);
  1780. return retval;
  1781. }
  1782. root = (struct dx_root *) bh->b_data;
  1783. /* The 0th block becomes the root, move the dirents out */
  1784. fde = &root->dotdot;
  1785. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1786. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1787. if ((char *) de >= (((char *) root) + blocksize)) {
  1788. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1789. brelse(bh);
  1790. return -EFSCORRUPTED;
  1791. }
  1792. len = ((char *) root) + (blocksize - csum_size) - (char *) de;
  1793. /* Allocate new block for the 0th block's dirents */
  1794. bh2 = ext4_append(handle, dir, &block);
  1795. if (IS_ERR(bh2)) {
  1796. brelse(bh);
  1797. return PTR_ERR(bh2);
  1798. }
  1799. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1800. data1 = bh2->b_data;
  1801. memcpy (data1, de, len);
  1802. de = (struct ext4_dir_entry_2 *) data1;
  1803. top = data1 + len;
  1804. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1805. de = de2;
  1806. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1807. (char *) de,
  1808. blocksize);
  1809. if (csum_size) {
  1810. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1811. initialize_dirent_tail(t, blocksize);
  1812. }
  1813. /* Initialize the root; the dot dirents already exist */
  1814. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1815. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1816. blocksize);
  1817. memset (&root->info, 0, sizeof(root->info));
  1818. root->info.info_length = sizeof(root->info);
  1819. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1820. entries = root->entries;
  1821. dx_set_block(entries, 1);
  1822. dx_set_count(entries, 1);
  1823. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1824. /* Initialize as for dx_probe */
  1825. fname->hinfo.hash_version = root->info.hash_version;
  1826. if (fname->hinfo.hash_version <= DX_HASH_TEA)
  1827. fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1828. fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1829. ext4fs_dirhash(fname_name(fname), fname_len(fname), &fname->hinfo);
  1830. memset(frames, 0, sizeof(frames));
  1831. frame = frames;
  1832. frame->entries = entries;
  1833. frame->at = entries;
  1834. frame->bh = bh;
  1835. bh = bh2;
  1836. retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1837. if (retval)
  1838. goto out_frames;
  1839. retval = ext4_handle_dirty_dirent_node(handle, dir, bh);
  1840. if (retval)
  1841. goto out_frames;
  1842. de = do_split(handle,dir, &bh, frame, &fname->hinfo);
  1843. if (IS_ERR(de)) {
  1844. retval = PTR_ERR(de);
  1845. goto out_frames;
  1846. }
  1847. dx_release(frames);
  1848. retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
  1849. brelse(bh);
  1850. return retval;
  1851. out_frames:
  1852. /*
  1853. * Even if the block split failed, we have to properly write
  1854. * out all the changes we did so far. Otherwise we can end up
  1855. * with corrupted filesystem.
  1856. */
  1857. ext4_mark_inode_dirty(handle, dir);
  1858. dx_release(frames);
  1859. return retval;
  1860. }
  1861. /*
  1862. * ext4_add_entry()
  1863. *
  1864. * adds a file entry to the specified directory, using the same
  1865. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1866. *
  1867. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1868. * may not sleep between calling this and putting something into
  1869. * the entry, as someone else might have used it while you slept.
  1870. */
  1871. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1872. struct inode *inode)
  1873. {
  1874. struct inode *dir = d_inode(dentry->d_parent);
  1875. struct buffer_head *bh = NULL;
  1876. struct ext4_dir_entry_2 *de;
  1877. struct ext4_dir_entry_tail *t;
  1878. struct super_block *sb;
  1879. struct ext4_filename fname;
  1880. int retval;
  1881. int dx_fallback=0;
  1882. unsigned blocksize;
  1883. ext4_lblk_t block, blocks;
  1884. int csum_size = 0;
  1885. if (ext4_has_metadata_csum(inode->i_sb))
  1886. csum_size = sizeof(struct ext4_dir_entry_tail);
  1887. sb = dir->i_sb;
  1888. blocksize = sb->s_blocksize;
  1889. if (!dentry->d_name.len)
  1890. return -EINVAL;
  1891. retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
  1892. if (retval)
  1893. return retval;
  1894. if (ext4_has_inline_data(dir)) {
  1895. retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
  1896. if (retval < 0)
  1897. goto out;
  1898. if (retval == 1) {
  1899. retval = 0;
  1900. goto out;
  1901. }
  1902. }
  1903. if (is_dx(dir)) {
  1904. retval = ext4_dx_add_entry(handle, &fname, dir, inode);
  1905. if (!retval || (retval != ERR_BAD_DX_DIR))
  1906. goto out;
  1907. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1908. dx_fallback++;
  1909. ext4_mark_inode_dirty(handle, dir);
  1910. }
  1911. blocks = dir->i_size >> sb->s_blocksize_bits;
  1912. for (block = 0; block < blocks; block++) {
  1913. bh = ext4_read_dirblock(dir, block, DIRENT);
  1914. if (IS_ERR(bh)) {
  1915. retval = PTR_ERR(bh);
  1916. bh = NULL;
  1917. goto out;
  1918. }
  1919. retval = add_dirent_to_buf(handle, &fname, dir, inode,
  1920. NULL, bh);
  1921. if (retval != -ENOSPC)
  1922. goto out;
  1923. if (blocks == 1 && !dx_fallback &&
  1924. ext4_has_feature_dir_index(sb)) {
  1925. retval = make_indexed_dir(handle, &fname, dir,
  1926. inode, bh);
  1927. bh = NULL; /* make_indexed_dir releases bh */
  1928. goto out;
  1929. }
  1930. brelse(bh);
  1931. }
  1932. bh = ext4_append(handle, dir, &block);
  1933. if (IS_ERR(bh)) {
  1934. retval = PTR_ERR(bh);
  1935. bh = NULL;
  1936. goto out;
  1937. }
  1938. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1939. de->inode = 0;
  1940. de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
  1941. if (csum_size) {
  1942. t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
  1943. initialize_dirent_tail(t, blocksize);
  1944. }
  1945. retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
  1946. out:
  1947. ext4_fname_free_filename(&fname);
  1948. brelse(bh);
  1949. if (retval == 0)
  1950. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1951. return retval;
  1952. }
  1953. /*
  1954. * Returns 0 for success, or a negative error value
  1955. */
  1956. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  1957. struct inode *dir, struct inode *inode)
  1958. {
  1959. struct dx_frame frames[2], *frame;
  1960. struct dx_entry *entries, *at;
  1961. struct buffer_head *bh;
  1962. struct super_block *sb = dir->i_sb;
  1963. struct ext4_dir_entry_2 *de;
  1964. int err;
  1965. frame = dx_probe(fname, dir, NULL, frames);
  1966. if (IS_ERR(frame))
  1967. return PTR_ERR(frame);
  1968. entries = frame->entries;
  1969. at = frame->at;
  1970. bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
  1971. if (IS_ERR(bh)) {
  1972. err = PTR_ERR(bh);
  1973. bh = NULL;
  1974. goto cleanup;
  1975. }
  1976. BUFFER_TRACE(bh, "get_write_access");
  1977. err = ext4_journal_get_write_access(handle, bh);
  1978. if (err)
  1979. goto journal_error;
  1980. err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
  1981. if (err != -ENOSPC)
  1982. goto cleanup;
  1983. /* Block full, should compress but for now just split */
  1984. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1985. dx_get_count(entries), dx_get_limit(entries)));
  1986. /* Need to split index? */
  1987. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1988. ext4_lblk_t newblock;
  1989. unsigned icount = dx_get_count(entries);
  1990. int levels = frame - frames;
  1991. struct dx_entry *entries2;
  1992. struct dx_node *node2;
  1993. struct buffer_head *bh2;
  1994. if (levels && (dx_get_count(frames->entries) ==
  1995. dx_get_limit(frames->entries))) {
  1996. ext4_warning_inode(dir, "Directory index full!");
  1997. err = -ENOSPC;
  1998. goto cleanup;
  1999. }
  2000. bh2 = ext4_append(handle, dir, &newblock);
  2001. if (IS_ERR(bh2)) {
  2002. err = PTR_ERR(bh2);
  2003. goto cleanup;
  2004. }
  2005. node2 = (struct dx_node *)(bh2->b_data);
  2006. entries2 = node2->entries;
  2007. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  2008. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  2009. sb->s_blocksize);
  2010. BUFFER_TRACE(frame->bh, "get_write_access");
  2011. err = ext4_journal_get_write_access(handle, frame->bh);
  2012. if (err)
  2013. goto journal_error;
  2014. if (levels) {
  2015. unsigned icount1 = icount/2, icount2 = icount - icount1;
  2016. unsigned hash2 = dx_get_hash(entries + icount1);
  2017. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  2018. icount1, icount2));
  2019. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  2020. err = ext4_journal_get_write_access(handle,
  2021. frames[0].bh);
  2022. if (err)
  2023. goto journal_error;
  2024. memcpy((char *) entries2, (char *) (entries + icount1),
  2025. icount2 * sizeof(struct dx_entry));
  2026. dx_set_count(entries, icount1);
  2027. dx_set_count(entries2, icount2);
  2028. dx_set_limit(entries2, dx_node_limit(dir));
  2029. /* Which index block gets the new entry? */
  2030. if (at - entries >= icount1) {
  2031. frame->at = at = at - entries - icount1 + entries2;
  2032. frame->entries = entries = entries2;
  2033. swap(frame->bh, bh2);
  2034. }
  2035. dx_insert_block(frames + 0, hash2, newblock);
  2036. dxtrace(dx_show_index("node", frames[1].entries));
  2037. dxtrace(dx_show_index("node",
  2038. ((struct dx_node *) bh2->b_data)->entries));
  2039. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  2040. if (err)
  2041. goto journal_error;
  2042. brelse (bh2);
  2043. } else {
  2044. dxtrace(printk(KERN_DEBUG
  2045. "Creating second level index...\n"));
  2046. memcpy((char *) entries2, (char *) entries,
  2047. icount * sizeof(struct dx_entry));
  2048. dx_set_limit(entries2, dx_node_limit(dir));
  2049. /* Set up root */
  2050. dx_set_count(entries, 1);
  2051. dx_set_block(entries + 0, newblock);
  2052. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  2053. /* Add new access path frame */
  2054. frame = frames + 1;
  2055. frame->at = at = at - entries + entries2;
  2056. frame->entries = entries = entries2;
  2057. frame->bh = bh2;
  2058. err = ext4_journal_get_write_access(handle,
  2059. frame->bh);
  2060. if (err)
  2061. goto journal_error;
  2062. }
  2063. err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
  2064. if (err) {
  2065. ext4_std_error(inode->i_sb, err);
  2066. goto cleanup;
  2067. }
  2068. }
  2069. de = do_split(handle, dir, &bh, frame, &fname->hinfo);
  2070. if (IS_ERR(de)) {
  2071. err = PTR_ERR(de);
  2072. goto cleanup;
  2073. }
  2074. err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
  2075. goto cleanup;
  2076. journal_error:
  2077. ext4_std_error(dir->i_sb, err);
  2078. cleanup:
  2079. brelse(bh);
  2080. dx_release(frames);
  2081. return err;
  2082. }
  2083. /*
  2084. * ext4_generic_delete_entry deletes a directory entry by merging it
  2085. * with the previous entry
  2086. */
  2087. int ext4_generic_delete_entry(handle_t *handle,
  2088. struct inode *dir,
  2089. struct ext4_dir_entry_2 *de_del,
  2090. struct buffer_head *bh,
  2091. void *entry_buf,
  2092. int buf_size,
  2093. int csum_size)
  2094. {
  2095. struct ext4_dir_entry_2 *de, *pde;
  2096. unsigned int blocksize = dir->i_sb->s_blocksize;
  2097. int i;
  2098. i = 0;
  2099. pde = NULL;
  2100. de = (struct ext4_dir_entry_2 *)entry_buf;
  2101. while (i < buf_size - csum_size) {
  2102. if (ext4_check_dir_entry(dir, NULL, de, bh,
  2103. bh->b_data, bh->b_size, i))
  2104. return -EFSCORRUPTED;
  2105. if (de == de_del) {
  2106. if (pde)
  2107. pde->rec_len = ext4_rec_len_to_disk(
  2108. ext4_rec_len_from_disk(pde->rec_len,
  2109. blocksize) +
  2110. ext4_rec_len_from_disk(de->rec_len,
  2111. blocksize),
  2112. blocksize);
  2113. else
  2114. de->inode = 0;
  2115. dir->i_version++;
  2116. return 0;
  2117. }
  2118. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  2119. pde = de;
  2120. de = ext4_next_entry(de, blocksize);
  2121. }
  2122. return -ENOENT;
  2123. }
  2124. static int ext4_delete_entry(handle_t *handle,
  2125. struct inode *dir,
  2126. struct ext4_dir_entry_2 *de_del,
  2127. struct buffer_head *bh)
  2128. {
  2129. int err, csum_size = 0;
  2130. if (ext4_has_inline_data(dir)) {
  2131. int has_inline_data = 1;
  2132. err = ext4_delete_inline_entry(handle, dir, de_del, bh,
  2133. &has_inline_data);
  2134. if (has_inline_data)
  2135. return err;
  2136. }
  2137. if (ext4_has_metadata_csum(dir->i_sb))
  2138. csum_size = sizeof(struct ext4_dir_entry_tail);
  2139. BUFFER_TRACE(bh, "get_write_access");
  2140. err = ext4_journal_get_write_access(handle, bh);
  2141. if (unlikely(err))
  2142. goto out;
  2143. err = ext4_generic_delete_entry(handle, dir, de_del,
  2144. bh, bh->b_data,
  2145. dir->i_sb->s_blocksize, csum_size);
  2146. if (err)
  2147. goto out;
  2148. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  2149. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  2150. if (unlikely(err))
  2151. goto out;
  2152. return 0;
  2153. out:
  2154. if (err != -ENOENT)
  2155. ext4_std_error(dir->i_sb, err);
  2156. return err;
  2157. }
  2158. /*
  2159. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  2160. * since this indicates that nlinks count was previously 1.
  2161. */
  2162. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  2163. {
  2164. inc_nlink(inode);
  2165. if (is_dx(inode) && inode->i_nlink > 1) {
  2166. /* limit is 16-bit i_links_count */
  2167. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  2168. set_nlink(inode, 1);
  2169. ext4_set_feature_dir_nlink(inode->i_sb);
  2170. }
  2171. }
  2172. }
  2173. /*
  2174. * If a directory had nlink == 1, then we should let it be 1. This indicates
  2175. * directory has >EXT4_LINK_MAX subdirs.
  2176. */
  2177. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  2178. {
  2179. if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
  2180. drop_nlink(inode);
  2181. }
  2182. static int ext4_add_nondir(handle_t *handle,
  2183. struct dentry *dentry, struct inode *inode)
  2184. {
  2185. int err = ext4_add_entry(handle, dentry, inode);
  2186. if (!err) {
  2187. ext4_mark_inode_dirty(handle, inode);
  2188. unlock_new_inode(inode);
  2189. d_instantiate(dentry, inode);
  2190. return 0;
  2191. }
  2192. drop_nlink(inode);
  2193. unlock_new_inode(inode);
  2194. iput(inode);
  2195. return err;
  2196. }
  2197. /*
  2198. * By the time this is called, we already have created
  2199. * the directory cache entry for the new file, but it
  2200. * is so far negative - it has no inode.
  2201. *
  2202. * If the create succeeds, we fill in the inode information
  2203. * with d_instantiate().
  2204. */
  2205. static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2206. bool excl)
  2207. {
  2208. handle_t *handle;
  2209. struct inode *inode;
  2210. int err, credits, retries = 0;
  2211. err = dquot_initialize(dir);
  2212. if (err)
  2213. return err;
  2214. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2215. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2216. retry:
  2217. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2218. NULL, EXT4_HT_DIR, credits);
  2219. handle = ext4_journal_current_handle();
  2220. err = PTR_ERR(inode);
  2221. if (!IS_ERR(inode)) {
  2222. inode->i_op = &ext4_file_inode_operations;
  2223. inode->i_fop = &ext4_file_operations;
  2224. ext4_set_aops(inode);
  2225. err = ext4_add_nondir(handle, dentry, inode);
  2226. if (!err && IS_DIRSYNC(dir))
  2227. ext4_handle_sync(handle);
  2228. }
  2229. if (handle)
  2230. ext4_journal_stop(handle);
  2231. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2232. goto retry;
  2233. return err;
  2234. }
  2235. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  2236. umode_t mode, dev_t rdev)
  2237. {
  2238. handle_t *handle;
  2239. struct inode *inode;
  2240. int err, credits, retries = 0;
  2241. err = dquot_initialize(dir);
  2242. if (err)
  2243. return err;
  2244. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2245. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2246. retry:
  2247. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2248. NULL, EXT4_HT_DIR, credits);
  2249. handle = ext4_journal_current_handle();
  2250. err = PTR_ERR(inode);
  2251. if (!IS_ERR(inode)) {
  2252. init_special_inode(inode, inode->i_mode, rdev);
  2253. inode->i_op = &ext4_special_inode_operations;
  2254. err = ext4_add_nondir(handle, dentry, inode);
  2255. if (!err && IS_DIRSYNC(dir))
  2256. ext4_handle_sync(handle);
  2257. }
  2258. if (handle)
  2259. ext4_journal_stop(handle);
  2260. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2261. goto retry;
  2262. return err;
  2263. }
  2264. static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  2265. {
  2266. handle_t *handle;
  2267. struct inode *inode;
  2268. int err, retries = 0;
  2269. err = dquot_initialize(dir);
  2270. if (err)
  2271. return err;
  2272. retry:
  2273. inode = ext4_new_inode_start_handle(dir, mode,
  2274. NULL, 0, NULL,
  2275. EXT4_HT_DIR,
  2276. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2277. 4 + EXT4_XATTR_TRANS_BLOCKS);
  2278. handle = ext4_journal_current_handle();
  2279. err = PTR_ERR(inode);
  2280. if (!IS_ERR(inode)) {
  2281. inode->i_op = &ext4_file_inode_operations;
  2282. inode->i_fop = &ext4_file_operations;
  2283. ext4_set_aops(inode);
  2284. d_tmpfile(dentry, inode);
  2285. err = ext4_orphan_add(handle, inode);
  2286. if (err)
  2287. goto err_unlock_inode;
  2288. mark_inode_dirty(inode);
  2289. unlock_new_inode(inode);
  2290. }
  2291. if (handle)
  2292. ext4_journal_stop(handle);
  2293. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2294. goto retry;
  2295. return err;
  2296. err_unlock_inode:
  2297. ext4_journal_stop(handle);
  2298. unlock_new_inode(inode);
  2299. return err;
  2300. }
  2301. struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
  2302. struct ext4_dir_entry_2 *de,
  2303. int blocksize, int csum_size,
  2304. unsigned int parent_ino, int dotdot_real_len)
  2305. {
  2306. de->inode = cpu_to_le32(inode->i_ino);
  2307. de->name_len = 1;
  2308. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  2309. blocksize);
  2310. strcpy(de->name, ".");
  2311. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2312. de = ext4_next_entry(de, blocksize);
  2313. de->inode = cpu_to_le32(parent_ino);
  2314. de->name_len = 2;
  2315. if (!dotdot_real_len)
  2316. de->rec_len = ext4_rec_len_to_disk(blocksize -
  2317. (csum_size + EXT4_DIR_REC_LEN(1)),
  2318. blocksize);
  2319. else
  2320. de->rec_len = ext4_rec_len_to_disk(
  2321. EXT4_DIR_REC_LEN(de->name_len), blocksize);
  2322. strcpy(de->name, "..");
  2323. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2324. return ext4_next_entry(de, blocksize);
  2325. }
  2326. static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
  2327. struct inode *inode)
  2328. {
  2329. struct buffer_head *dir_block = NULL;
  2330. struct ext4_dir_entry_2 *de;
  2331. struct ext4_dir_entry_tail *t;
  2332. ext4_lblk_t block = 0;
  2333. unsigned int blocksize = dir->i_sb->s_blocksize;
  2334. int csum_size = 0;
  2335. int err;
  2336. if (ext4_has_metadata_csum(dir->i_sb))
  2337. csum_size = sizeof(struct ext4_dir_entry_tail);
  2338. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2339. err = ext4_try_create_inline_dir(handle, dir, inode);
  2340. if (err < 0 && err != -ENOSPC)
  2341. goto out;
  2342. if (!err)
  2343. goto out;
  2344. }
  2345. inode->i_size = 0;
  2346. dir_block = ext4_append(handle, inode, &block);
  2347. if (IS_ERR(dir_block))
  2348. return PTR_ERR(dir_block);
  2349. de = (struct ext4_dir_entry_2 *)dir_block->b_data;
  2350. ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
  2351. set_nlink(inode, 2);
  2352. if (csum_size) {
  2353. t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
  2354. initialize_dirent_tail(t, blocksize);
  2355. }
  2356. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  2357. err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  2358. if (err)
  2359. goto out;
  2360. set_buffer_verified(dir_block);
  2361. out:
  2362. brelse(dir_block);
  2363. return err;
  2364. }
  2365. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2366. {
  2367. handle_t *handle;
  2368. struct inode *inode;
  2369. int err, credits, retries = 0;
  2370. if (EXT4_DIR_LINK_MAX(dir))
  2371. return -EMLINK;
  2372. err = dquot_initialize(dir);
  2373. if (err)
  2374. return err;
  2375. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2376. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2377. retry:
  2378. inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
  2379. &dentry->d_name,
  2380. 0, NULL, EXT4_HT_DIR, credits);
  2381. handle = ext4_journal_current_handle();
  2382. err = PTR_ERR(inode);
  2383. if (IS_ERR(inode))
  2384. goto out_stop;
  2385. inode->i_op = &ext4_dir_inode_operations;
  2386. inode->i_fop = &ext4_dir_operations;
  2387. err = ext4_init_new_dir(handle, dir, inode);
  2388. if (err)
  2389. goto out_clear_inode;
  2390. err = ext4_mark_inode_dirty(handle, inode);
  2391. if (!err)
  2392. err = ext4_add_entry(handle, dentry, inode);
  2393. if (err) {
  2394. out_clear_inode:
  2395. clear_nlink(inode);
  2396. unlock_new_inode(inode);
  2397. ext4_mark_inode_dirty(handle, inode);
  2398. iput(inode);
  2399. goto out_stop;
  2400. }
  2401. ext4_inc_count(handle, dir);
  2402. ext4_update_dx_flag(dir);
  2403. err = ext4_mark_inode_dirty(handle, dir);
  2404. if (err)
  2405. goto out_clear_inode;
  2406. unlock_new_inode(inode);
  2407. d_instantiate(dentry, inode);
  2408. if (IS_DIRSYNC(dir))
  2409. ext4_handle_sync(handle);
  2410. out_stop:
  2411. if (handle)
  2412. ext4_journal_stop(handle);
  2413. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2414. goto retry;
  2415. return err;
  2416. }
  2417. /*
  2418. * routine to check that the specified directory is empty (for rmdir)
  2419. */
  2420. int ext4_empty_dir(struct inode *inode)
  2421. {
  2422. unsigned int offset;
  2423. struct buffer_head *bh;
  2424. struct ext4_dir_entry_2 *de, *de1;
  2425. struct super_block *sb;
  2426. int err = 0;
  2427. if (ext4_has_inline_data(inode)) {
  2428. int has_inline_data = 1;
  2429. err = empty_inline_dir(inode, &has_inline_data);
  2430. if (has_inline_data)
  2431. return err;
  2432. }
  2433. sb = inode->i_sb;
  2434. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
  2435. EXT4_ERROR_INODE(inode, "invalid size");
  2436. return 1;
  2437. }
  2438. bh = ext4_read_dirblock(inode, 0, EITHER);
  2439. if (IS_ERR(bh))
  2440. return 1;
  2441. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2442. de1 = ext4_next_entry(de, sb->s_blocksize);
  2443. if (le32_to_cpu(de->inode) != inode->i_ino ||
  2444. le32_to_cpu(de1->inode) == 0 ||
  2445. strcmp(".", de->name) || strcmp("..", de1->name)) {
  2446. ext4_warning_inode(inode, "directory missing '.' and/or '..'");
  2447. brelse(bh);
  2448. return 1;
  2449. }
  2450. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
  2451. ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
  2452. de = ext4_next_entry(de1, sb->s_blocksize);
  2453. while (offset < inode->i_size) {
  2454. if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  2455. unsigned int lblock;
  2456. err = 0;
  2457. brelse(bh);
  2458. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  2459. bh = ext4_read_dirblock(inode, lblock, EITHER);
  2460. if (IS_ERR(bh))
  2461. return 1;
  2462. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2463. }
  2464. if (ext4_check_dir_entry(inode, NULL, de, bh,
  2465. bh->b_data, bh->b_size, offset)) {
  2466. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  2467. sb->s_blocksize);
  2468. offset = (offset | (sb->s_blocksize - 1)) + 1;
  2469. continue;
  2470. }
  2471. if (le32_to_cpu(de->inode)) {
  2472. brelse(bh);
  2473. return 0;
  2474. }
  2475. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2476. de = ext4_next_entry(de, sb->s_blocksize);
  2477. }
  2478. brelse(bh);
  2479. return 1;
  2480. }
  2481. /*
  2482. * ext4_orphan_add() links an unlinked or truncated inode into a list of
  2483. * such inodes, starting at the superblock, in case we crash before the
  2484. * file is closed/deleted, or in case the inode truncate spans multiple
  2485. * transactions and the last transaction is not recovered after a crash.
  2486. *
  2487. * At filesystem recovery time, we walk this list deleting unlinked
  2488. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  2489. *
  2490. * Orphan list manipulation functions must be called under i_mutex unless
  2491. * we are just creating the inode or deleting it.
  2492. */
  2493. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  2494. {
  2495. struct super_block *sb = inode->i_sb;
  2496. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2497. struct ext4_iloc iloc;
  2498. int err = 0, rc;
  2499. bool dirty = false;
  2500. if (!sbi->s_journal || is_bad_inode(inode))
  2501. return 0;
  2502. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  2503. !inode_is_locked(inode));
  2504. /*
  2505. * Exit early if inode already is on orphan list. This is a big speedup
  2506. * since we don't have to contend on the global s_orphan_lock.
  2507. */
  2508. if (!list_empty(&EXT4_I(inode)->i_orphan))
  2509. return 0;
  2510. /*
  2511. * Orphan handling is only valid for files with data blocks
  2512. * being truncated, or files being unlinked. Note that we either
  2513. * hold i_mutex, or the inode can not be referenced from outside,
  2514. * so i_nlink should not be bumped due to race
  2515. */
  2516. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  2517. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  2518. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2519. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2520. if (err)
  2521. goto out;
  2522. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2523. if (err)
  2524. goto out;
  2525. mutex_lock(&sbi->s_orphan_lock);
  2526. /*
  2527. * Due to previous errors inode may be already a part of on-disk
  2528. * orphan list. If so skip on-disk list modification.
  2529. */
  2530. if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
  2531. (le32_to_cpu(sbi->s_es->s_inodes_count))) {
  2532. /* Insert this inode at the head of the on-disk orphan list */
  2533. NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
  2534. sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  2535. dirty = true;
  2536. }
  2537. list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
  2538. mutex_unlock(&sbi->s_orphan_lock);
  2539. if (dirty) {
  2540. err = ext4_handle_dirty_super(handle, sb);
  2541. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2542. if (!err)
  2543. err = rc;
  2544. if (err) {
  2545. /*
  2546. * We have to remove inode from in-memory list if
  2547. * addition to on disk orphan list failed. Stray orphan
  2548. * list entries can cause panics at unmount time.
  2549. */
  2550. mutex_lock(&sbi->s_orphan_lock);
  2551. list_del(&EXT4_I(inode)->i_orphan);
  2552. mutex_unlock(&sbi->s_orphan_lock);
  2553. }
  2554. }
  2555. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  2556. jbd_debug(4, "orphan inode %lu will point to %d\n",
  2557. inode->i_ino, NEXT_ORPHAN(inode));
  2558. out:
  2559. ext4_std_error(sb, err);
  2560. return err;
  2561. }
  2562. /*
  2563. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  2564. * of such inodes stored on disk, because it is finally being cleaned up.
  2565. */
  2566. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  2567. {
  2568. struct list_head *prev;
  2569. struct ext4_inode_info *ei = EXT4_I(inode);
  2570. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2571. __u32 ino_next;
  2572. struct ext4_iloc iloc;
  2573. int err = 0;
  2574. if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
  2575. return 0;
  2576. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  2577. !inode_is_locked(inode));
  2578. /* Do this quick check before taking global s_orphan_lock. */
  2579. if (list_empty(&ei->i_orphan))
  2580. return 0;
  2581. if (handle) {
  2582. /* Grab inode buffer early before taking global s_orphan_lock */
  2583. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2584. }
  2585. mutex_lock(&sbi->s_orphan_lock);
  2586. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  2587. prev = ei->i_orphan.prev;
  2588. list_del_init(&ei->i_orphan);
  2589. /* If we're on an error path, we may not have a valid
  2590. * transaction handle with which to update the orphan list on
  2591. * disk, but we still need to remove the inode from the linked
  2592. * list in memory. */
  2593. if (!handle || err) {
  2594. mutex_unlock(&sbi->s_orphan_lock);
  2595. goto out_err;
  2596. }
  2597. ino_next = NEXT_ORPHAN(inode);
  2598. if (prev == &sbi->s_orphan) {
  2599. jbd_debug(4, "superblock will point to %u\n", ino_next);
  2600. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2601. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2602. if (err) {
  2603. mutex_unlock(&sbi->s_orphan_lock);
  2604. goto out_brelse;
  2605. }
  2606. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  2607. mutex_unlock(&sbi->s_orphan_lock);
  2608. err = ext4_handle_dirty_super(handle, inode->i_sb);
  2609. } else {
  2610. struct ext4_iloc iloc2;
  2611. struct inode *i_prev =
  2612. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  2613. jbd_debug(4, "orphan inode %lu will point to %u\n",
  2614. i_prev->i_ino, ino_next);
  2615. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  2616. if (err) {
  2617. mutex_unlock(&sbi->s_orphan_lock);
  2618. goto out_brelse;
  2619. }
  2620. NEXT_ORPHAN(i_prev) = ino_next;
  2621. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  2622. mutex_unlock(&sbi->s_orphan_lock);
  2623. }
  2624. if (err)
  2625. goto out_brelse;
  2626. NEXT_ORPHAN(inode) = 0;
  2627. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2628. out_err:
  2629. ext4_std_error(inode->i_sb, err);
  2630. return err;
  2631. out_brelse:
  2632. brelse(iloc.bh);
  2633. goto out_err;
  2634. }
  2635. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  2636. {
  2637. int retval;
  2638. struct inode *inode;
  2639. struct buffer_head *bh;
  2640. struct ext4_dir_entry_2 *de;
  2641. handle_t *handle = NULL;
  2642. /* Initialize quotas before so that eventual writes go in
  2643. * separate transaction */
  2644. retval = dquot_initialize(dir);
  2645. if (retval)
  2646. return retval;
  2647. retval = dquot_initialize(d_inode(dentry));
  2648. if (retval)
  2649. return retval;
  2650. retval = -ENOENT;
  2651. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2652. if (IS_ERR(bh))
  2653. return PTR_ERR(bh);
  2654. if (!bh)
  2655. goto end_rmdir;
  2656. inode = d_inode(dentry);
  2657. retval = -EFSCORRUPTED;
  2658. if (le32_to_cpu(de->inode) != inode->i_ino)
  2659. goto end_rmdir;
  2660. retval = -ENOTEMPTY;
  2661. if (!ext4_empty_dir(inode))
  2662. goto end_rmdir;
  2663. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2664. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2665. if (IS_ERR(handle)) {
  2666. retval = PTR_ERR(handle);
  2667. handle = NULL;
  2668. goto end_rmdir;
  2669. }
  2670. if (IS_DIRSYNC(dir))
  2671. ext4_handle_sync(handle);
  2672. retval = ext4_delete_entry(handle, dir, de, bh);
  2673. if (retval)
  2674. goto end_rmdir;
  2675. if (!EXT4_DIR_LINK_EMPTY(inode))
  2676. ext4_warning_inode(inode,
  2677. "empty directory '%.*s' has too many links (%u)",
  2678. dentry->d_name.len, dentry->d_name.name,
  2679. inode->i_nlink);
  2680. inode->i_version++;
  2681. clear_nlink(inode);
  2682. /* There's no need to set i_disksize: the fact that i_nlink is
  2683. * zero will ensure that the right thing happens during any
  2684. * recovery. */
  2685. inode->i_size = 0;
  2686. ext4_orphan_add(handle, inode);
  2687. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  2688. ext4_mark_inode_dirty(handle, inode);
  2689. ext4_dec_count(handle, dir);
  2690. ext4_update_dx_flag(dir);
  2691. ext4_mark_inode_dirty(handle, dir);
  2692. end_rmdir:
  2693. brelse(bh);
  2694. if (handle)
  2695. ext4_journal_stop(handle);
  2696. return retval;
  2697. }
  2698. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  2699. {
  2700. int retval;
  2701. struct inode *inode;
  2702. struct buffer_head *bh;
  2703. struct ext4_dir_entry_2 *de;
  2704. handle_t *handle = NULL;
  2705. trace_ext4_unlink_enter(dir, dentry);
  2706. /* Initialize quotas before so that eventual writes go
  2707. * in separate transaction */
  2708. retval = dquot_initialize(dir);
  2709. if (retval)
  2710. return retval;
  2711. retval = dquot_initialize(d_inode(dentry));
  2712. if (retval)
  2713. return retval;
  2714. retval = -ENOENT;
  2715. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2716. if (IS_ERR(bh))
  2717. return PTR_ERR(bh);
  2718. if (!bh)
  2719. goto end_unlink;
  2720. inode = d_inode(dentry);
  2721. retval = -EFSCORRUPTED;
  2722. if (le32_to_cpu(de->inode) != inode->i_ino)
  2723. goto end_unlink;
  2724. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2725. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2726. if (IS_ERR(handle)) {
  2727. retval = PTR_ERR(handle);
  2728. handle = NULL;
  2729. goto end_unlink;
  2730. }
  2731. if (IS_DIRSYNC(dir))
  2732. ext4_handle_sync(handle);
  2733. if (inode->i_nlink == 0) {
  2734. ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
  2735. dentry->d_name.len, dentry->d_name.name);
  2736. set_nlink(inode, 1);
  2737. }
  2738. retval = ext4_delete_entry(handle, dir, de, bh);
  2739. if (retval)
  2740. goto end_unlink;
  2741. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  2742. ext4_update_dx_flag(dir);
  2743. ext4_mark_inode_dirty(handle, dir);
  2744. drop_nlink(inode);
  2745. if (!inode->i_nlink)
  2746. ext4_orphan_add(handle, inode);
  2747. inode->i_ctime = ext4_current_time(inode);
  2748. ext4_mark_inode_dirty(handle, inode);
  2749. end_unlink:
  2750. brelse(bh);
  2751. if (handle)
  2752. ext4_journal_stop(handle);
  2753. trace_ext4_unlink_exit(dentry, retval);
  2754. return retval;
  2755. }
  2756. static int ext4_symlink(struct inode *dir,
  2757. struct dentry *dentry, const char *symname)
  2758. {
  2759. handle_t *handle;
  2760. struct inode *inode;
  2761. int err, len = strlen(symname);
  2762. int credits;
  2763. bool encryption_required;
  2764. struct ext4_str disk_link;
  2765. struct ext4_encrypted_symlink_data *sd = NULL;
  2766. disk_link.len = len + 1;
  2767. disk_link.name = (char *) symname;
  2768. encryption_required = (ext4_encrypted_inode(dir) ||
  2769. DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)));
  2770. if (encryption_required) {
  2771. err = ext4_get_encryption_info(dir);
  2772. if (err)
  2773. return err;
  2774. if (ext4_encryption_info(dir) == NULL)
  2775. return -EPERM;
  2776. disk_link.len = (ext4_fname_encrypted_size(dir, len) +
  2777. sizeof(struct ext4_encrypted_symlink_data));
  2778. sd = kzalloc(disk_link.len, GFP_KERNEL);
  2779. if (!sd)
  2780. return -ENOMEM;
  2781. }
  2782. if (disk_link.len > dir->i_sb->s_blocksize) {
  2783. err = -ENAMETOOLONG;
  2784. goto err_free_sd;
  2785. }
  2786. err = dquot_initialize(dir);
  2787. if (err)
  2788. goto err_free_sd;
  2789. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  2790. /*
  2791. * For non-fast symlinks, we just allocate inode and put it on
  2792. * orphan list in the first transaction => we need bitmap,
  2793. * group descriptor, sb, inode block, quota blocks, and
  2794. * possibly selinux xattr blocks.
  2795. */
  2796. credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2797. EXT4_XATTR_TRANS_BLOCKS;
  2798. } else {
  2799. /*
  2800. * Fast symlink. We have to add entry to directory
  2801. * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
  2802. * allocate new inode (bitmap, group descriptor, inode block,
  2803. * quota blocks, sb is already counted in previous macros).
  2804. */
  2805. credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2806. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
  2807. }
  2808. inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
  2809. &dentry->d_name, 0, NULL,
  2810. EXT4_HT_DIR, credits);
  2811. handle = ext4_journal_current_handle();
  2812. if (IS_ERR(inode)) {
  2813. if (handle)
  2814. ext4_journal_stop(handle);
  2815. err = PTR_ERR(inode);
  2816. goto err_free_sd;
  2817. }
  2818. if (encryption_required) {
  2819. struct qstr istr;
  2820. struct ext4_str ostr;
  2821. istr.name = (const unsigned char *) symname;
  2822. istr.len = len;
  2823. ostr.name = sd->encrypted_path;
  2824. ostr.len = disk_link.len;
  2825. err = ext4_fname_usr_to_disk(inode, &istr, &ostr);
  2826. if (err < 0)
  2827. goto err_drop_inode;
  2828. sd->len = cpu_to_le16(ostr.len);
  2829. disk_link.name = (char *) sd;
  2830. inode->i_op = &ext4_encrypted_symlink_inode_operations;
  2831. }
  2832. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  2833. if (!encryption_required)
  2834. inode->i_op = &ext4_symlink_inode_operations;
  2835. inode_nohighmem(inode);
  2836. ext4_set_aops(inode);
  2837. /*
  2838. * We cannot call page_symlink() with transaction started
  2839. * because it calls into ext4_write_begin() which can wait
  2840. * for transaction commit if we are running out of space
  2841. * and thus we deadlock. So we have to stop transaction now
  2842. * and restart it when symlink contents is written.
  2843. *
  2844. * To keep fs consistent in case of crash, we have to put inode
  2845. * to orphan list in the mean time.
  2846. */
  2847. drop_nlink(inode);
  2848. err = ext4_orphan_add(handle, inode);
  2849. ext4_journal_stop(handle);
  2850. handle = NULL;
  2851. if (err)
  2852. goto err_drop_inode;
  2853. err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
  2854. if (err)
  2855. goto err_drop_inode;
  2856. /*
  2857. * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
  2858. * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
  2859. */
  2860. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2861. EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2862. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
  2863. if (IS_ERR(handle)) {
  2864. err = PTR_ERR(handle);
  2865. handle = NULL;
  2866. goto err_drop_inode;
  2867. }
  2868. set_nlink(inode, 1);
  2869. err = ext4_orphan_del(handle, inode);
  2870. if (err)
  2871. goto err_drop_inode;
  2872. } else {
  2873. /* clear the extent format for fast symlink */
  2874. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  2875. if (!encryption_required) {
  2876. inode->i_op = &ext4_fast_symlink_inode_operations;
  2877. inode->i_link = (char *)&EXT4_I(inode)->i_data;
  2878. }
  2879. memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
  2880. disk_link.len);
  2881. inode->i_size = disk_link.len - 1;
  2882. }
  2883. EXT4_I(inode)->i_disksize = inode->i_size;
  2884. err = ext4_add_nondir(handle, dentry, inode);
  2885. if (!err && IS_DIRSYNC(dir))
  2886. ext4_handle_sync(handle);
  2887. if (handle)
  2888. ext4_journal_stop(handle);
  2889. kfree(sd);
  2890. return err;
  2891. err_drop_inode:
  2892. if (handle)
  2893. ext4_journal_stop(handle);
  2894. clear_nlink(inode);
  2895. unlock_new_inode(inode);
  2896. iput(inode);
  2897. err_free_sd:
  2898. kfree(sd);
  2899. return err;
  2900. }
  2901. static int ext4_link(struct dentry *old_dentry,
  2902. struct inode *dir, struct dentry *dentry)
  2903. {
  2904. handle_t *handle;
  2905. struct inode *inode = d_inode(old_dentry);
  2906. int err, retries = 0;
  2907. if (inode->i_nlink >= EXT4_LINK_MAX)
  2908. return -EMLINK;
  2909. if (ext4_encrypted_inode(dir) &&
  2910. !ext4_is_child_context_consistent_with_parent(dir, inode))
  2911. return -EPERM;
  2912. if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
  2913. (!projid_eq(EXT4_I(dir)->i_projid,
  2914. EXT4_I(old_dentry->d_inode)->i_projid)))
  2915. return -EXDEV;
  2916. err = dquot_initialize(dir);
  2917. if (err)
  2918. return err;
  2919. retry:
  2920. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2921. (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2922. EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
  2923. if (IS_ERR(handle))
  2924. return PTR_ERR(handle);
  2925. if (IS_DIRSYNC(dir))
  2926. ext4_handle_sync(handle);
  2927. inode->i_ctime = ext4_current_time(inode);
  2928. ext4_inc_count(handle, inode);
  2929. ihold(inode);
  2930. err = ext4_add_entry(handle, dentry, inode);
  2931. if (!err) {
  2932. ext4_mark_inode_dirty(handle, inode);
  2933. /* this can happen only for tmpfile being
  2934. * linked the first time
  2935. */
  2936. if (inode->i_nlink == 1)
  2937. ext4_orphan_del(handle, inode);
  2938. d_instantiate(dentry, inode);
  2939. } else {
  2940. drop_nlink(inode);
  2941. iput(inode);
  2942. }
  2943. ext4_journal_stop(handle);
  2944. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2945. goto retry;
  2946. return err;
  2947. }
  2948. /*
  2949. * Try to find buffer head where contains the parent block.
  2950. * It should be the inode block if it is inlined or the 1st block
  2951. * if it is a normal dir.
  2952. */
  2953. static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
  2954. struct inode *inode,
  2955. int *retval,
  2956. struct ext4_dir_entry_2 **parent_de,
  2957. int *inlined)
  2958. {
  2959. struct buffer_head *bh;
  2960. if (!ext4_has_inline_data(inode)) {
  2961. bh = ext4_read_dirblock(inode, 0, EITHER);
  2962. if (IS_ERR(bh)) {
  2963. *retval = PTR_ERR(bh);
  2964. return NULL;
  2965. }
  2966. *parent_de = ext4_next_entry(
  2967. (struct ext4_dir_entry_2 *)bh->b_data,
  2968. inode->i_sb->s_blocksize);
  2969. return bh;
  2970. }
  2971. *inlined = 1;
  2972. return ext4_get_first_inline_block(inode, parent_de, retval);
  2973. }
  2974. struct ext4_renament {
  2975. struct inode *dir;
  2976. struct dentry *dentry;
  2977. struct inode *inode;
  2978. bool is_dir;
  2979. int dir_nlink_delta;
  2980. /* entry for "dentry" */
  2981. struct buffer_head *bh;
  2982. struct ext4_dir_entry_2 *de;
  2983. int inlined;
  2984. /* entry for ".." in inode if it's a directory */
  2985. struct buffer_head *dir_bh;
  2986. struct ext4_dir_entry_2 *parent_de;
  2987. int dir_inlined;
  2988. };
  2989. static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
  2990. {
  2991. int retval;
  2992. ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
  2993. &retval, &ent->parent_de,
  2994. &ent->dir_inlined);
  2995. if (!ent->dir_bh)
  2996. return retval;
  2997. if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
  2998. return -EFSCORRUPTED;
  2999. BUFFER_TRACE(ent->dir_bh, "get_write_access");
  3000. return ext4_journal_get_write_access(handle, ent->dir_bh);
  3001. }
  3002. static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
  3003. unsigned dir_ino)
  3004. {
  3005. int retval;
  3006. ent->parent_de->inode = cpu_to_le32(dir_ino);
  3007. BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
  3008. if (!ent->dir_inlined) {
  3009. if (is_dx(ent->inode)) {
  3010. retval = ext4_handle_dirty_dx_node(handle,
  3011. ent->inode,
  3012. ent->dir_bh);
  3013. } else {
  3014. retval = ext4_handle_dirty_dirent_node(handle,
  3015. ent->inode,
  3016. ent->dir_bh);
  3017. }
  3018. } else {
  3019. retval = ext4_mark_inode_dirty(handle, ent->inode);
  3020. }
  3021. if (retval) {
  3022. ext4_std_error(ent->dir->i_sb, retval);
  3023. return retval;
  3024. }
  3025. return 0;
  3026. }
  3027. static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
  3028. unsigned ino, unsigned file_type)
  3029. {
  3030. int retval;
  3031. BUFFER_TRACE(ent->bh, "get write access");
  3032. retval = ext4_journal_get_write_access(handle, ent->bh);
  3033. if (retval)
  3034. return retval;
  3035. ent->de->inode = cpu_to_le32(ino);
  3036. if (ext4_has_feature_filetype(ent->dir->i_sb))
  3037. ent->de->file_type = file_type;
  3038. ent->dir->i_version++;
  3039. ent->dir->i_ctime = ent->dir->i_mtime =
  3040. ext4_current_time(ent->dir);
  3041. ext4_mark_inode_dirty(handle, ent->dir);
  3042. BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
  3043. if (!ent->inlined) {
  3044. retval = ext4_handle_dirty_dirent_node(handle,
  3045. ent->dir, ent->bh);
  3046. if (unlikely(retval)) {
  3047. ext4_std_error(ent->dir->i_sb, retval);
  3048. return retval;
  3049. }
  3050. }
  3051. brelse(ent->bh);
  3052. ent->bh = NULL;
  3053. return 0;
  3054. }
  3055. static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
  3056. const struct qstr *d_name)
  3057. {
  3058. int retval = -ENOENT;
  3059. struct buffer_head *bh;
  3060. struct ext4_dir_entry_2 *de;
  3061. bh = ext4_find_entry(dir, d_name, &de, NULL);
  3062. if (IS_ERR(bh))
  3063. return PTR_ERR(bh);
  3064. if (bh) {
  3065. retval = ext4_delete_entry(handle, dir, de, bh);
  3066. brelse(bh);
  3067. }
  3068. return retval;
  3069. }
  3070. static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
  3071. int force_reread)
  3072. {
  3073. int retval;
  3074. /*
  3075. * ent->de could have moved from under us during htree split, so make
  3076. * sure that we are deleting the right entry. We might also be pointing
  3077. * to a stale entry in the unused part of ent->bh so just checking inum
  3078. * and the name isn't enough.
  3079. */
  3080. if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
  3081. ent->de->name_len != ent->dentry->d_name.len ||
  3082. strncmp(ent->de->name, ent->dentry->d_name.name,
  3083. ent->de->name_len) ||
  3084. force_reread) {
  3085. retval = ext4_find_delete_entry(handle, ent->dir,
  3086. &ent->dentry->d_name);
  3087. } else {
  3088. retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
  3089. if (retval == -ENOENT) {
  3090. retval = ext4_find_delete_entry(handle, ent->dir,
  3091. &ent->dentry->d_name);
  3092. }
  3093. }
  3094. if (retval) {
  3095. ext4_warning_inode(ent->dir,
  3096. "Deleting old file: nlink %d, error=%d",
  3097. ent->dir->i_nlink, retval);
  3098. }
  3099. }
  3100. static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
  3101. {
  3102. if (ent->dir_nlink_delta) {
  3103. if (ent->dir_nlink_delta == -1)
  3104. ext4_dec_count(handle, ent->dir);
  3105. else
  3106. ext4_inc_count(handle, ent->dir);
  3107. ext4_mark_inode_dirty(handle, ent->dir);
  3108. }
  3109. }
  3110. static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
  3111. int credits, handle_t **h)
  3112. {
  3113. struct inode *wh;
  3114. handle_t *handle;
  3115. int retries = 0;
  3116. /*
  3117. * for inode block, sb block, group summaries,
  3118. * and inode bitmap
  3119. */
  3120. credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
  3121. EXT4_XATTR_TRANS_BLOCKS + 4);
  3122. retry:
  3123. wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
  3124. &ent->dentry->d_name, 0, NULL,
  3125. EXT4_HT_DIR, credits);
  3126. handle = ext4_journal_current_handle();
  3127. if (IS_ERR(wh)) {
  3128. if (handle)
  3129. ext4_journal_stop(handle);
  3130. if (PTR_ERR(wh) == -ENOSPC &&
  3131. ext4_should_retry_alloc(ent->dir->i_sb, &retries))
  3132. goto retry;
  3133. } else {
  3134. *h = handle;
  3135. init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
  3136. wh->i_op = &ext4_special_inode_operations;
  3137. }
  3138. return wh;
  3139. }
  3140. /*
  3141. * Anybody can rename anything with this: the permission checks are left to the
  3142. * higher-level routines.
  3143. *
  3144. * n.b. old_{dentry,inode) refers to the source dentry/inode
  3145. * while new_{dentry,inode) refers to the destination dentry/inode
  3146. * This comes from rename(const char *oldpath, const char *newpath)
  3147. */
  3148. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  3149. struct inode *new_dir, struct dentry *new_dentry,
  3150. unsigned int flags)
  3151. {
  3152. handle_t *handle = NULL;
  3153. struct ext4_renament old = {
  3154. .dir = old_dir,
  3155. .dentry = old_dentry,
  3156. .inode = d_inode(old_dentry),
  3157. };
  3158. struct ext4_renament new = {
  3159. .dir = new_dir,
  3160. .dentry = new_dentry,
  3161. .inode = d_inode(new_dentry),
  3162. };
  3163. int force_reread;
  3164. int retval;
  3165. struct inode *whiteout = NULL;
  3166. int credits;
  3167. u8 old_file_type;
  3168. if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
  3169. (!projid_eq(EXT4_I(new_dir)->i_projid,
  3170. EXT4_I(old_dentry->d_inode)->i_projid)))
  3171. return -EXDEV;
  3172. retval = dquot_initialize(old.dir);
  3173. if (retval)
  3174. return retval;
  3175. retval = dquot_initialize(new.dir);
  3176. if (retval)
  3177. return retval;
  3178. /* Initialize quotas before so that eventual writes go
  3179. * in separate transaction */
  3180. if (new.inode) {
  3181. retval = dquot_initialize(new.inode);
  3182. if (retval)
  3183. return retval;
  3184. }
  3185. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
  3186. if (IS_ERR(old.bh))
  3187. return PTR_ERR(old.bh);
  3188. /*
  3189. * Check for inode number is _not_ due to possible IO errors.
  3190. * We might rmdir the source, keep it as pwd of some process
  3191. * and merrily kill the link to whatever was created under the
  3192. * same name. Goodbye sticky bit ;-<
  3193. */
  3194. retval = -ENOENT;
  3195. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3196. goto end_rename;
  3197. if ((old.dir != new.dir) &&
  3198. ext4_encrypted_inode(new.dir) &&
  3199. !ext4_is_child_context_consistent_with_parent(new.dir,
  3200. old.inode)) {
  3201. retval = -EPERM;
  3202. goto end_rename;
  3203. }
  3204. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3205. &new.de, &new.inlined);
  3206. if (IS_ERR(new.bh)) {
  3207. retval = PTR_ERR(new.bh);
  3208. new.bh = NULL;
  3209. goto end_rename;
  3210. }
  3211. if (new.bh) {
  3212. if (!new.inode) {
  3213. brelse(new.bh);
  3214. new.bh = NULL;
  3215. }
  3216. }
  3217. if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
  3218. ext4_alloc_da_blocks(old.inode);
  3219. credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3220. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  3221. if (!(flags & RENAME_WHITEOUT)) {
  3222. handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
  3223. if (IS_ERR(handle)) {
  3224. retval = PTR_ERR(handle);
  3225. handle = NULL;
  3226. goto end_rename;
  3227. }
  3228. } else {
  3229. whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
  3230. if (IS_ERR(whiteout)) {
  3231. retval = PTR_ERR(whiteout);
  3232. whiteout = NULL;
  3233. goto end_rename;
  3234. }
  3235. }
  3236. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3237. ext4_handle_sync(handle);
  3238. if (S_ISDIR(old.inode->i_mode)) {
  3239. if (new.inode) {
  3240. retval = -ENOTEMPTY;
  3241. if (!ext4_empty_dir(new.inode))
  3242. goto end_rename;
  3243. } else {
  3244. retval = -EMLINK;
  3245. if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
  3246. goto end_rename;
  3247. }
  3248. retval = ext4_rename_dir_prepare(handle, &old);
  3249. if (retval)
  3250. goto end_rename;
  3251. }
  3252. /*
  3253. * If we're renaming a file within an inline_data dir and adding or
  3254. * setting the new dirent causes a conversion from inline_data to
  3255. * extents/blockmap, we need to force the dirent delete code to
  3256. * re-read the directory, or else we end up trying to delete a dirent
  3257. * from what is now the extent tree root (or a block map).
  3258. */
  3259. force_reread = (new.dir->i_ino == old.dir->i_ino &&
  3260. ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
  3261. old_file_type = old.de->file_type;
  3262. if (whiteout) {
  3263. /*
  3264. * Do this before adding a new entry, so the old entry is sure
  3265. * to be still pointing to the valid old entry.
  3266. */
  3267. retval = ext4_setent(handle, &old, whiteout->i_ino,
  3268. EXT4_FT_CHRDEV);
  3269. if (retval)
  3270. goto end_rename;
  3271. ext4_mark_inode_dirty(handle, whiteout);
  3272. }
  3273. if (!new.bh) {
  3274. retval = ext4_add_entry(handle, new.dentry, old.inode);
  3275. if (retval)
  3276. goto end_rename;
  3277. } else {
  3278. retval = ext4_setent(handle, &new,
  3279. old.inode->i_ino, old_file_type);
  3280. if (retval)
  3281. goto end_rename;
  3282. }
  3283. if (force_reread)
  3284. force_reread = !ext4_test_inode_flag(new.dir,
  3285. EXT4_INODE_INLINE_DATA);
  3286. /*
  3287. * Like most other Unix systems, set the ctime for inodes on a
  3288. * rename.
  3289. */
  3290. old.inode->i_ctime = ext4_current_time(old.inode);
  3291. ext4_mark_inode_dirty(handle, old.inode);
  3292. if (!whiteout) {
  3293. /*
  3294. * ok, that's it
  3295. */
  3296. ext4_rename_delete(handle, &old, force_reread);
  3297. }
  3298. if (new.inode) {
  3299. ext4_dec_count(handle, new.inode);
  3300. new.inode->i_ctime = ext4_current_time(new.inode);
  3301. }
  3302. old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
  3303. ext4_update_dx_flag(old.dir);
  3304. if (old.dir_bh) {
  3305. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3306. if (retval)
  3307. goto end_rename;
  3308. ext4_dec_count(handle, old.dir);
  3309. if (new.inode) {
  3310. /* checked ext4_empty_dir above, can't have another
  3311. * parent, ext4_dec_count() won't work for many-linked
  3312. * dirs */
  3313. clear_nlink(new.inode);
  3314. } else {
  3315. ext4_inc_count(handle, new.dir);
  3316. ext4_update_dx_flag(new.dir);
  3317. ext4_mark_inode_dirty(handle, new.dir);
  3318. }
  3319. }
  3320. ext4_mark_inode_dirty(handle, old.dir);
  3321. if (new.inode) {
  3322. ext4_mark_inode_dirty(handle, new.inode);
  3323. if (!new.inode->i_nlink)
  3324. ext4_orphan_add(handle, new.inode);
  3325. }
  3326. retval = 0;
  3327. end_rename:
  3328. brelse(old.dir_bh);
  3329. brelse(old.bh);
  3330. brelse(new.bh);
  3331. if (whiteout) {
  3332. if (retval)
  3333. drop_nlink(whiteout);
  3334. unlock_new_inode(whiteout);
  3335. iput(whiteout);
  3336. }
  3337. if (handle)
  3338. ext4_journal_stop(handle);
  3339. return retval;
  3340. }
  3341. static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
  3342. struct inode *new_dir, struct dentry *new_dentry)
  3343. {
  3344. handle_t *handle = NULL;
  3345. struct ext4_renament old = {
  3346. .dir = old_dir,
  3347. .dentry = old_dentry,
  3348. .inode = d_inode(old_dentry),
  3349. };
  3350. struct ext4_renament new = {
  3351. .dir = new_dir,
  3352. .dentry = new_dentry,
  3353. .inode = d_inode(new_dentry),
  3354. };
  3355. u8 new_file_type;
  3356. int retval;
  3357. if ((ext4_encrypted_inode(old_dir) ||
  3358. ext4_encrypted_inode(new_dir)) &&
  3359. (old_dir != new_dir) &&
  3360. (!ext4_is_child_context_consistent_with_parent(new_dir,
  3361. old.inode) ||
  3362. !ext4_is_child_context_consistent_with_parent(old_dir,
  3363. new.inode)))
  3364. return -EPERM;
  3365. if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
  3366. !projid_eq(EXT4_I(new_dir)->i_projid,
  3367. EXT4_I(old_dentry->d_inode)->i_projid)) ||
  3368. (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
  3369. !projid_eq(EXT4_I(old_dir)->i_projid,
  3370. EXT4_I(new_dentry->d_inode)->i_projid)))
  3371. return -EXDEV;
  3372. retval = dquot_initialize(old.dir);
  3373. if (retval)
  3374. return retval;
  3375. retval = dquot_initialize(new.dir);
  3376. if (retval)
  3377. return retval;
  3378. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
  3379. &old.de, &old.inlined);
  3380. if (IS_ERR(old.bh))
  3381. return PTR_ERR(old.bh);
  3382. /*
  3383. * Check for inode number is _not_ due to possible IO errors.
  3384. * We might rmdir the source, keep it as pwd of some process
  3385. * and merrily kill the link to whatever was created under the
  3386. * same name. Goodbye sticky bit ;-<
  3387. */
  3388. retval = -ENOENT;
  3389. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3390. goto end_rename;
  3391. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3392. &new.de, &new.inlined);
  3393. if (IS_ERR(new.bh)) {
  3394. retval = PTR_ERR(new.bh);
  3395. new.bh = NULL;
  3396. goto end_rename;
  3397. }
  3398. /* RENAME_EXCHANGE case: old *and* new must both exist */
  3399. if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
  3400. goto end_rename;
  3401. handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
  3402. (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3403. 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
  3404. if (IS_ERR(handle)) {
  3405. retval = PTR_ERR(handle);
  3406. handle = NULL;
  3407. goto end_rename;
  3408. }
  3409. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3410. ext4_handle_sync(handle);
  3411. if (S_ISDIR(old.inode->i_mode)) {
  3412. old.is_dir = true;
  3413. retval = ext4_rename_dir_prepare(handle, &old);
  3414. if (retval)
  3415. goto end_rename;
  3416. }
  3417. if (S_ISDIR(new.inode->i_mode)) {
  3418. new.is_dir = true;
  3419. retval = ext4_rename_dir_prepare(handle, &new);
  3420. if (retval)
  3421. goto end_rename;
  3422. }
  3423. /*
  3424. * Other than the special case of overwriting a directory, parents'
  3425. * nlink only needs to be modified if this is a cross directory rename.
  3426. */
  3427. if (old.dir != new.dir && old.is_dir != new.is_dir) {
  3428. old.dir_nlink_delta = old.is_dir ? -1 : 1;
  3429. new.dir_nlink_delta = -old.dir_nlink_delta;
  3430. retval = -EMLINK;
  3431. if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
  3432. (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
  3433. goto end_rename;
  3434. }
  3435. new_file_type = new.de->file_type;
  3436. retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
  3437. if (retval)
  3438. goto end_rename;
  3439. retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
  3440. if (retval)
  3441. goto end_rename;
  3442. /*
  3443. * Like most other Unix systems, set the ctime for inodes on a
  3444. * rename.
  3445. */
  3446. old.inode->i_ctime = ext4_current_time(old.inode);
  3447. new.inode->i_ctime = ext4_current_time(new.inode);
  3448. ext4_mark_inode_dirty(handle, old.inode);
  3449. ext4_mark_inode_dirty(handle, new.inode);
  3450. if (old.dir_bh) {
  3451. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3452. if (retval)
  3453. goto end_rename;
  3454. }
  3455. if (new.dir_bh) {
  3456. retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
  3457. if (retval)
  3458. goto end_rename;
  3459. }
  3460. ext4_update_dir_count(handle, &old);
  3461. ext4_update_dir_count(handle, &new);
  3462. retval = 0;
  3463. end_rename:
  3464. brelse(old.dir_bh);
  3465. brelse(new.dir_bh);
  3466. brelse(old.bh);
  3467. brelse(new.bh);
  3468. if (handle)
  3469. ext4_journal_stop(handle);
  3470. return retval;
  3471. }
  3472. static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
  3473. struct inode *new_dir, struct dentry *new_dentry,
  3474. unsigned int flags)
  3475. {
  3476. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3477. return -EINVAL;
  3478. if (flags & RENAME_EXCHANGE) {
  3479. return ext4_cross_rename(old_dir, old_dentry,
  3480. new_dir, new_dentry);
  3481. }
  3482. return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  3483. }
  3484. /*
  3485. * directories can handle most operations...
  3486. */
  3487. const struct inode_operations ext4_dir_inode_operations = {
  3488. .create = ext4_create,
  3489. .lookup = ext4_lookup,
  3490. .link = ext4_link,
  3491. .unlink = ext4_unlink,
  3492. .symlink = ext4_symlink,
  3493. .mkdir = ext4_mkdir,
  3494. .rmdir = ext4_rmdir,
  3495. .mknod = ext4_mknod,
  3496. .tmpfile = ext4_tmpfile,
  3497. .rename2 = ext4_rename2,
  3498. .setattr = ext4_setattr,
  3499. .setxattr = generic_setxattr,
  3500. .getxattr = generic_getxattr,
  3501. .listxattr = ext4_listxattr,
  3502. .removexattr = generic_removexattr,
  3503. .get_acl = ext4_get_acl,
  3504. .set_acl = ext4_set_acl,
  3505. .fiemap = ext4_fiemap,
  3506. };
  3507. const struct inode_operations ext4_special_inode_operations = {
  3508. .setattr = ext4_setattr,
  3509. .setxattr = generic_setxattr,
  3510. .getxattr = generic_getxattr,
  3511. .listxattr = ext4_listxattr,
  3512. .removexattr = generic_removexattr,
  3513. .get_acl = ext4_get_acl,
  3514. .set_acl = ext4_set_acl,
  3515. };