namei.c 105 KB

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