fsntfs.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. */
  7. #include <linux/blkdev.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/fs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/nls.h>
  12. #include "debug.h"
  13. #include "ntfs.h"
  14. #include "ntfs_fs.h"
  15. // clang-format off
  16. const struct cpu_str NAME_MFT = {
  17. 4, 0, { '$', 'M', 'F', 'T' },
  18. };
  19. const struct cpu_str NAME_MIRROR = {
  20. 8, 0, { '$', 'M', 'F', 'T', 'M', 'i', 'r', 'r' },
  21. };
  22. const struct cpu_str NAME_LOGFILE = {
  23. 8, 0, { '$', 'L', 'o', 'g', 'F', 'i', 'l', 'e' },
  24. };
  25. const struct cpu_str NAME_VOLUME = {
  26. 7, 0, { '$', 'V', 'o', 'l', 'u', 'm', 'e' },
  27. };
  28. const struct cpu_str NAME_ATTRDEF = {
  29. 8, 0, { '$', 'A', 't', 't', 'r', 'D', 'e', 'f' },
  30. };
  31. const struct cpu_str NAME_ROOT = {
  32. 1, 0, { '.' },
  33. };
  34. const struct cpu_str NAME_BITMAP = {
  35. 7, 0, { '$', 'B', 'i', 't', 'm', 'a', 'p' },
  36. };
  37. const struct cpu_str NAME_BOOT = {
  38. 5, 0, { '$', 'B', 'o', 'o', 't' },
  39. };
  40. const struct cpu_str NAME_BADCLUS = {
  41. 8, 0, { '$', 'B', 'a', 'd', 'C', 'l', 'u', 's' },
  42. };
  43. const struct cpu_str NAME_QUOTA = {
  44. 6, 0, { '$', 'Q', 'u', 'o', 't', 'a' },
  45. };
  46. const struct cpu_str NAME_SECURE = {
  47. 7, 0, { '$', 'S', 'e', 'c', 'u', 'r', 'e' },
  48. };
  49. const struct cpu_str NAME_UPCASE = {
  50. 7, 0, { '$', 'U', 'p', 'C', 'a', 's', 'e' },
  51. };
  52. const struct cpu_str NAME_EXTEND = {
  53. 7, 0, { '$', 'E', 'x', 't', 'e', 'n', 'd' },
  54. };
  55. const struct cpu_str NAME_OBJID = {
  56. 6, 0, { '$', 'O', 'b', 'j', 'I', 'd' },
  57. };
  58. const struct cpu_str NAME_REPARSE = {
  59. 8, 0, { '$', 'R', 'e', 'p', 'a', 'r', 's', 'e' },
  60. };
  61. const struct cpu_str NAME_USNJRNL = {
  62. 8, 0, { '$', 'U', 's', 'n', 'J', 'r', 'n', 'l' },
  63. };
  64. const __le16 BAD_NAME[4] = {
  65. cpu_to_le16('$'), cpu_to_le16('B'), cpu_to_le16('a'), cpu_to_le16('d'),
  66. };
  67. const __le16 I30_NAME[4] = {
  68. cpu_to_le16('$'), cpu_to_le16('I'), cpu_to_le16('3'), cpu_to_le16('0'),
  69. };
  70. const __le16 SII_NAME[4] = {
  71. cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('I'), cpu_to_le16('I'),
  72. };
  73. const __le16 SDH_NAME[4] = {
  74. cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('D'), cpu_to_le16('H'),
  75. };
  76. const __le16 SDS_NAME[4] = {
  77. cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('D'), cpu_to_le16('S'),
  78. };
  79. const __le16 SO_NAME[2] = {
  80. cpu_to_le16('$'), cpu_to_le16('O'),
  81. };
  82. const __le16 SQ_NAME[2] = {
  83. cpu_to_le16('$'), cpu_to_le16('Q'),
  84. };
  85. const __le16 SR_NAME[2] = {
  86. cpu_to_le16('$'), cpu_to_le16('R'),
  87. };
  88. #ifdef CONFIG_NTFS3_LZX_XPRESS
  89. const __le16 WOF_NAME[17] = {
  90. cpu_to_le16('W'), cpu_to_le16('o'), cpu_to_le16('f'), cpu_to_le16('C'),
  91. cpu_to_le16('o'), cpu_to_le16('m'), cpu_to_le16('p'), cpu_to_le16('r'),
  92. cpu_to_le16('e'), cpu_to_le16('s'), cpu_to_le16('s'), cpu_to_le16('e'),
  93. cpu_to_le16('d'), cpu_to_le16('D'), cpu_to_le16('a'), cpu_to_le16('t'),
  94. cpu_to_le16('a'),
  95. };
  96. #endif
  97. static const __le16 CON_NAME[3] = {
  98. cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('N'),
  99. };
  100. static const __le16 NUL_NAME[3] = {
  101. cpu_to_le16('N'), cpu_to_le16('U'), cpu_to_le16('L'),
  102. };
  103. static const __le16 AUX_NAME[3] = {
  104. cpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X'),
  105. };
  106. static const __le16 PRN_NAME[3] = {
  107. cpu_to_le16('P'), cpu_to_le16('R'), cpu_to_le16('N'),
  108. };
  109. static const __le16 COM_NAME[3] = {
  110. cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('M'),
  111. };
  112. static const __le16 LPT_NAME[3] = {
  113. cpu_to_le16('L'), cpu_to_le16('P'), cpu_to_le16('T'),
  114. };
  115. // clang-format on
  116. /*
  117. * ntfs_fix_pre_write - Insert fixups into @rhdr before writing to disk.
  118. */
  119. bool ntfs_fix_pre_write(struct NTFS_RECORD_HEADER *rhdr, size_t bytes)
  120. {
  121. u16 *fixup, *ptr;
  122. u16 sample;
  123. u16 fo = le16_to_cpu(rhdr->fix_off);
  124. u16 fn = le16_to_cpu(rhdr->fix_num);
  125. if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
  126. fn * SECTOR_SIZE > bytes) {
  127. return false;
  128. }
  129. /* Get fixup pointer. */
  130. fixup = Add2Ptr(rhdr, fo);
  131. if (*fixup >= 0x7FFF)
  132. *fixup = 1;
  133. else
  134. *fixup += 1;
  135. sample = *fixup;
  136. ptr = Add2Ptr(rhdr, SECTOR_SIZE - sizeof(short));
  137. while (fn--) {
  138. *++fixup = *ptr;
  139. *ptr = sample;
  140. ptr += SECTOR_SIZE / sizeof(short);
  141. }
  142. return true;
  143. }
  144. /*
  145. * ntfs_fix_post_read - Remove fixups after reading from disk.
  146. *
  147. * Return: < 0 if error, 0 if ok, 1 if need to update fixups.
  148. */
  149. int ntfs_fix_post_read(struct NTFS_RECORD_HEADER *rhdr, size_t bytes,
  150. bool simple)
  151. {
  152. int ret;
  153. u16 *fixup, *ptr;
  154. u16 sample, fo, fn;
  155. fo = le16_to_cpu(rhdr->fix_off);
  156. fn = simple ? ((bytes >> SECTOR_SHIFT) + 1) :
  157. le16_to_cpu(rhdr->fix_num);
  158. /* Check errors. */
  159. if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
  160. fn * SECTOR_SIZE > bytes) {
  161. return -E_NTFS_CORRUPT;
  162. }
  163. /* Get fixup pointer. */
  164. fixup = Add2Ptr(rhdr, fo);
  165. sample = *fixup;
  166. ptr = Add2Ptr(rhdr, SECTOR_SIZE - sizeof(short));
  167. ret = 0;
  168. while (fn--) {
  169. /* Test current word. */
  170. if (*ptr != sample) {
  171. /* Fixup does not match! Is it serious error? */
  172. ret = -E_NTFS_FIXUP;
  173. }
  174. /* Replace fixup. */
  175. *ptr = *++fixup;
  176. ptr += SECTOR_SIZE / sizeof(short);
  177. }
  178. return ret;
  179. }
  180. /*
  181. * ntfs_extend_init - Load $Extend file.
  182. */
  183. int ntfs_extend_init(struct ntfs_sb_info *sbi)
  184. {
  185. int err;
  186. struct super_block *sb = sbi->sb;
  187. struct inode *inode, *inode2;
  188. struct MFT_REF ref;
  189. if (sbi->volume.major_ver < 3) {
  190. ntfs_notice(sb, "Skip $Extend 'cause NTFS version");
  191. return 0;
  192. }
  193. ref.low = cpu_to_le32(MFT_REC_EXTEND);
  194. ref.high = 0;
  195. ref.seq = cpu_to_le16(MFT_REC_EXTEND);
  196. inode = ntfs_iget5(sb, &ref, &NAME_EXTEND);
  197. if (IS_ERR(inode)) {
  198. err = PTR_ERR(inode);
  199. ntfs_err(sb, "Failed to load $Extend (%d).", err);
  200. inode = NULL;
  201. goto out;
  202. }
  203. /* If ntfs_iget5() reads from disk it never returns bad inode. */
  204. if (!S_ISDIR(inode->i_mode)) {
  205. err = -EINVAL;
  206. goto out;
  207. }
  208. /* Try to find $ObjId */
  209. inode2 = dir_search_u(inode, &NAME_OBJID, NULL);
  210. if (inode2 && !IS_ERR(inode2)) {
  211. if (is_bad_inode(inode2)) {
  212. iput(inode2);
  213. } else {
  214. sbi->objid.ni = ntfs_i(inode2);
  215. sbi->objid_no = inode2->i_ino;
  216. }
  217. }
  218. /* Try to find $Quota */
  219. inode2 = dir_search_u(inode, &NAME_QUOTA, NULL);
  220. if (inode2 && !IS_ERR(inode2)) {
  221. sbi->quota_no = inode2->i_ino;
  222. iput(inode2);
  223. }
  224. /* Try to find $Reparse */
  225. inode2 = dir_search_u(inode, &NAME_REPARSE, NULL);
  226. if (inode2 && !IS_ERR(inode2)) {
  227. sbi->reparse.ni = ntfs_i(inode2);
  228. sbi->reparse_no = inode2->i_ino;
  229. }
  230. /* Try to find $UsnJrnl */
  231. inode2 = dir_search_u(inode, &NAME_USNJRNL, NULL);
  232. if (inode2 && !IS_ERR(inode2)) {
  233. sbi->usn_jrnl_no = inode2->i_ino;
  234. iput(inode2);
  235. }
  236. err = 0;
  237. out:
  238. iput(inode);
  239. return err;
  240. }
  241. int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi)
  242. {
  243. int err = 0;
  244. struct super_block *sb = sbi->sb;
  245. bool initialized = false;
  246. struct MFT_REF ref;
  247. struct inode *inode;
  248. /* Check for 4GB. */
  249. if (ni->vfs_inode.i_size >= 0x100000000ull) {
  250. ntfs_err(sb, "\x24LogFile is large than 4G.");
  251. err = -EINVAL;
  252. goto out;
  253. }
  254. sbi->flags |= NTFS_FLAGS_LOG_REPLAYING;
  255. ref.low = cpu_to_le32(MFT_REC_MFT);
  256. ref.high = 0;
  257. ref.seq = cpu_to_le16(1);
  258. inode = ntfs_iget5(sb, &ref, NULL);
  259. if (IS_ERR(inode))
  260. inode = NULL;
  261. if (!inode) {
  262. /* Try to use MFT copy. */
  263. u64 t64 = sbi->mft.lbo;
  264. sbi->mft.lbo = sbi->mft.lbo2;
  265. inode = ntfs_iget5(sb, &ref, NULL);
  266. sbi->mft.lbo = t64;
  267. if (IS_ERR(inode))
  268. inode = NULL;
  269. }
  270. if (!inode) {
  271. err = -EINVAL;
  272. ntfs_err(sb, "Failed to load $MFT.");
  273. goto out;
  274. }
  275. sbi->mft.ni = ntfs_i(inode);
  276. /* LogFile should not contains attribute list. */
  277. err = ni_load_all_mi(sbi->mft.ni);
  278. if (!err)
  279. err = log_replay(ni, &initialized);
  280. iput(inode);
  281. sbi->mft.ni = NULL;
  282. sync_blockdev(sb->s_bdev);
  283. invalidate_bdev(sb->s_bdev);
  284. if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
  285. err = 0;
  286. goto out;
  287. }
  288. if (sb_rdonly(sb) || !initialized)
  289. goto out;
  290. /* Fill LogFile by '-1' if it is initialized. */
  291. err = ntfs_bio_fill_1(sbi, &ni->file.run);
  292. out:
  293. sbi->flags &= ~NTFS_FLAGS_LOG_REPLAYING;
  294. return err;
  295. }
  296. /*
  297. * ntfs_look_for_free_space - Look for a free space in bitmap.
  298. */
  299. int ntfs_look_for_free_space(struct ntfs_sb_info *sbi, CLST lcn, CLST len,
  300. CLST *new_lcn, CLST *new_len,
  301. enum ALLOCATE_OPT opt)
  302. {
  303. int err;
  304. CLST alen;
  305. struct super_block *sb = sbi->sb;
  306. size_t alcn, zlen, zeroes, zlcn, zlen2, ztrim, new_zlen;
  307. struct wnd_bitmap *wnd = &sbi->used.bitmap;
  308. down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
  309. if (opt & ALLOCATE_MFT) {
  310. zlen = wnd_zone_len(wnd);
  311. if (!zlen) {
  312. err = ntfs_refresh_zone(sbi);
  313. if (err)
  314. goto up_write;
  315. zlen = wnd_zone_len(wnd);
  316. }
  317. if (!zlen) {
  318. ntfs_err(sbi->sb, "no free space to extend mft");
  319. err = -ENOSPC;
  320. goto up_write;
  321. }
  322. lcn = wnd_zone_bit(wnd);
  323. alen = min_t(CLST, len, zlen);
  324. wnd_zone_set(wnd, lcn + alen, zlen - alen);
  325. err = wnd_set_used(wnd, lcn, alen);
  326. if (err)
  327. goto up_write;
  328. alcn = lcn;
  329. goto space_found;
  330. }
  331. /*
  332. * 'Cause cluster 0 is always used this value means that we should use
  333. * cached value of 'next_free_lcn' to improve performance.
  334. */
  335. if (!lcn)
  336. lcn = sbi->used.next_free_lcn;
  337. if (lcn >= wnd->nbits)
  338. lcn = 0;
  339. alen = wnd_find(wnd, len, lcn, BITMAP_FIND_MARK_AS_USED, &alcn);
  340. if (alen)
  341. goto space_found;
  342. /* Try to use clusters from MftZone. */
  343. zlen = wnd_zone_len(wnd);
  344. zeroes = wnd_zeroes(wnd);
  345. /* Check too big request */
  346. if (len > zeroes + zlen || zlen <= NTFS_MIN_MFT_ZONE) {
  347. err = -ENOSPC;
  348. goto up_write;
  349. }
  350. /* How many clusters to cat from zone. */
  351. zlcn = wnd_zone_bit(wnd);
  352. zlen2 = zlen >> 1;
  353. ztrim = clamp_val(len, zlen2, zlen);
  354. new_zlen = max_t(size_t, zlen - ztrim, NTFS_MIN_MFT_ZONE);
  355. wnd_zone_set(wnd, zlcn, new_zlen);
  356. /* Allocate continues clusters. */
  357. alen = wnd_find(wnd, len, 0,
  358. BITMAP_FIND_MARK_AS_USED | BITMAP_FIND_FULL, &alcn);
  359. if (!alen) {
  360. err = -ENOSPC;
  361. goto up_write;
  362. }
  363. space_found:
  364. err = 0;
  365. *new_len = alen;
  366. *new_lcn = alcn;
  367. ntfs_unmap_meta(sb, alcn, alen);
  368. /* Set hint for next requests. */
  369. if (!(opt & ALLOCATE_MFT))
  370. sbi->used.next_free_lcn = alcn + alen;
  371. up_write:
  372. up_write(&wnd->rw_lock);
  373. return err;
  374. }
  375. /*
  376. * ntfs_check_for_free_space
  377. *
  378. * Check if it is possible to allocate 'clen' clusters and 'mlen' Mft records
  379. */
  380. bool ntfs_check_for_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen)
  381. {
  382. size_t free, zlen, avail;
  383. struct wnd_bitmap *wnd;
  384. wnd = &sbi->used.bitmap;
  385. down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
  386. free = wnd_zeroes(wnd);
  387. zlen = min_t(size_t, NTFS_MIN_MFT_ZONE, wnd_zone_len(wnd));
  388. up_read(&wnd->rw_lock);
  389. if (free < zlen + clen)
  390. return false;
  391. avail = free - (zlen + clen);
  392. wnd = &sbi->mft.bitmap;
  393. down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
  394. free = wnd_zeroes(wnd);
  395. zlen = wnd_zone_len(wnd);
  396. up_read(&wnd->rw_lock);
  397. if (free >= zlen + mlen)
  398. return true;
  399. return avail >= bytes_to_cluster(sbi, mlen << sbi->record_bits);
  400. }
  401. /*
  402. * ntfs_extend_mft - Allocate additional MFT records.
  403. *
  404. * sbi->mft.bitmap is locked for write.
  405. *
  406. * NOTE: recursive:
  407. * ntfs_look_free_mft ->
  408. * ntfs_extend_mft ->
  409. * attr_set_size ->
  410. * ni_insert_nonresident ->
  411. * ni_insert_attr ->
  412. * ni_ins_attr_ext ->
  413. * ntfs_look_free_mft ->
  414. * ntfs_extend_mft
  415. *
  416. * To avoid recursive always allocate space for two new MFT records
  417. * see attrib.c: "at least two MFT to avoid recursive loop".
  418. */
  419. static int ntfs_extend_mft(struct ntfs_sb_info *sbi)
  420. {
  421. int err;
  422. struct ntfs_inode *ni = sbi->mft.ni;
  423. size_t new_mft_total;
  424. u64 new_mft_bytes, new_bitmap_bytes;
  425. struct ATTRIB *attr;
  426. struct wnd_bitmap *wnd = &sbi->mft.bitmap;
  427. new_mft_total = ALIGN(wnd->nbits + NTFS_MFT_INCREASE_STEP, 128);
  428. new_mft_bytes = (u64)new_mft_total << sbi->record_bits;
  429. /* Step 1: Resize $MFT::DATA. */
  430. down_write(&ni->file.run_lock);
  431. err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
  432. new_mft_bytes, NULL, false, &attr);
  433. if (err) {
  434. up_write(&ni->file.run_lock);
  435. goto out;
  436. }
  437. attr->nres.valid_size = attr->nres.data_size;
  438. new_mft_total = le64_to_cpu(attr->nres.alloc_size) >> sbi->record_bits;
  439. ni->mi.dirty = true;
  440. /* Step 2: Resize $MFT::BITMAP. */
  441. new_bitmap_bytes = ntfs3_bitmap_size(new_mft_total);
  442. err = attr_set_size(ni, ATTR_BITMAP, NULL, 0, &sbi->mft.bitmap.run,
  443. new_bitmap_bytes, &new_bitmap_bytes, true, NULL);
  444. /* Refresh MFT Zone if necessary. */
  445. down_write_nested(&sbi->used.bitmap.rw_lock, BITMAP_MUTEX_CLUSTERS);
  446. ntfs_refresh_zone(sbi);
  447. up_write(&sbi->used.bitmap.rw_lock);
  448. up_write(&ni->file.run_lock);
  449. if (err)
  450. goto out;
  451. err = wnd_extend(wnd, new_mft_total);
  452. if (err)
  453. goto out;
  454. ntfs_clear_mft_tail(sbi, sbi->mft.used, new_mft_total);
  455. err = _ni_write_inode(&ni->vfs_inode, 0);
  456. out:
  457. return err;
  458. }
  459. /*
  460. * ntfs_look_free_mft - Look for a free MFT record.
  461. */
  462. int ntfs_look_free_mft(struct ntfs_sb_info *sbi, CLST *rno, bool mft,
  463. struct ntfs_inode *ni, struct mft_inode **mi)
  464. {
  465. int err = 0;
  466. size_t zbit, zlen, from, to, fr;
  467. size_t mft_total;
  468. struct MFT_REF ref;
  469. struct super_block *sb = sbi->sb;
  470. struct wnd_bitmap *wnd = &sbi->mft.bitmap;
  471. u32 ir;
  472. static_assert(sizeof(sbi->mft.reserved_bitmap) * 8 >=
  473. MFT_REC_FREE - MFT_REC_RESERVED);
  474. if (!mft)
  475. down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
  476. zlen = wnd_zone_len(wnd);
  477. /* Always reserve space for MFT. */
  478. if (zlen) {
  479. if (mft) {
  480. zbit = wnd_zone_bit(wnd);
  481. *rno = zbit;
  482. wnd_zone_set(wnd, zbit + 1, zlen - 1);
  483. }
  484. goto found;
  485. }
  486. /* No MFT zone. Find the nearest to '0' free MFT. */
  487. if (!wnd_find(wnd, 1, MFT_REC_FREE, 0, &zbit)) {
  488. /* Resize MFT */
  489. mft_total = wnd->nbits;
  490. err = ntfs_extend_mft(sbi);
  491. if (!err) {
  492. zbit = mft_total;
  493. goto reserve_mft;
  494. }
  495. if (!mft || MFT_REC_FREE == sbi->mft.next_reserved)
  496. goto out;
  497. err = 0;
  498. /*
  499. * Look for free record reserved area [11-16) ==
  500. * [MFT_REC_RESERVED, MFT_REC_FREE ) MFT bitmap always
  501. * marks it as used.
  502. */
  503. if (!sbi->mft.reserved_bitmap) {
  504. /* Once per session create internal bitmap for 5 bits. */
  505. sbi->mft.reserved_bitmap = 0xFF;
  506. ref.high = 0;
  507. for (ir = MFT_REC_RESERVED; ir < MFT_REC_FREE; ir++) {
  508. struct inode *i;
  509. struct ntfs_inode *ni;
  510. struct MFT_REC *mrec;
  511. ref.low = cpu_to_le32(ir);
  512. ref.seq = cpu_to_le16(ir);
  513. i = ntfs_iget5(sb, &ref, NULL);
  514. if (IS_ERR(i)) {
  515. next:
  516. ntfs_notice(
  517. sb,
  518. "Invalid reserved record %x",
  519. ref.low);
  520. continue;
  521. }
  522. if (is_bad_inode(i)) {
  523. iput(i);
  524. goto next;
  525. }
  526. ni = ntfs_i(i);
  527. mrec = ni->mi.mrec;
  528. if (!is_rec_base(mrec))
  529. goto next;
  530. if (mrec->hard_links)
  531. goto next;
  532. if (!ni_std(ni))
  533. goto next;
  534. if (ni_find_attr(ni, NULL, NULL, ATTR_NAME,
  535. NULL, 0, NULL, NULL))
  536. goto next;
  537. __clear_bit(ir - MFT_REC_RESERVED,
  538. &sbi->mft.reserved_bitmap);
  539. }
  540. }
  541. /* Scan 5 bits for zero. Bit 0 == MFT_REC_RESERVED */
  542. zbit = find_next_zero_bit(&sbi->mft.reserved_bitmap,
  543. MFT_REC_FREE, MFT_REC_RESERVED);
  544. if (zbit >= MFT_REC_FREE) {
  545. sbi->mft.next_reserved = MFT_REC_FREE;
  546. goto out;
  547. }
  548. zlen = 1;
  549. sbi->mft.next_reserved = zbit;
  550. } else {
  551. reserve_mft:
  552. zlen = zbit == MFT_REC_FREE ? (MFT_REC_USER - MFT_REC_FREE) : 4;
  553. if (zbit + zlen > wnd->nbits)
  554. zlen = wnd->nbits - zbit;
  555. while (zlen > 1 && !wnd_is_free(wnd, zbit, zlen))
  556. zlen -= 1;
  557. /* [zbit, zbit + zlen) will be used for MFT itself. */
  558. from = sbi->mft.used;
  559. if (from < zbit)
  560. from = zbit;
  561. to = zbit + zlen;
  562. if (from < to) {
  563. ntfs_clear_mft_tail(sbi, from, to);
  564. sbi->mft.used = to;
  565. }
  566. }
  567. if (mft) {
  568. *rno = zbit;
  569. zbit += 1;
  570. zlen -= 1;
  571. }
  572. wnd_zone_set(wnd, zbit, zlen);
  573. found:
  574. if (!mft) {
  575. /* The request to get record for general purpose. */
  576. if (sbi->mft.next_free < MFT_REC_USER)
  577. sbi->mft.next_free = MFT_REC_USER;
  578. for (;;) {
  579. if (sbi->mft.next_free >= sbi->mft.bitmap.nbits) {
  580. } else if (!wnd_find(wnd, 1, MFT_REC_USER, 0, &fr)) {
  581. sbi->mft.next_free = sbi->mft.bitmap.nbits;
  582. } else {
  583. *rno = fr;
  584. sbi->mft.next_free = *rno + 1;
  585. break;
  586. }
  587. err = ntfs_extend_mft(sbi);
  588. if (err)
  589. goto out;
  590. }
  591. }
  592. if (ni && !ni_add_subrecord(ni, *rno, mi)) {
  593. err = -ENOMEM;
  594. goto out;
  595. }
  596. /* We have found a record that are not reserved for next MFT. */
  597. if (*rno >= MFT_REC_FREE)
  598. wnd_set_used(wnd, *rno, 1);
  599. else if (*rno >= MFT_REC_RESERVED && sbi->mft.reserved_bitmap_inited)
  600. __set_bit(*rno - MFT_REC_RESERVED, &sbi->mft.reserved_bitmap);
  601. out:
  602. if (!mft)
  603. up_write(&wnd->rw_lock);
  604. return err;
  605. }
  606. /*
  607. * ntfs_mark_rec_free - Mark record as free.
  608. * is_mft - true if we are changing MFT
  609. */
  610. void ntfs_mark_rec_free(struct ntfs_sb_info *sbi, CLST rno, bool is_mft)
  611. {
  612. struct wnd_bitmap *wnd = &sbi->mft.bitmap;
  613. if (!is_mft)
  614. down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
  615. if (rno >= wnd->nbits)
  616. goto out;
  617. if (rno >= MFT_REC_FREE) {
  618. if (!wnd_is_used(wnd, rno, 1))
  619. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  620. else
  621. wnd_set_free(wnd, rno, 1);
  622. } else if (rno >= MFT_REC_RESERVED && sbi->mft.reserved_bitmap_inited) {
  623. __clear_bit(rno - MFT_REC_RESERVED, &sbi->mft.reserved_bitmap);
  624. }
  625. if (rno < wnd_zone_bit(wnd))
  626. wnd_zone_set(wnd, rno, 1);
  627. else if (rno < sbi->mft.next_free && rno >= MFT_REC_USER)
  628. sbi->mft.next_free = rno;
  629. out:
  630. if (!is_mft)
  631. up_write(&wnd->rw_lock);
  632. }
  633. /*
  634. * ntfs_clear_mft_tail - Format empty records [from, to).
  635. *
  636. * sbi->mft.bitmap is locked for write.
  637. */
  638. int ntfs_clear_mft_tail(struct ntfs_sb_info *sbi, size_t from, size_t to)
  639. {
  640. int err;
  641. u32 rs;
  642. u64 vbo;
  643. struct runs_tree *run;
  644. struct ntfs_inode *ni;
  645. if (from >= to)
  646. return 0;
  647. rs = sbi->record_size;
  648. ni = sbi->mft.ni;
  649. run = &ni->file.run;
  650. down_read(&ni->file.run_lock);
  651. vbo = (u64)from * rs;
  652. for (; from < to; from++, vbo += rs) {
  653. struct ntfs_buffers nb;
  654. err = ntfs_get_bh(sbi, run, vbo, rs, &nb);
  655. if (err)
  656. goto out;
  657. err = ntfs_write_bh(sbi, &sbi->new_rec->rhdr, &nb, 0);
  658. nb_put(&nb);
  659. if (err)
  660. goto out;
  661. }
  662. out:
  663. sbi->mft.used = from;
  664. up_read(&ni->file.run_lock);
  665. return err;
  666. }
  667. /*
  668. * ntfs_refresh_zone - Refresh MFT zone.
  669. *
  670. * sbi->used.bitmap is locked for rw.
  671. * sbi->mft.bitmap is locked for write.
  672. * sbi->mft.ni->file.run_lock for write.
  673. */
  674. int ntfs_refresh_zone(struct ntfs_sb_info *sbi)
  675. {
  676. CLST lcn, vcn, len;
  677. size_t lcn_s, zlen;
  678. struct wnd_bitmap *wnd = &sbi->used.bitmap;
  679. struct ntfs_inode *ni = sbi->mft.ni;
  680. /* Do not change anything unless we have non empty MFT zone. */
  681. if (wnd_zone_len(wnd))
  682. return 0;
  683. vcn = bytes_to_cluster(sbi,
  684. (u64)sbi->mft.bitmap.nbits << sbi->record_bits);
  685. if (!run_lookup_entry(&ni->file.run, vcn - 1, &lcn, &len, NULL))
  686. lcn = SPARSE_LCN;
  687. /* We should always find Last Lcn for MFT. */
  688. if (lcn == SPARSE_LCN)
  689. return -EINVAL;
  690. lcn_s = lcn + 1;
  691. /* Try to allocate clusters after last MFT run. */
  692. zlen = wnd_find(wnd, sbi->zone_max, lcn_s, 0, &lcn_s);
  693. wnd_zone_set(wnd, lcn_s, zlen);
  694. return 0;
  695. }
  696. /*
  697. * ntfs_update_mftmirr - Update $MFTMirr data.
  698. */
  699. void ntfs_update_mftmirr(struct ntfs_sb_info *sbi, int wait)
  700. {
  701. int err;
  702. struct super_block *sb = sbi->sb;
  703. u32 blocksize, bytes;
  704. sector_t block1, block2;
  705. /*
  706. * sb can be NULL here. In this case sbi->flags should be 0 too.
  707. */
  708. if (!sb || !(sbi->flags & NTFS_FLAGS_MFTMIRR) ||
  709. unlikely(ntfs3_forced_shutdown(sb)))
  710. return;
  711. blocksize = sb->s_blocksize;
  712. bytes = sbi->mft.recs_mirr << sbi->record_bits;
  713. block1 = sbi->mft.lbo >> sb->s_blocksize_bits;
  714. block2 = sbi->mft.lbo2 >> sb->s_blocksize_bits;
  715. for (; bytes >= blocksize; bytes -= blocksize) {
  716. struct buffer_head *bh1, *bh2;
  717. bh1 = sb_bread(sb, block1++);
  718. if (!bh1)
  719. return;
  720. bh2 = sb_getblk(sb, block2++);
  721. if (!bh2) {
  722. put_bh(bh1);
  723. return;
  724. }
  725. if (buffer_locked(bh2))
  726. __wait_on_buffer(bh2);
  727. lock_buffer(bh2);
  728. memcpy(bh2->b_data, bh1->b_data, blocksize);
  729. set_buffer_uptodate(bh2);
  730. mark_buffer_dirty(bh2);
  731. unlock_buffer(bh2);
  732. put_bh(bh1);
  733. bh1 = NULL;
  734. err = wait ? sync_dirty_buffer(bh2) : 0;
  735. put_bh(bh2);
  736. if (err)
  737. return;
  738. }
  739. sbi->flags &= ~NTFS_FLAGS_MFTMIRR;
  740. }
  741. /*
  742. * ntfs_bad_inode
  743. *
  744. * Marks inode as bad and marks fs as 'dirty'
  745. */
  746. void ntfs_bad_inode(struct inode *inode, const char *hint)
  747. {
  748. struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
  749. ntfs_inode_err(inode, "%s", hint);
  750. make_bad_inode(inode);
  751. /* Avoid recursion if bad inode is $Volume. */
  752. if (inode->i_ino != MFT_REC_VOL &&
  753. !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING)) {
  754. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  755. }
  756. }
  757. /*
  758. * ntfs_set_state
  759. *
  760. * Mount: ntfs_set_state(NTFS_DIRTY_DIRTY)
  761. * Umount: ntfs_set_state(NTFS_DIRTY_CLEAR)
  762. * NTFS error: ntfs_set_state(NTFS_DIRTY_ERROR)
  763. */
  764. int ntfs_set_state(struct ntfs_sb_info *sbi, enum NTFS_DIRTY_FLAGS dirty)
  765. {
  766. int err;
  767. struct ATTRIB *attr;
  768. struct VOLUME_INFO *info;
  769. struct mft_inode *mi;
  770. struct ntfs_inode *ni;
  771. __le16 info_flags;
  772. /*
  773. * Do not change state if fs was real_dirty.
  774. * Do not change state if fs already dirty(clear).
  775. * Do not change any thing if mounted read only.
  776. */
  777. if (sbi->volume.real_dirty || sb_rdonly(sbi->sb))
  778. return 0;
  779. /* Check cached value. */
  780. if ((dirty == NTFS_DIRTY_CLEAR ? 0 : VOLUME_FLAG_DIRTY) ==
  781. (sbi->volume.flags & VOLUME_FLAG_DIRTY))
  782. return 0;
  783. ni = sbi->volume.ni;
  784. if (!ni)
  785. return -EINVAL;
  786. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_DIRTY);
  787. attr = ni_find_attr(ni, NULL, NULL, ATTR_VOL_INFO, NULL, 0, NULL, &mi);
  788. if (!attr) {
  789. err = -EINVAL;
  790. goto out;
  791. }
  792. info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
  793. if (!info) {
  794. err = -EINVAL;
  795. goto out;
  796. }
  797. info_flags = info->flags;
  798. switch (dirty) {
  799. case NTFS_DIRTY_ERROR:
  800. ntfs_notice(sbi->sb, "Mark volume as dirty due to NTFS errors");
  801. sbi->volume.real_dirty = true;
  802. fallthrough;
  803. case NTFS_DIRTY_DIRTY:
  804. info->flags |= VOLUME_FLAG_DIRTY;
  805. break;
  806. case NTFS_DIRTY_CLEAR:
  807. info->flags &= ~VOLUME_FLAG_DIRTY;
  808. break;
  809. }
  810. /* Cache current volume flags. */
  811. if (info_flags != info->flags) {
  812. sbi->volume.flags = info->flags;
  813. mi->dirty = true;
  814. }
  815. err = 0;
  816. out:
  817. ni_unlock(ni);
  818. if (err)
  819. return err;
  820. mark_inode_dirty_sync(&ni->vfs_inode);
  821. /* verify(!ntfs_update_mftmirr()); */
  822. /* write mft record on disk. */
  823. err = _ni_write_inode(&ni->vfs_inode, 1);
  824. return err;
  825. }
  826. /*
  827. * security_hash - Calculates a hash of security descriptor.
  828. */
  829. static inline __le32 security_hash(const void *sd, size_t bytes)
  830. {
  831. u32 hash = 0;
  832. const __le32 *ptr = sd;
  833. bytes >>= 2;
  834. while (bytes--)
  835. hash = ((hash >> 0x1D) | (hash << 3)) + le32_to_cpu(*ptr++);
  836. return cpu_to_le32(hash);
  837. }
  838. /*
  839. * simple wrapper for sb_bread_unmovable.
  840. */
  841. struct buffer_head *ntfs_bread(struct super_block *sb, sector_t block)
  842. {
  843. struct ntfs_sb_info *sbi = sb->s_fs_info;
  844. struct buffer_head *bh;
  845. if (unlikely(block >= sbi->volume.blocks)) {
  846. /* prevent generic message "attempt to access beyond end of device" */
  847. ntfs_err(sb, "try to read out of volume at offset 0x%llx",
  848. (u64)block << sb->s_blocksize_bits);
  849. return NULL;
  850. }
  851. bh = sb_bread_unmovable(sb, block);
  852. if (bh)
  853. return bh;
  854. ntfs_err(sb, "failed to read volume at offset 0x%llx",
  855. (u64)block << sb->s_blocksize_bits);
  856. return NULL;
  857. }
  858. int ntfs_sb_read(struct super_block *sb, u64 lbo, size_t bytes, void *buffer)
  859. {
  860. struct block_device *bdev = sb->s_bdev;
  861. u32 blocksize = sb->s_blocksize;
  862. u64 block = lbo >> sb->s_blocksize_bits;
  863. u32 off = lbo & (blocksize - 1);
  864. u32 op = blocksize - off;
  865. for (; bytes; block += 1, off = 0, op = blocksize) {
  866. struct buffer_head *bh = __bread(bdev, block, blocksize);
  867. if (!bh)
  868. return -EIO;
  869. if (op > bytes)
  870. op = bytes;
  871. memcpy(buffer, bh->b_data + off, op);
  872. put_bh(bh);
  873. bytes -= op;
  874. buffer = Add2Ptr(buffer, op);
  875. }
  876. return 0;
  877. }
  878. int ntfs_sb_write(struct super_block *sb, u64 lbo, size_t bytes,
  879. const void *buf, int wait)
  880. {
  881. u32 blocksize = sb->s_blocksize;
  882. struct block_device *bdev = sb->s_bdev;
  883. sector_t block = lbo >> sb->s_blocksize_bits;
  884. u32 off = lbo & (blocksize - 1);
  885. u32 op = blocksize - off;
  886. struct buffer_head *bh;
  887. if (!wait && (sb->s_flags & SB_SYNCHRONOUS))
  888. wait = 1;
  889. for (; bytes; block += 1, off = 0, op = blocksize) {
  890. if (op > bytes)
  891. op = bytes;
  892. if (op < blocksize) {
  893. bh = __bread(bdev, block, blocksize);
  894. if (!bh) {
  895. ntfs_err(sb, "failed to read block %llx",
  896. (u64)block);
  897. return -EIO;
  898. }
  899. } else {
  900. bh = __getblk(bdev, block, blocksize);
  901. if (!bh)
  902. return -ENOMEM;
  903. }
  904. if (buffer_locked(bh))
  905. __wait_on_buffer(bh);
  906. lock_buffer(bh);
  907. if (buf) {
  908. memcpy(bh->b_data + off, buf, op);
  909. buf = Add2Ptr(buf, op);
  910. } else {
  911. memset(bh->b_data + off, -1, op);
  912. }
  913. set_buffer_uptodate(bh);
  914. mark_buffer_dirty(bh);
  915. unlock_buffer(bh);
  916. if (wait) {
  917. int err = sync_dirty_buffer(bh);
  918. if (err) {
  919. ntfs_err(
  920. sb,
  921. "failed to sync buffer at block %llx, error %d",
  922. (u64)block, err);
  923. put_bh(bh);
  924. return err;
  925. }
  926. }
  927. put_bh(bh);
  928. bytes -= op;
  929. }
  930. return 0;
  931. }
  932. int ntfs_sb_write_run(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  933. u64 vbo, const void *buf, size_t bytes, int sync)
  934. {
  935. struct super_block *sb = sbi->sb;
  936. u8 cluster_bits = sbi->cluster_bits;
  937. u32 off = vbo & sbi->cluster_mask;
  938. CLST lcn, clen, vcn = vbo >> cluster_bits, vcn_next;
  939. u64 lbo, len;
  940. size_t idx;
  941. if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
  942. return -ENOENT;
  943. if (lcn == SPARSE_LCN)
  944. return -EINVAL;
  945. lbo = ((u64)lcn << cluster_bits) + off;
  946. len = ((u64)clen << cluster_bits) - off;
  947. for (;;) {
  948. u32 op = min_t(u64, len, bytes);
  949. int err = ntfs_sb_write(sb, lbo, op, buf, sync);
  950. if (err)
  951. return err;
  952. bytes -= op;
  953. if (!bytes)
  954. break;
  955. vcn_next = vcn + clen;
  956. if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
  957. vcn != vcn_next)
  958. return -ENOENT;
  959. if (lcn == SPARSE_LCN)
  960. return -EINVAL;
  961. if (buf)
  962. buf = Add2Ptr(buf, op);
  963. lbo = ((u64)lcn << cluster_bits);
  964. len = ((u64)clen << cluster_bits);
  965. }
  966. return 0;
  967. }
  968. struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi,
  969. const struct runs_tree *run, u64 vbo)
  970. {
  971. struct super_block *sb = sbi->sb;
  972. u8 cluster_bits = sbi->cluster_bits;
  973. CLST lcn;
  974. u64 lbo;
  975. if (!run_lookup_entry(run, vbo >> cluster_bits, &lcn, NULL, NULL))
  976. return ERR_PTR(-ENOENT);
  977. lbo = ((u64)lcn << cluster_bits) + (vbo & sbi->cluster_mask);
  978. return ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
  979. }
  980. int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  981. u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb)
  982. {
  983. int err;
  984. struct super_block *sb = sbi->sb;
  985. u32 blocksize = sb->s_blocksize;
  986. u8 cluster_bits = sbi->cluster_bits;
  987. u32 off = vbo & sbi->cluster_mask;
  988. u32 nbh = 0;
  989. CLST vcn_next, vcn = vbo >> cluster_bits;
  990. CLST lcn, clen;
  991. u64 lbo, len;
  992. size_t idx;
  993. struct buffer_head *bh;
  994. if (!run) {
  995. /* First reading of $Volume + $MFTMirr + $LogFile goes here. */
  996. if (vbo > MFT_REC_VOL * sbi->record_size) {
  997. err = -ENOENT;
  998. goto out;
  999. }
  1000. /* Use absolute boot's 'MFTCluster' to read record. */
  1001. lbo = vbo + sbi->mft.lbo;
  1002. len = sbi->record_size;
  1003. } else if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
  1004. err = -ENOENT;
  1005. goto out;
  1006. } else {
  1007. if (lcn == SPARSE_LCN) {
  1008. err = -EINVAL;
  1009. goto out;
  1010. }
  1011. lbo = ((u64)lcn << cluster_bits) + off;
  1012. len = ((u64)clen << cluster_bits) - off;
  1013. }
  1014. off = lbo & (blocksize - 1);
  1015. if (nb) {
  1016. nb->off = off;
  1017. nb->bytes = bytes;
  1018. }
  1019. for (;;) {
  1020. u32 len32 = len >= bytes ? bytes : len;
  1021. sector_t block = lbo >> sb->s_blocksize_bits;
  1022. do {
  1023. u32 op = blocksize - off;
  1024. if (op > len32)
  1025. op = len32;
  1026. bh = ntfs_bread(sb, block);
  1027. if (!bh) {
  1028. err = -EIO;
  1029. goto out;
  1030. }
  1031. if (buf) {
  1032. memcpy(buf, bh->b_data + off, op);
  1033. buf = Add2Ptr(buf, op);
  1034. }
  1035. if (!nb) {
  1036. put_bh(bh);
  1037. } else if (nbh >= ARRAY_SIZE(nb->bh)) {
  1038. err = -EINVAL;
  1039. goto out;
  1040. } else {
  1041. nb->bh[nbh++] = bh;
  1042. nb->nbufs = nbh;
  1043. }
  1044. bytes -= op;
  1045. if (!bytes)
  1046. return 0;
  1047. len32 -= op;
  1048. block += 1;
  1049. off = 0;
  1050. } while (len32);
  1051. vcn_next = vcn + clen;
  1052. if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
  1053. vcn != vcn_next) {
  1054. err = -ENOENT;
  1055. goto out;
  1056. }
  1057. if (lcn == SPARSE_LCN) {
  1058. err = -EINVAL;
  1059. goto out;
  1060. }
  1061. lbo = ((u64)lcn << cluster_bits);
  1062. len = ((u64)clen << cluster_bits);
  1063. }
  1064. out:
  1065. if (!nbh)
  1066. return err;
  1067. while (nbh) {
  1068. put_bh(nb->bh[--nbh]);
  1069. nb->bh[nbh] = NULL;
  1070. }
  1071. nb->nbufs = 0;
  1072. return err;
  1073. }
  1074. /*
  1075. * ntfs_read_bh
  1076. *
  1077. * Return: < 0 if error, 0 if ok, -E_NTFS_FIXUP if need to update fixups.
  1078. */
  1079. int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
  1080. struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
  1081. struct ntfs_buffers *nb)
  1082. {
  1083. int err = ntfs_read_run_nb(sbi, run, vbo, rhdr, bytes, nb);
  1084. if (err)
  1085. return err;
  1086. return ntfs_fix_post_read(rhdr, nb->bytes, true);
  1087. }
  1088. int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
  1089. u32 bytes, struct ntfs_buffers *nb)
  1090. {
  1091. int err = 0;
  1092. struct super_block *sb = sbi->sb;
  1093. u32 blocksize = sb->s_blocksize;
  1094. u8 cluster_bits = sbi->cluster_bits;
  1095. CLST vcn_next, vcn = vbo >> cluster_bits;
  1096. u32 off;
  1097. u32 nbh = 0;
  1098. CLST lcn, clen;
  1099. u64 lbo, len;
  1100. size_t idx;
  1101. nb->bytes = bytes;
  1102. if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
  1103. err = -ENOENT;
  1104. goto out;
  1105. }
  1106. off = vbo & sbi->cluster_mask;
  1107. lbo = ((u64)lcn << cluster_bits) + off;
  1108. len = ((u64)clen << cluster_bits) - off;
  1109. nb->off = off = lbo & (blocksize - 1);
  1110. for (;;) {
  1111. u32 len32 = min_t(u64, len, bytes);
  1112. sector_t block = lbo >> sb->s_blocksize_bits;
  1113. do {
  1114. u32 op;
  1115. struct buffer_head *bh;
  1116. if (nbh >= ARRAY_SIZE(nb->bh)) {
  1117. err = -EINVAL;
  1118. goto out;
  1119. }
  1120. op = blocksize - off;
  1121. if (op > len32)
  1122. op = len32;
  1123. if (op == blocksize) {
  1124. bh = sb_getblk(sb, block);
  1125. if (!bh) {
  1126. err = -ENOMEM;
  1127. goto out;
  1128. }
  1129. if (buffer_locked(bh))
  1130. __wait_on_buffer(bh);
  1131. set_buffer_uptodate(bh);
  1132. } else {
  1133. bh = ntfs_bread(sb, block);
  1134. if (!bh) {
  1135. err = -EIO;
  1136. goto out;
  1137. }
  1138. }
  1139. nb->bh[nbh++] = bh;
  1140. bytes -= op;
  1141. if (!bytes) {
  1142. nb->nbufs = nbh;
  1143. return 0;
  1144. }
  1145. block += 1;
  1146. len32 -= op;
  1147. off = 0;
  1148. } while (len32);
  1149. vcn_next = vcn + clen;
  1150. if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
  1151. vcn != vcn_next) {
  1152. err = -ENOENT;
  1153. goto out;
  1154. }
  1155. lbo = ((u64)lcn << cluster_bits);
  1156. len = ((u64)clen << cluster_bits);
  1157. }
  1158. out:
  1159. while (nbh) {
  1160. put_bh(nb->bh[--nbh]);
  1161. nb->bh[nbh] = NULL;
  1162. }
  1163. nb->nbufs = 0;
  1164. return err;
  1165. }
  1166. int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr,
  1167. struct ntfs_buffers *nb, int sync)
  1168. {
  1169. int err = 0;
  1170. struct super_block *sb = sbi->sb;
  1171. u32 block_size = sb->s_blocksize;
  1172. u32 bytes = nb->bytes;
  1173. u32 off = nb->off;
  1174. u16 fo = le16_to_cpu(rhdr->fix_off);
  1175. u16 fn = le16_to_cpu(rhdr->fix_num);
  1176. u32 idx;
  1177. __le16 *fixup;
  1178. __le16 sample;
  1179. if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
  1180. fn * SECTOR_SIZE > bytes) {
  1181. return -EINVAL;
  1182. }
  1183. for (idx = 0; bytes && idx < nb->nbufs; idx += 1, off = 0) {
  1184. u32 op = block_size - off;
  1185. char *bh_data;
  1186. struct buffer_head *bh = nb->bh[idx];
  1187. __le16 *ptr, *end_data;
  1188. if (op > bytes)
  1189. op = bytes;
  1190. if (buffer_locked(bh))
  1191. __wait_on_buffer(bh);
  1192. lock_buffer(bh);
  1193. bh_data = bh->b_data + off;
  1194. end_data = Add2Ptr(bh_data, op);
  1195. memcpy(bh_data, rhdr, op);
  1196. if (!idx) {
  1197. u16 t16;
  1198. fixup = Add2Ptr(bh_data, fo);
  1199. sample = *fixup;
  1200. t16 = le16_to_cpu(sample);
  1201. if (t16 >= 0x7FFF) {
  1202. sample = *fixup = cpu_to_le16(1);
  1203. } else {
  1204. sample = cpu_to_le16(t16 + 1);
  1205. *fixup = sample;
  1206. }
  1207. *(__le16 *)Add2Ptr(rhdr, fo) = sample;
  1208. }
  1209. ptr = Add2Ptr(bh_data, SECTOR_SIZE - sizeof(short));
  1210. do {
  1211. *++fixup = *ptr;
  1212. *ptr = sample;
  1213. ptr += SECTOR_SIZE / sizeof(short);
  1214. } while (ptr < end_data);
  1215. set_buffer_uptodate(bh);
  1216. mark_buffer_dirty(bh);
  1217. unlock_buffer(bh);
  1218. if (sync) {
  1219. int err2 = sync_dirty_buffer(bh);
  1220. if (!err && err2)
  1221. err = err2;
  1222. }
  1223. bytes -= op;
  1224. rhdr = Add2Ptr(rhdr, op);
  1225. }
  1226. return err;
  1227. }
  1228. /*
  1229. * ntfs_bio_pages - Read/write pages from/to disk.
  1230. */
  1231. int ntfs_bio_pages(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  1232. struct page **pages, u32 nr_pages, u64 vbo, u32 bytes,
  1233. enum req_op op)
  1234. {
  1235. int err = 0;
  1236. struct bio *new, *bio = NULL;
  1237. struct super_block *sb = sbi->sb;
  1238. struct block_device *bdev = sb->s_bdev;
  1239. struct page *page;
  1240. u8 cluster_bits = sbi->cluster_bits;
  1241. CLST lcn, clen, vcn, vcn_next;
  1242. u32 add, off, page_idx;
  1243. u64 lbo, len;
  1244. size_t run_idx;
  1245. struct blk_plug plug;
  1246. if (!bytes)
  1247. return 0;
  1248. blk_start_plug(&plug);
  1249. /* Align vbo and bytes to be 512 bytes aligned. */
  1250. lbo = (vbo + bytes + 511) & ~511ull;
  1251. vbo = vbo & ~511ull;
  1252. bytes = lbo - vbo;
  1253. vcn = vbo >> cluster_bits;
  1254. if (!run_lookup_entry(run, vcn, &lcn, &clen, &run_idx)) {
  1255. err = -ENOENT;
  1256. goto out;
  1257. }
  1258. off = vbo & sbi->cluster_mask;
  1259. page_idx = 0;
  1260. page = pages[0];
  1261. for (;;) {
  1262. lbo = ((u64)lcn << cluster_bits) + off;
  1263. len = ((u64)clen << cluster_bits) - off;
  1264. new_bio:
  1265. new = bio_alloc(bdev, nr_pages - page_idx, op, GFP_NOFS);
  1266. if (bio) {
  1267. bio_chain(bio, new);
  1268. submit_bio(bio);
  1269. }
  1270. bio = new;
  1271. bio->bi_iter.bi_sector = lbo >> 9;
  1272. while (len) {
  1273. off = vbo & (PAGE_SIZE - 1);
  1274. add = off + len > PAGE_SIZE ? (PAGE_SIZE - off) : len;
  1275. if (bio_add_page(bio, page, add, off) < add)
  1276. goto new_bio;
  1277. if (bytes <= add)
  1278. goto out;
  1279. bytes -= add;
  1280. vbo += add;
  1281. if (add + off == PAGE_SIZE) {
  1282. page_idx += 1;
  1283. if (WARN_ON(page_idx >= nr_pages)) {
  1284. err = -EINVAL;
  1285. goto out;
  1286. }
  1287. page = pages[page_idx];
  1288. }
  1289. if (len <= add)
  1290. break;
  1291. len -= add;
  1292. lbo += add;
  1293. }
  1294. vcn_next = vcn + clen;
  1295. if (!run_get_entry(run, ++run_idx, &vcn, &lcn, &clen) ||
  1296. vcn != vcn_next) {
  1297. err = -ENOENT;
  1298. goto out;
  1299. }
  1300. off = 0;
  1301. }
  1302. out:
  1303. if (bio) {
  1304. if (!err)
  1305. err = submit_bio_wait(bio);
  1306. bio_put(bio);
  1307. }
  1308. blk_finish_plug(&plug);
  1309. return err;
  1310. }
  1311. /*
  1312. * ntfs_bio_fill_1 - Helper for ntfs_loadlog_and_replay().
  1313. *
  1314. * Fill on-disk logfile range by (-1)
  1315. * this means empty logfile.
  1316. */
  1317. int ntfs_bio_fill_1(struct ntfs_sb_info *sbi, const struct runs_tree *run)
  1318. {
  1319. int err = 0;
  1320. struct super_block *sb = sbi->sb;
  1321. struct block_device *bdev = sb->s_bdev;
  1322. u8 cluster_bits = sbi->cluster_bits;
  1323. struct bio *new, *bio = NULL;
  1324. CLST lcn, clen;
  1325. u64 lbo, len;
  1326. size_t run_idx;
  1327. struct page *fill;
  1328. void *kaddr;
  1329. struct blk_plug plug;
  1330. fill = alloc_page(GFP_KERNEL);
  1331. if (!fill)
  1332. return -ENOMEM;
  1333. kaddr = kmap_atomic(fill);
  1334. memset(kaddr, -1, PAGE_SIZE);
  1335. kunmap_atomic(kaddr);
  1336. flush_dcache_page(fill);
  1337. lock_page(fill);
  1338. if (!run_lookup_entry(run, 0, &lcn, &clen, &run_idx)) {
  1339. err = -ENOENT;
  1340. goto out;
  1341. }
  1342. /*
  1343. * TODO: Try blkdev_issue_write_same.
  1344. */
  1345. blk_start_plug(&plug);
  1346. do {
  1347. lbo = (u64)lcn << cluster_bits;
  1348. len = (u64)clen << cluster_bits;
  1349. new_bio:
  1350. new = bio_alloc(bdev, BIO_MAX_VECS, REQ_OP_WRITE, GFP_NOFS);
  1351. if (bio) {
  1352. bio_chain(bio, new);
  1353. submit_bio(bio);
  1354. }
  1355. bio = new;
  1356. bio->bi_iter.bi_sector = lbo >> 9;
  1357. for (;;) {
  1358. u32 add = len > PAGE_SIZE ? PAGE_SIZE : len;
  1359. if (bio_add_page(bio, fill, add, 0) < add)
  1360. goto new_bio;
  1361. lbo += add;
  1362. if (len <= add)
  1363. break;
  1364. len -= add;
  1365. }
  1366. } while (run_get_entry(run, ++run_idx, NULL, &lcn, &clen));
  1367. if (!err)
  1368. err = submit_bio_wait(bio);
  1369. bio_put(bio);
  1370. blk_finish_plug(&plug);
  1371. out:
  1372. unlock_page(fill);
  1373. put_page(fill);
  1374. return err;
  1375. }
  1376. int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  1377. u64 vbo, u64 *lbo, u64 *bytes)
  1378. {
  1379. u32 off;
  1380. CLST lcn, len;
  1381. u8 cluster_bits = sbi->cluster_bits;
  1382. if (!run_lookup_entry(run, vbo >> cluster_bits, &lcn, &len, NULL))
  1383. return -ENOENT;
  1384. off = vbo & sbi->cluster_mask;
  1385. *lbo = lcn == SPARSE_LCN ? -1 : (((u64)lcn << cluster_bits) + off);
  1386. *bytes = ((u64)len << cluster_bits) - off;
  1387. return 0;
  1388. }
  1389. struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST rno,
  1390. enum RECORD_FLAG flag)
  1391. {
  1392. int err = 0;
  1393. struct super_block *sb = sbi->sb;
  1394. struct inode *inode = new_inode(sb);
  1395. struct ntfs_inode *ni;
  1396. if (!inode)
  1397. return ERR_PTR(-ENOMEM);
  1398. ni = ntfs_i(inode);
  1399. err = mi_format_new(&ni->mi, sbi, rno, flag, false);
  1400. if (err)
  1401. goto out;
  1402. inode->i_ino = rno;
  1403. if (insert_inode_locked(inode) < 0) {
  1404. err = -EIO;
  1405. goto out;
  1406. }
  1407. out:
  1408. if (err) {
  1409. make_bad_inode(inode);
  1410. iput(inode);
  1411. ni = ERR_PTR(err);
  1412. }
  1413. return ni;
  1414. }
  1415. /*
  1416. * O:BAG:BAD:(A;OICI;FA;;;WD)
  1417. * Owner S-1-5-32-544 (Administrators)
  1418. * Group S-1-5-32-544 (Administrators)
  1419. * ACE: allow S-1-1-0 (Everyone) with FILE_ALL_ACCESS
  1420. */
  1421. const u8 s_default_security[] __aligned(8) = {
  1422. 0x01, 0x00, 0x04, 0x80, 0x30, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  1423. 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x02, 0x00, 0x1C, 0x00,
  1424. 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x14, 0x00, 0xFF, 0x01, 0x1F, 0x00,
  1425. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  1426. 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00,
  1427. 0x20, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
  1428. 0x20, 0x00, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00,
  1429. };
  1430. static_assert(sizeof(s_default_security) == 0x50);
  1431. static inline u32 sid_length(const struct SID *sid)
  1432. {
  1433. return struct_size(sid, SubAuthority, sid->SubAuthorityCount);
  1434. }
  1435. /*
  1436. * is_acl_valid
  1437. *
  1438. * Thanks Mark Harmstone for idea.
  1439. */
  1440. static bool is_acl_valid(const struct ACL *acl, u32 len)
  1441. {
  1442. const struct ACE_HEADER *ace;
  1443. u32 i;
  1444. u16 ace_count, ace_size;
  1445. if (acl->AclRevision != ACL_REVISION &&
  1446. acl->AclRevision != ACL_REVISION_DS) {
  1447. /*
  1448. * This value should be ACL_REVISION, unless the ACL contains an
  1449. * object-specific ACE, in which case this value must be ACL_REVISION_DS.
  1450. * All ACEs in an ACL must be at the same revision level.
  1451. */
  1452. return false;
  1453. }
  1454. if (acl->Sbz1)
  1455. return false;
  1456. if (le16_to_cpu(acl->AclSize) > len)
  1457. return false;
  1458. if (acl->Sbz2)
  1459. return false;
  1460. len -= sizeof(struct ACL);
  1461. ace = (struct ACE_HEADER *)&acl[1];
  1462. ace_count = le16_to_cpu(acl->AceCount);
  1463. for (i = 0; i < ace_count; i++) {
  1464. if (len < sizeof(struct ACE_HEADER))
  1465. return false;
  1466. ace_size = le16_to_cpu(ace->AceSize);
  1467. if (len < ace_size)
  1468. return false;
  1469. len -= ace_size;
  1470. ace = Add2Ptr(ace, ace_size);
  1471. }
  1472. return true;
  1473. }
  1474. bool is_sd_valid(const struct SECURITY_DESCRIPTOR_RELATIVE *sd, u32 len)
  1475. {
  1476. u32 sd_owner, sd_group, sd_sacl, sd_dacl;
  1477. if (len < sizeof(struct SECURITY_DESCRIPTOR_RELATIVE))
  1478. return false;
  1479. if (sd->Revision != 1)
  1480. return false;
  1481. if (sd->Sbz1)
  1482. return false;
  1483. if (!(sd->Control & SE_SELF_RELATIVE))
  1484. return false;
  1485. sd_owner = le32_to_cpu(sd->Owner);
  1486. if (sd_owner) {
  1487. const struct SID *owner = Add2Ptr(sd, sd_owner);
  1488. if (sd_owner + offsetof(struct SID, SubAuthority) > len)
  1489. return false;
  1490. if (owner->Revision != 1)
  1491. return false;
  1492. if (sd_owner + sid_length(owner) > len)
  1493. return false;
  1494. }
  1495. sd_group = le32_to_cpu(sd->Group);
  1496. if (sd_group) {
  1497. const struct SID *group = Add2Ptr(sd, sd_group);
  1498. if (sd_group + offsetof(struct SID, SubAuthority) > len)
  1499. return false;
  1500. if (group->Revision != 1)
  1501. return false;
  1502. if (sd_group + sid_length(group) > len)
  1503. return false;
  1504. }
  1505. sd_sacl = le32_to_cpu(sd->Sacl);
  1506. if (sd_sacl) {
  1507. const struct ACL *sacl = Add2Ptr(sd, sd_sacl);
  1508. if (sd_sacl + sizeof(struct ACL) > len)
  1509. return false;
  1510. if (!is_acl_valid(sacl, len - sd_sacl))
  1511. return false;
  1512. }
  1513. sd_dacl = le32_to_cpu(sd->Dacl);
  1514. if (sd_dacl) {
  1515. const struct ACL *dacl = Add2Ptr(sd, sd_dacl);
  1516. if (sd_dacl + sizeof(struct ACL) > len)
  1517. return false;
  1518. if (!is_acl_valid(dacl, len - sd_dacl))
  1519. return false;
  1520. }
  1521. return true;
  1522. }
  1523. /*
  1524. * ntfs_security_init - Load and parse $Secure.
  1525. */
  1526. int ntfs_security_init(struct ntfs_sb_info *sbi)
  1527. {
  1528. int err;
  1529. struct super_block *sb = sbi->sb;
  1530. struct inode *inode;
  1531. struct ntfs_inode *ni;
  1532. struct MFT_REF ref;
  1533. struct ATTRIB *attr;
  1534. struct ATTR_LIST_ENTRY *le;
  1535. u64 sds_size;
  1536. size_t off;
  1537. struct NTFS_DE *ne;
  1538. struct NTFS_DE_SII *sii_e;
  1539. struct ntfs_fnd *fnd_sii = NULL;
  1540. const struct INDEX_ROOT *root_sii;
  1541. const struct INDEX_ROOT *root_sdh;
  1542. struct ntfs_index *indx_sdh = &sbi->security.index_sdh;
  1543. struct ntfs_index *indx_sii = &sbi->security.index_sii;
  1544. ref.low = cpu_to_le32(MFT_REC_SECURE);
  1545. ref.high = 0;
  1546. ref.seq = cpu_to_le16(MFT_REC_SECURE);
  1547. inode = ntfs_iget5(sb, &ref, &NAME_SECURE);
  1548. if (IS_ERR(inode)) {
  1549. err = PTR_ERR(inode);
  1550. ntfs_err(sb, "Failed to load $Secure (%d).", err);
  1551. inode = NULL;
  1552. goto out;
  1553. }
  1554. ni = ntfs_i(inode);
  1555. le = NULL;
  1556. attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SDH_NAME,
  1557. ARRAY_SIZE(SDH_NAME), NULL, NULL);
  1558. if (!attr ||
  1559. !(root_sdh = resident_data_ex(attr, sizeof(struct INDEX_ROOT))) ||
  1560. root_sdh->type != ATTR_ZERO ||
  1561. root_sdh->rule != NTFS_COLLATION_TYPE_SECURITY_HASH ||
  1562. offsetof(struct INDEX_ROOT, ihdr) +
  1563. le32_to_cpu(root_sdh->ihdr.used) >
  1564. le32_to_cpu(attr->res.data_size)) {
  1565. ntfs_err(sb, "$Secure::$SDH is corrupted.");
  1566. err = -EINVAL;
  1567. goto out;
  1568. }
  1569. err = indx_init(indx_sdh, sbi, attr, INDEX_MUTEX_SDH);
  1570. if (err) {
  1571. ntfs_err(sb, "Failed to initialize $Secure::$SDH (%d).", err);
  1572. goto out;
  1573. }
  1574. attr = ni_find_attr(ni, attr, &le, ATTR_ROOT, SII_NAME,
  1575. ARRAY_SIZE(SII_NAME), NULL, NULL);
  1576. if (!attr ||
  1577. !(root_sii = resident_data_ex(attr, sizeof(struct INDEX_ROOT))) ||
  1578. root_sii->type != ATTR_ZERO ||
  1579. root_sii->rule != NTFS_COLLATION_TYPE_UINT ||
  1580. offsetof(struct INDEX_ROOT, ihdr) +
  1581. le32_to_cpu(root_sii->ihdr.used) >
  1582. le32_to_cpu(attr->res.data_size)) {
  1583. ntfs_err(sb, "$Secure::$SII is corrupted.");
  1584. err = -EINVAL;
  1585. goto out;
  1586. }
  1587. err = indx_init(indx_sii, sbi, attr, INDEX_MUTEX_SII);
  1588. if (err) {
  1589. ntfs_err(sb, "Failed to initialize $Secure::$SII (%d).", err);
  1590. goto out;
  1591. }
  1592. fnd_sii = fnd_get();
  1593. if (!fnd_sii) {
  1594. err = -ENOMEM;
  1595. goto out;
  1596. }
  1597. sds_size = inode->i_size;
  1598. /* Find the last valid Id. */
  1599. sbi->security.next_id = SECURITY_ID_FIRST;
  1600. /* Always write new security at the end of bucket. */
  1601. sbi->security.next_off =
  1602. ALIGN(sds_size - SecurityDescriptorsBlockSize, 16);
  1603. off = 0;
  1604. ne = NULL;
  1605. for (;;) {
  1606. u32 next_id;
  1607. err = indx_find_raw(indx_sii, ni, root_sii, &ne, &off, fnd_sii);
  1608. if (err || !ne)
  1609. break;
  1610. sii_e = (struct NTFS_DE_SII *)ne;
  1611. if (le16_to_cpu(ne->view.data_size) < sizeof(sii_e->sec_hdr))
  1612. continue;
  1613. next_id = le32_to_cpu(sii_e->sec_id) + 1;
  1614. if (next_id >= sbi->security.next_id)
  1615. sbi->security.next_id = next_id;
  1616. }
  1617. sbi->security.ni = ni;
  1618. inode = NULL;
  1619. out:
  1620. iput(inode);
  1621. fnd_put(fnd_sii);
  1622. return err;
  1623. }
  1624. /*
  1625. * ntfs_get_security_by_id - Read security descriptor by id.
  1626. */
  1627. int ntfs_get_security_by_id(struct ntfs_sb_info *sbi, __le32 security_id,
  1628. struct SECURITY_DESCRIPTOR_RELATIVE **sd,
  1629. size_t *size)
  1630. {
  1631. int err;
  1632. int diff;
  1633. struct ntfs_inode *ni = sbi->security.ni;
  1634. struct ntfs_index *indx = &sbi->security.index_sii;
  1635. void *p = NULL;
  1636. struct NTFS_DE_SII *sii_e;
  1637. struct ntfs_fnd *fnd_sii;
  1638. struct SECURITY_HDR d_security;
  1639. const struct INDEX_ROOT *root_sii;
  1640. u32 t32;
  1641. *sd = NULL;
  1642. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_SECURITY);
  1643. fnd_sii = fnd_get();
  1644. if (!fnd_sii) {
  1645. err = -ENOMEM;
  1646. goto out;
  1647. }
  1648. root_sii = indx_get_root(indx, ni, NULL, NULL);
  1649. if (!root_sii) {
  1650. err = -EINVAL;
  1651. goto out;
  1652. }
  1653. /* Try to find this SECURITY descriptor in SII indexes. */
  1654. err = indx_find(indx, ni, root_sii, &security_id, sizeof(security_id),
  1655. NULL, &diff, (struct NTFS_DE **)&sii_e, fnd_sii);
  1656. if (err)
  1657. goto out;
  1658. if (diff)
  1659. goto out;
  1660. t32 = le32_to_cpu(sii_e->sec_hdr.size);
  1661. if (t32 < sizeof(struct SECURITY_HDR)) {
  1662. err = -EINVAL;
  1663. goto out;
  1664. }
  1665. if (t32 > sizeof(struct SECURITY_HDR) + 0x10000) {
  1666. /* Looks like too big security. 0x10000 - is arbitrary big number. */
  1667. err = -EFBIG;
  1668. goto out;
  1669. }
  1670. *size = t32 - sizeof(struct SECURITY_HDR);
  1671. p = kmalloc(*size, GFP_NOFS);
  1672. if (!p) {
  1673. err = -ENOMEM;
  1674. goto out;
  1675. }
  1676. err = ntfs_read_run_nb(sbi, &ni->file.run,
  1677. le64_to_cpu(sii_e->sec_hdr.off), &d_security,
  1678. sizeof(d_security), NULL);
  1679. if (err)
  1680. goto out;
  1681. if (memcmp(&d_security, &sii_e->sec_hdr, sizeof(d_security))) {
  1682. err = -EINVAL;
  1683. goto out;
  1684. }
  1685. err = ntfs_read_run_nb(sbi, &ni->file.run,
  1686. le64_to_cpu(sii_e->sec_hdr.off) +
  1687. sizeof(struct SECURITY_HDR),
  1688. p, *size, NULL);
  1689. if (err)
  1690. goto out;
  1691. *sd = p;
  1692. p = NULL;
  1693. out:
  1694. kfree(p);
  1695. fnd_put(fnd_sii);
  1696. ni_unlock(ni);
  1697. return err;
  1698. }
  1699. /*
  1700. * ntfs_insert_security - Insert security descriptor into $Secure::SDS.
  1701. *
  1702. * SECURITY Descriptor Stream data is organized into chunks of 256K bytes
  1703. * and it contains a mirror copy of each security descriptor. When writing
  1704. * to a security descriptor at location X, another copy will be written at
  1705. * location (X+256K).
  1706. * When writing a security descriptor that will cross the 256K boundary,
  1707. * the pointer will be advanced by 256K to skip
  1708. * over the mirror portion.
  1709. */
  1710. int ntfs_insert_security(struct ntfs_sb_info *sbi,
  1711. const struct SECURITY_DESCRIPTOR_RELATIVE *sd,
  1712. u32 size_sd, __le32 *security_id, bool *inserted)
  1713. {
  1714. int err, diff;
  1715. struct ntfs_inode *ni = sbi->security.ni;
  1716. struct ntfs_index *indx_sdh = &sbi->security.index_sdh;
  1717. struct ntfs_index *indx_sii = &sbi->security.index_sii;
  1718. struct NTFS_DE_SDH *e;
  1719. struct NTFS_DE_SDH sdh_e;
  1720. struct NTFS_DE_SII sii_e;
  1721. struct SECURITY_HDR *d_security;
  1722. u32 new_sec_size = size_sd + sizeof(struct SECURITY_HDR);
  1723. u32 aligned_sec_size = ALIGN(new_sec_size, 16);
  1724. struct SECURITY_KEY hash_key;
  1725. struct ntfs_fnd *fnd_sdh = NULL;
  1726. const struct INDEX_ROOT *root_sdh;
  1727. const struct INDEX_ROOT *root_sii;
  1728. u64 mirr_off, new_sds_size;
  1729. u32 next, left;
  1730. static_assert((1 << Log2OfSecurityDescriptorsBlockSize) ==
  1731. SecurityDescriptorsBlockSize);
  1732. hash_key.hash = security_hash(sd, size_sd);
  1733. hash_key.sec_id = SECURITY_ID_INVALID;
  1734. if (inserted)
  1735. *inserted = false;
  1736. *security_id = SECURITY_ID_INVALID;
  1737. /* Allocate a temporal buffer. */
  1738. d_security = kzalloc(aligned_sec_size, GFP_NOFS);
  1739. if (!d_security)
  1740. return -ENOMEM;
  1741. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_SECURITY);
  1742. fnd_sdh = fnd_get();
  1743. if (!fnd_sdh) {
  1744. err = -ENOMEM;
  1745. goto out;
  1746. }
  1747. root_sdh = indx_get_root(indx_sdh, ni, NULL, NULL);
  1748. if (!root_sdh) {
  1749. err = -EINVAL;
  1750. goto out;
  1751. }
  1752. root_sii = indx_get_root(indx_sii, ni, NULL, NULL);
  1753. if (!root_sii) {
  1754. err = -EINVAL;
  1755. goto out;
  1756. }
  1757. /*
  1758. * Check if such security already exists.
  1759. * Use "SDH" and hash -> to get the offset in "SDS".
  1760. */
  1761. err = indx_find(indx_sdh, ni, root_sdh, &hash_key, sizeof(hash_key),
  1762. &d_security->key.sec_id, &diff, (struct NTFS_DE **)&e,
  1763. fnd_sdh);
  1764. if (err)
  1765. goto out;
  1766. while (e) {
  1767. if (le32_to_cpu(e->sec_hdr.size) == new_sec_size) {
  1768. err = ntfs_read_run_nb(sbi, &ni->file.run,
  1769. le64_to_cpu(e->sec_hdr.off),
  1770. d_security, new_sec_size, NULL);
  1771. if (err)
  1772. goto out;
  1773. if (le32_to_cpu(d_security->size) == new_sec_size &&
  1774. d_security->key.hash == hash_key.hash &&
  1775. !memcmp(d_security + 1, sd, size_sd)) {
  1776. /* Such security already exists. */
  1777. *security_id = d_security->key.sec_id;
  1778. err = 0;
  1779. goto out;
  1780. }
  1781. }
  1782. err = indx_find_sort(indx_sdh, ni, root_sdh,
  1783. (struct NTFS_DE **)&e, fnd_sdh);
  1784. if (err)
  1785. goto out;
  1786. if (!e || e->key.hash != hash_key.hash)
  1787. break;
  1788. }
  1789. /* Zero unused space. */
  1790. next = sbi->security.next_off & (SecurityDescriptorsBlockSize - 1);
  1791. left = SecurityDescriptorsBlockSize - next;
  1792. /* Zero gap until SecurityDescriptorsBlockSize. */
  1793. if (left < new_sec_size) {
  1794. /* Zero "left" bytes from sbi->security.next_off. */
  1795. sbi->security.next_off += SecurityDescriptorsBlockSize + left;
  1796. }
  1797. /* Zero tail of previous security. */
  1798. //used = ni->vfs_inode.i_size & (SecurityDescriptorsBlockSize - 1);
  1799. /*
  1800. * Example:
  1801. * 0x40438 == ni->vfs_inode.i_size
  1802. * 0x00440 == sbi->security.next_off
  1803. * need to zero [0x438-0x440)
  1804. * if (next > used) {
  1805. * u32 tozero = next - used;
  1806. * zero "tozero" bytes from sbi->security.next_off - tozero
  1807. */
  1808. /* Format new security descriptor. */
  1809. d_security->key.hash = hash_key.hash;
  1810. d_security->key.sec_id = cpu_to_le32(sbi->security.next_id);
  1811. d_security->off = cpu_to_le64(sbi->security.next_off);
  1812. d_security->size = cpu_to_le32(new_sec_size);
  1813. memcpy(d_security + 1, sd, size_sd);
  1814. /* Write main SDS bucket. */
  1815. err = ntfs_sb_write_run(sbi, &ni->file.run, sbi->security.next_off,
  1816. d_security, aligned_sec_size, 0);
  1817. if (err)
  1818. goto out;
  1819. mirr_off = sbi->security.next_off + SecurityDescriptorsBlockSize;
  1820. new_sds_size = mirr_off + aligned_sec_size;
  1821. if (new_sds_size > ni->vfs_inode.i_size) {
  1822. err = attr_set_size(ni, ATTR_DATA, SDS_NAME,
  1823. ARRAY_SIZE(SDS_NAME), &ni->file.run,
  1824. new_sds_size, &new_sds_size, false, NULL);
  1825. if (err)
  1826. goto out;
  1827. }
  1828. /* Write copy SDS bucket. */
  1829. err = ntfs_sb_write_run(sbi, &ni->file.run, mirr_off, d_security,
  1830. aligned_sec_size, 0);
  1831. if (err)
  1832. goto out;
  1833. /* Fill SII entry. */
  1834. sii_e.de.view.data_off =
  1835. cpu_to_le16(offsetof(struct NTFS_DE_SII, sec_hdr));
  1836. sii_e.de.view.data_size = cpu_to_le16(sizeof(struct SECURITY_HDR));
  1837. sii_e.de.view.res = 0;
  1838. sii_e.de.size = cpu_to_le16(sizeof(struct NTFS_DE_SII));
  1839. sii_e.de.key_size = cpu_to_le16(sizeof(d_security->key.sec_id));
  1840. sii_e.de.flags = 0;
  1841. sii_e.de.res = 0;
  1842. sii_e.sec_id = d_security->key.sec_id;
  1843. memcpy(&sii_e.sec_hdr, d_security, sizeof(struct SECURITY_HDR));
  1844. err = indx_insert_entry(indx_sii, ni, &sii_e.de, NULL, NULL, 0);
  1845. if (err)
  1846. goto out;
  1847. /* Fill SDH entry. */
  1848. sdh_e.de.view.data_off =
  1849. cpu_to_le16(offsetof(struct NTFS_DE_SDH, sec_hdr));
  1850. sdh_e.de.view.data_size = cpu_to_le16(sizeof(struct SECURITY_HDR));
  1851. sdh_e.de.view.res = 0;
  1852. sdh_e.de.size = cpu_to_le16(SIZEOF_SDH_DIRENTRY);
  1853. sdh_e.de.key_size = cpu_to_le16(sizeof(sdh_e.key));
  1854. sdh_e.de.flags = 0;
  1855. sdh_e.de.res = 0;
  1856. sdh_e.key.hash = d_security->key.hash;
  1857. sdh_e.key.sec_id = d_security->key.sec_id;
  1858. memcpy(&sdh_e.sec_hdr, d_security, sizeof(struct SECURITY_HDR));
  1859. sdh_e.magic[0] = cpu_to_le16('I');
  1860. sdh_e.magic[1] = cpu_to_le16('I');
  1861. fnd_clear(fnd_sdh);
  1862. err = indx_insert_entry(indx_sdh, ni, &sdh_e.de, (void *)(size_t)1,
  1863. fnd_sdh, 0);
  1864. if (err)
  1865. goto out;
  1866. *security_id = d_security->key.sec_id;
  1867. if (inserted)
  1868. *inserted = true;
  1869. /* Update Id and offset for next descriptor. */
  1870. sbi->security.next_id += 1;
  1871. sbi->security.next_off += aligned_sec_size;
  1872. out:
  1873. fnd_put(fnd_sdh);
  1874. mark_inode_dirty(&ni->vfs_inode);
  1875. ni_unlock(ni);
  1876. kfree(d_security);
  1877. return err;
  1878. }
  1879. /*
  1880. * ntfs_reparse_init - Load and parse $Extend/$Reparse.
  1881. */
  1882. int ntfs_reparse_init(struct ntfs_sb_info *sbi)
  1883. {
  1884. int err;
  1885. struct ntfs_inode *ni = sbi->reparse.ni;
  1886. struct ntfs_index *indx = &sbi->reparse.index_r;
  1887. struct ATTRIB *attr;
  1888. struct ATTR_LIST_ENTRY *le;
  1889. const struct INDEX_ROOT *root_r;
  1890. if (!ni)
  1891. return 0;
  1892. le = NULL;
  1893. attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SR_NAME,
  1894. ARRAY_SIZE(SR_NAME), NULL, NULL);
  1895. if (!attr) {
  1896. err = -EINVAL;
  1897. goto out;
  1898. }
  1899. root_r = resident_data(attr);
  1900. if (root_r->type != ATTR_ZERO ||
  1901. root_r->rule != NTFS_COLLATION_TYPE_UINTS) {
  1902. err = -EINVAL;
  1903. goto out;
  1904. }
  1905. err = indx_init(indx, sbi, attr, INDEX_MUTEX_SR);
  1906. if (err)
  1907. goto out;
  1908. out:
  1909. return err;
  1910. }
  1911. /*
  1912. * ntfs_objid_init - Load and parse $Extend/$ObjId.
  1913. */
  1914. int ntfs_objid_init(struct ntfs_sb_info *sbi)
  1915. {
  1916. int err;
  1917. struct ntfs_inode *ni = sbi->objid.ni;
  1918. struct ntfs_index *indx = &sbi->objid.index_o;
  1919. struct ATTRIB *attr;
  1920. struct ATTR_LIST_ENTRY *le;
  1921. const struct INDEX_ROOT *root;
  1922. if (!ni)
  1923. return 0;
  1924. le = NULL;
  1925. attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SO_NAME,
  1926. ARRAY_SIZE(SO_NAME), NULL, NULL);
  1927. if (!attr) {
  1928. err = -EINVAL;
  1929. goto out;
  1930. }
  1931. root = resident_data(attr);
  1932. if (root->type != ATTR_ZERO ||
  1933. root->rule != NTFS_COLLATION_TYPE_UINTS) {
  1934. err = -EINVAL;
  1935. goto out;
  1936. }
  1937. err = indx_init(indx, sbi, attr, INDEX_MUTEX_SO);
  1938. if (err)
  1939. goto out;
  1940. out:
  1941. return err;
  1942. }
  1943. int ntfs_objid_remove(struct ntfs_sb_info *sbi, struct GUID *guid)
  1944. {
  1945. int err;
  1946. struct ntfs_inode *ni = sbi->objid.ni;
  1947. struct ntfs_index *indx = &sbi->objid.index_o;
  1948. if (!ni)
  1949. return -EINVAL;
  1950. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_OBJID);
  1951. err = indx_delete_entry(indx, ni, guid, sizeof(*guid), NULL);
  1952. mark_inode_dirty(&ni->vfs_inode);
  1953. ni_unlock(ni);
  1954. return err;
  1955. }
  1956. int ntfs_insert_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
  1957. const struct MFT_REF *ref)
  1958. {
  1959. int err;
  1960. struct ntfs_inode *ni = sbi->reparse.ni;
  1961. struct ntfs_index *indx = &sbi->reparse.index_r;
  1962. struct NTFS_DE_R re;
  1963. if (!ni)
  1964. return -EINVAL;
  1965. memset(&re, 0, sizeof(re));
  1966. re.de.view.data_off = cpu_to_le16(offsetof(struct NTFS_DE_R, zero));
  1967. re.de.size = cpu_to_le16(sizeof(struct NTFS_DE_R));
  1968. re.de.key_size = cpu_to_le16(sizeof(re.key));
  1969. re.key.ReparseTag = rtag;
  1970. memcpy(&re.key.ref, ref, sizeof(*ref));
  1971. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_REPARSE);
  1972. err = indx_insert_entry(indx, ni, &re.de, NULL, NULL, 0);
  1973. mark_inode_dirty(&ni->vfs_inode);
  1974. ni_unlock(ni);
  1975. return err;
  1976. }
  1977. int ntfs_remove_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
  1978. const struct MFT_REF *ref)
  1979. {
  1980. int err, diff;
  1981. struct ntfs_inode *ni = sbi->reparse.ni;
  1982. struct ntfs_index *indx = &sbi->reparse.index_r;
  1983. struct ntfs_fnd *fnd = NULL;
  1984. struct REPARSE_KEY rkey;
  1985. struct NTFS_DE_R *re;
  1986. struct INDEX_ROOT *root_r;
  1987. if (!ni)
  1988. return -EINVAL;
  1989. rkey.ReparseTag = rtag;
  1990. rkey.ref = *ref;
  1991. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_REPARSE);
  1992. if (rtag) {
  1993. err = indx_delete_entry(indx, ni, &rkey, sizeof(rkey), NULL);
  1994. goto out1;
  1995. }
  1996. fnd = fnd_get();
  1997. if (!fnd) {
  1998. err = -ENOMEM;
  1999. goto out1;
  2000. }
  2001. root_r = indx_get_root(indx, ni, NULL, NULL);
  2002. if (!root_r) {
  2003. err = -EINVAL;
  2004. goto out;
  2005. }
  2006. /* 1 - forces to ignore rkey.ReparseTag when comparing keys. */
  2007. err = indx_find(indx, ni, root_r, &rkey, sizeof(rkey), (void *)1, &diff,
  2008. (struct NTFS_DE **)&re, fnd);
  2009. if (err)
  2010. goto out;
  2011. if (memcmp(&re->key.ref, ref, sizeof(*ref))) {
  2012. /* Impossible. Looks like volume corrupt? */
  2013. goto out;
  2014. }
  2015. memcpy(&rkey, &re->key, sizeof(rkey));
  2016. fnd_put(fnd);
  2017. fnd = NULL;
  2018. err = indx_delete_entry(indx, ni, &rkey, sizeof(rkey), NULL);
  2019. if (err)
  2020. goto out;
  2021. out:
  2022. fnd_put(fnd);
  2023. out1:
  2024. mark_inode_dirty(&ni->vfs_inode);
  2025. ni_unlock(ni);
  2026. return err;
  2027. }
  2028. static inline void ntfs_unmap_and_discard(struct ntfs_sb_info *sbi, CLST lcn,
  2029. CLST len)
  2030. {
  2031. ntfs_unmap_meta(sbi->sb, lcn, len);
  2032. ntfs_discard(sbi, lcn, len);
  2033. }
  2034. void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST lcn, CLST len, bool trim)
  2035. {
  2036. CLST end, i, zone_len, zlen;
  2037. struct wnd_bitmap *wnd = &sbi->used.bitmap;
  2038. bool dirty = false;
  2039. down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
  2040. if (!wnd_is_used(wnd, lcn, len)) {
  2041. /* mark volume as dirty out of wnd->rw_lock */
  2042. dirty = true;
  2043. end = lcn + len;
  2044. len = 0;
  2045. for (i = lcn; i < end; i++) {
  2046. if (wnd_is_used(wnd, i, 1)) {
  2047. if (!len)
  2048. lcn = i;
  2049. len += 1;
  2050. continue;
  2051. }
  2052. if (!len)
  2053. continue;
  2054. if (trim)
  2055. ntfs_unmap_and_discard(sbi, lcn, len);
  2056. wnd_set_free(wnd, lcn, len);
  2057. len = 0;
  2058. }
  2059. if (!len)
  2060. goto out;
  2061. }
  2062. if (trim)
  2063. ntfs_unmap_and_discard(sbi, lcn, len);
  2064. wnd_set_free(wnd, lcn, len);
  2065. /* append to MFT zone, if possible. */
  2066. zone_len = wnd_zone_len(wnd);
  2067. zlen = min(zone_len + len, sbi->zone_max);
  2068. if (zlen == zone_len) {
  2069. /* MFT zone already has maximum size. */
  2070. } else if (!zone_len) {
  2071. /* Create MFT zone only if 'zlen' is large enough. */
  2072. if (zlen == sbi->zone_max)
  2073. wnd_zone_set(wnd, lcn, zlen);
  2074. } else {
  2075. CLST zone_lcn = wnd_zone_bit(wnd);
  2076. if (lcn + len == zone_lcn) {
  2077. /* Append into head MFT zone. */
  2078. wnd_zone_set(wnd, lcn, zlen);
  2079. } else if (zone_lcn + zone_len == lcn) {
  2080. /* Append into tail MFT zone. */
  2081. wnd_zone_set(wnd, zone_lcn, zlen);
  2082. }
  2083. }
  2084. out:
  2085. up_write(&wnd->rw_lock);
  2086. if (dirty)
  2087. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  2088. }
  2089. /*
  2090. * run_deallocate - Deallocate clusters.
  2091. */
  2092. int run_deallocate(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  2093. bool trim)
  2094. {
  2095. CLST lcn, len;
  2096. size_t idx = 0;
  2097. while (run_get_entry(run, idx++, NULL, &lcn, &len)) {
  2098. if (lcn == SPARSE_LCN)
  2099. continue;
  2100. mark_as_free_ex(sbi, lcn, len, trim);
  2101. }
  2102. return 0;
  2103. }
  2104. static inline bool name_has_forbidden_chars(const struct le_str *fname)
  2105. {
  2106. int i, ch;
  2107. /* check for forbidden chars */
  2108. for (i = 0; i < fname->len; ++i) {
  2109. ch = le16_to_cpu(fname->name[i]);
  2110. /* control chars */
  2111. if (ch < 0x20)
  2112. return true;
  2113. switch (ch) {
  2114. /* disallowed by Windows */
  2115. case '\\':
  2116. case '/':
  2117. case ':':
  2118. case '*':
  2119. case '?':
  2120. case '<':
  2121. case '>':
  2122. case '|':
  2123. case '\"':
  2124. return true;
  2125. default:
  2126. /* allowed char */
  2127. break;
  2128. }
  2129. }
  2130. /* file names cannot end with space or . */
  2131. if (fname->len > 0) {
  2132. ch = le16_to_cpu(fname->name[fname->len - 1]);
  2133. if (ch == ' ' || ch == '.')
  2134. return true;
  2135. }
  2136. return false;
  2137. }
  2138. static inline bool is_reserved_name(const struct ntfs_sb_info *sbi,
  2139. const struct le_str *fname)
  2140. {
  2141. int port_digit;
  2142. const __le16 *name = fname->name;
  2143. int len = fname->len;
  2144. const u16 *upcase = sbi->upcase;
  2145. /* check for 3 chars reserved names (device names) */
  2146. /* name by itself or with any extension is forbidden */
  2147. if (len == 3 || (len > 3 && le16_to_cpu(name[3]) == '.'))
  2148. if (!ntfs_cmp_names(name, 3, CON_NAME, 3, upcase, false) ||
  2149. !ntfs_cmp_names(name, 3, NUL_NAME, 3, upcase, false) ||
  2150. !ntfs_cmp_names(name, 3, AUX_NAME, 3, upcase, false) ||
  2151. !ntfs_cmp_names(name, 3, PRN_NAME, 3, upcase, false))
  2152. return true;
  2153. /* check for 4 chars reserved names (port name followed by 1..9) */
  2154. /* name by itself or with any extension is forbidden */
  2155. if (len == 4 || (len > 4 && le16_to_cpu(name[4]) == '.')) {
  2156. port_digit = le16_to_cpu(name[3]);
  2157. if (port_digit >= '1' && port_digit <= '9')
  2158. if (!ntfs_cmp_names(name, 3, COM_NAME, 3, upcase,
  2159. false) ||
  2160. !ntfs_cmp_names(name, 3, LPT_NAME, 3, upcase,
  2161. false))
  2162. return true;
  2163. }
  2164. return false;
  2165. }
  2166. /*
  2167. * valid_windows_name - Check if a file name is valid in Windows.
  2168. */
  2169. bool valid_windows_name(struct ntfs_sb_info *sbi, const struct le_str *fname)
  2170. {
  2171. return !name_has_forbidden_chars(fname) &&
  2172. !is_reserved_name(sbi, fname);
  2173. }
  2174. /*
  2175. * ntfs_set_label - updates current ntfs label.
  2176. */
  2177. int ntfs_set_label(struct ntfs_sb_info *sbi, u8 *label, int len)
  2178. {
  2179. int err;
  2180. struct ATTRIB *attr;
  2181. u32 uni_bytes;
  2182. struct ntfs_inode *ni = sbi->volume.ni;
  2183. /* Allocate PATH_MAX bytes. */
  2184. struct cpu_str *uni = __getname();
  2185. if (!uni)
  2186. return -ENOMEM;
  2187. err = ntfs_nls_to_utf16(sbi, label, len, uni, (PATH_MAX - 2) / 2,
  2188. UTF16_LITTLE_ENDIAN);
  2189. if (err < 0)
  2190. goto out;
  2191. uni_bytes = uni->len * sizeof(u16);
  2192. if (uni_bytes > NTFS_LABEL_MAX_LENGTH * sizeof(u16)) {
  2193. ntfs_warn(sbi->sb, "new label is too long");
  2194. err = -EFBIG;
  2195. goto out;
  2196. }
  2197. ni_lock(ni);
  2198. /* Ignore any errors. */
  2199. ni_remove_attr(ni, ATTR_LABEL, NULL, 0, false, NULL);
  2200. err = ni_insert_resident(ni, uni_bytes, ATTR_LABEL, NULL, 0, &attr,
  2201. NULL, NULL);
  2202. if (err < 0)
  2203. goto unlock_out;
  2204. /* write new label in on-disk struct. */
  2205. memcpy(resident_data(attr), uni->name, uni_bytes);
  2206. /* update cached value of current label. */
  2207. if (len >= ARRAY_SIZE(sbi->volume.label))
  2208. len = ARRAY_SIZE(sbi->volume.label) - 1;
  2209. memcpy(sbi->volume.label, label, len);
  2210. sbi->volume.label[len] = 0;
  2211. mark_inode_dirty_sync(&ni->vfs_inode);
  2212. unlock_out:
  2213. ni_unlock(ni);
  2214. if (!err)
  2215. err = _ni_write_inode(&ni->vfs_inode, 0);
  2216. out:
  2217. __putname(uni);
  2218. return err;
  2219. }