inode.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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/buffer_head.h>
  8. #include <linux/fs.h>
  9. #include <linux/mpage.h>
  10. #include <linux/namei.h>
  11. #include <linux/nls.h>
  12. #include <linux/uio.h>
  13. #include <linux/writeback.h>
  14. #include <linux/iomap.h>
  15. #include "debug.h"
  16. #include "ntfs.h"
  17. #include "ntfs_fs.h"
  18. /*
  19. * ntfs_read_mft - Read record and parse MFT.
  20. */
  21. static struct inode *ntfs_read_mft(struct inode *inode,
  22. const struct cpu_str *name,
  23. const struct MFT_REF *ref)
  24. {
  25. int err = 0;
  26. struct ntfs_inode *ni = ntfs_i(inode);
  27. struct super_block *sb = inode->i_sb;
  28. struct ntfs_sb_info *sbi = sb->s_fs_info;
  29. mode_t mode = 0;
  30. struct ATTR_STD_INFO5 *std5 = NULL;
  31. struct ATTR_LIST_ENTRY *le;
  32. struct ATTRIB *attr;
  33. bool is_match = false;
  34. bool is_root = false;
  35. bool is_dir;
  36. unsigned long ino = inode->i_ino;
  37. u32 rp_fa = 0, asize, t32;
  38. u16 roff, rsize, names = 0, links = 0;
  39. const struct ATTR_FILE_NAME *fname = NULL;
  40. const struct INDEX_ROOT *root = NULL;
  41. struct REPARSE_DATA_BUFFER rp; // 0x18 bytes
  42. u64 t64;
  43. struct MFT_REC *rec;
  44. struct runs_tree *run;
  45. struct timespec64 ts;
  46. inode->i_op = NULL;
  47. /* Setup 'uid' and 'gid' */
  48. inode->i_uid = sbi->options->fs_uid;
  49. inode->i_gid = sbi->options->fs_gid;
  50. err = mi_init(&ni->mi, sbi, ino);
  51. if (err)
  52. goto out;
  53. if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
  54. t64 = sbi->mft.lbo >> sbi->cluster_bits;
  55. t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
  56. sbi->mft.ni = ni;
  57. init_rwsem(&ni->file.run_lock);
  58. if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
  59. err = -ENOMEM;
  60. goto out;
  61. }
  62. }
  63. err = mi_read(&ni->mi, ino == MFT_REC_MFT);
  64. if (err)
  65. goto out;
  66. rec = ni->mi.mrec;
  67. if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
  68. ;
  69. } else if (ref->seq != rec->seq) {
  70. err = -EINVAL;
  71. ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
  72. le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
  73. goto out;
  74. } else if (!is_rec_inuse(rec)) {
  75. err = -ESTALE;
  76. ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
  77. goto out;
  78. }
  79. if (le32_to_cpu(rec->total) != sbi->record_size) {
  80. /* Bad inode? */
  81. err = -EINVAL;
  82. goto out;
  83. }
  84. if (!is_rec_base(rec)) {
  85. err = -EINVAL;
  86. goto out;
  87. }
  88. /* Record should contain $I30 root. */
  89. is_dir = rec->flags & RECORD_FLAG_DIR;
  90. /* MFT_REC_MFT is not a dir */
  91. if (is_dir && ino == MFT_REC_MFT) {
  92. err = -EINVAL;
  93. goto out;
  94. }
  95. inode->i_generation = le16_to_cpu(rec->seq);
  96. /* Enumerate all struct Attributes MFT. */
  97. le = NULL;
  98. attr = NULL;
  99. /*
  100. * To reduce tab pressure use goto instead of
  101. * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
  102. */
  103. next_attr:
  104. run = NULL;
  105. err = -EINVAL;
  106. attr = ni_enum_attr_ex(ni, attr, &le, NULL);
  107. if (!attr)
  108. goto end_enum;
  109. if (le && le->vcn) {
  110. /* This is non primary attribute segment. Ignore if not MFT. */
  111. if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
  112. goto next_attr;
  113. run = &ni->file.run;
  114. asize = le32_to_cpu(attr->size);
  115. goto attr_unpack_run;
  116. }
  117. roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
  118. rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
  119. asize = le32_to_cpu(attr->size);
  120. /*
  121. * Really this check was done in 'ni_enum_attr_ex' -> ... 'mi_enum_attr'.
  122. * There not critical to check this case again
  123. */
  124. if (attr->name_len &&
  125. sizeof(short) * attr->name_len + le16_to_cpu(attr->name_off) >
  126. asize)
  127. goto out;
  128. if (attr->non_res) {
  129. t64 = le64_to_cpu(attr->nres.alloc_size);
  130. if (le64_to_cpu(attr->nres.data_size) > t64 ||
  131. le64_to_cpu(attr->nres.valid_size) > t64)
  132. goto out;
  133. }
  134. switch (attr->type) {
  135. case ATTR_STD:
  136. if (attr->non_res ||
  137. asize < sizeof(struct ATTR_STD_INFO) + roff ||
  138. rsize < sizeof(struct ATTR_STD_INFO))
  139. goto out;
  140. if (std5)
  141. goto next_attr;
  142. std5 = Add2Ptr(attr, roff);
  143. nt2kernel(std5->cr_time, &ni->i_crtime);
  144. nt2kernel(std5->a_time, &ts);
  145. inode_set_atime_to_ts(inode, ts);
  146. nt2kernel(std5->c_time, &ts);
  147. inode_set_ctime_to_ts(inode, ts);
  148. nt2kernel(std5->m_time, &ts);
  149. inode_set_mtime_to_ts(inode, ts);
  150. ni->std_fa = std5->fa;
  151. if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
  152. rsize >= sizeof(struct ATTR_STD_INFO5))
  153. ni->std_security_id = std5->security_id;
  154. goto next_attr;
  155. case ATTR_LIST:
  156. if (attr->name_len || le || ino == MFT_REC_LOG)
  157. goto out;
  158. err = ntfs_load_attr_list(ni, attr);
  159. if (err)
  160. goto out;
  161. le = NULL;
  162. attr = NULL;
  163. goto next_attr;
  164. case ATTR_NAME:
  165. if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
  166. rsize < SIZEOF_ATTRIBUTE_FILENAME)
  167. goto out;
  168. names += 1;
  169. fname = Add2Ptr(attr, roff);
  170. if (fname->type == FILE_NAME_DOS)
  171. goto next_attr;
  172. links += 1;
  173. if (name && name->len == fname->name_len &&
  174. !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
  175. NULL, false))
  176. is_match = true;
  177. goto next_attr;
  178. case ATTR_DATA:
  179. if (is_dir) {
  180. /* Ignore data attribute in dir record. */
  181. goto next_attr;
  182. }
  183. if (ino == MFT_REC_BADCLUST && !attr->non_res)
  184. goto next_attr;
  185. if (attr->name_len &&
  186. ((ino != MFT_REC_BADCLUST || !attr->non_res ||
  187. attr->name_len != ARRAY_SIZE(BAD_NAME) ||
  188. memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
  189. (ino != MFT_REC_SECURE || !attr->non_res ||
  190. attr->name_len != ARRAY_SIZE(SDS_NAME) ||
  191. memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
  192. /* File contains stream attribute. Ignore it. */
  193. goto next_attr;
  194. }
  195. if (is_attr_sparsed(attr))
  196. ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
  197. else
  198. ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
  199. if (is_attr_compressed(attr))
  200. ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
  201. else
  202. ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
  203. if (is_attr_encrypted(attr))
  204. ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
  205. else
  206. ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
  207. if (!attr->non_res) {
  208. ni->i_valid = inode->i_size = rsize;
  209. inode_set_bytes(inode, rsize);
  210. }
  211. mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
  212. if (!attr->non_res) {
  213. ni->ni_flags |= NI_FLAG_RESIDENT;
  214. goto next_attr;
  215. }
  216. inode_set_bytes(inode, attr_ondisk_size(attr));
  217. ni->i_valid = le64_to_cpu(attr->nres.valid_size);
  218. inode->i_size = le64_to_cpu(attr->nres.data_size);
  219. if (!attr->nres.alloc_size)
  220. goto next_attr;
  221. run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run :
  222. &ni->file.run;
  223. break;
  224. case ATTR_ROOT:
  225. if (attr->non_res)
  226. goto out;
  227. root = Add2Ptr(attr, roff);
  228. if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
  229. memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
  230. goto next_attr;
  231. if (root->type != ATTR_NAME ||
  232. root->rule != NTFS_COLLATION_TYPE_FILENAME)
  233. goto out;
  234. if (!is_dir)
  235. goto next_attr;
  236. is_root = true;
  237. ni->ni_flags |= NI_FLAG_DIR;
  238. err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
  239. if (err)
  240. goto out;
  241. mode = sb->s_root ?
  242. (S_IFDIR | (0777 & sbi->options->fs_dmask_inv)) :
  243. (S_IFDIR | 0777);
  244. goto next_attr;
  245. case ATTR_ALLOC:
  246. if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
  247. memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
  248. goto next_attr;
  249. inode->i_size = le64_to_cpu(attr->nres.data_size);
  250. ni->i_valid = le64_to_cpu(attr->nres.valid_size);
  251. inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
  252. run = &ni->dir.alloc_run;
  253. break;
  254. case ATTR_BITMAP:
  255. if (ino == MFT_REC_MFT) {
  256. if (!attr->non_res)
  257. goto out;
  258. #ifndef CONFIG_NTFS3_64BIT_CLUSTER
  259. /* 0x20000000 = 2^32 / 8 */
  260. if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
  261. goto out;
  262. #endif
  263. run = &sbi->mft.bitmap.run;
  264. break;
  265. } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
  266. !memcmp(attr_name(attr), I30_NAME,
  267. sizeof(I30_NAME)) &&
  268. attr->non_res) {
  269. run = &ni->dir.bitmap_run;
  270. break;
  271. }
  272. goto next_attr;
  273. case ATTR_REPARSE:
  274. if (attr->name_len)
  275. goto next_attr;
  276. rp_fa = ni_parse_reparse(ni, attr, &rp);
  277. switch (rp_fa) {
  278. case REPARSE_LINK:
  279. /*
  280. * Normal symlink.
  281. * Assume one unicode symbol == one utf8.
  282. */
  283. inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
  284. .PrintNameLength) /
  285. sizeof(u16);
  286. ni->i_valid = inode->i_size;
  287. /* Clear directory bit. */
  288. if (ni->ni_flags & NI_FLAG_DIR) {
  289. indx_clear(&ni->dir);
  290. memset(&ni->dir, 0, sizeof(ni->dir));
  291. ni->ni_flags &= ~NI_FLAG_DIR;
  292. } else {
  293. run_close(&ni->file.run);
  294. }
  295. mode = S_IFLNK | 0777;
  296. is_dir = false;
  297. if (attr->non_res) {
  298. run = &ni->file.run;
  299. goto attr_unpack_run; // Double break.
  300. }
  301. break;
  302. case REPARSE_COMPRESSED:
  303. break;
  304. case REPARSE_DEDUPLICATED:
  305. break;
  306. }
  307. goto next_attr;
  308. case ATTR_EA_INFO:
  309. if (!attr->name_len &&
  310. resident_data_ex(attr, sizeof(struct EA_INFO))) {
  311. ni->ni_flags |= NI_FLAG_EA;
  312. /*
  313. * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
  314. */
  315. inode->i_mode = mode;
  316. ntfs_get_wsl_perm(inode);
  317. mode = inode->i_mode;
  318. }
  319. goto next_attr;
  320. default:
  321. goto next_attr;
  322. }
  323. attr_unpack_run:
  324. roff = le16_to_cpu(attr->nres.run_off);
  325. if (roff > asize) {
  326. err = -EINVAL;
  327. goto out;
  328. }
  329. t64 = le64_to_cpu(attr->nres.svcn);
  330. err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
  331. t64, Add2Ptr(attr, roff), asize - roff);
  332. if (err < 0)
  333. goto out;
  334. err = 0;
  335. goto next_attr;
  336. end_enum:
  337. if (!std5)
  338. goto out;
  339. if (is_bad_inode(inode))
  340. goto out;
  341. if (!is_match && name) {
  342. err = -ENOENT;
  343. goto out;
  344. }
  345. if (std5->fa & FILE_ATTRIBUTE_READONLY)
  346. mode &= ~0222;
  347. if (!names) {
  348. err = -EINVAL;
  349. goto out;
  350. }
  351. if (names != le16_to_cpu(rec->hard_links)) {
  352. /* Correct minor error on the fly. Do not mark inode as dirty. */
  353. ntfs_inode_warn(inode, "Correct links count -> %u.", names);
  354. rec->hard_links = cpu_to_le16(names);
  355. ni->mi.dirty = true;
  356. }
  357. set_nlink(inode, links);
  358. if (S_ISDIR(mode)) {
  359. ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
  360. /*
  361. * Dot and dot-dot should be included in count but was not
  362. * included in enumeration.
  363. * Usually a hard links to directories are disabled.
  364. */
  365. inode->i_op = &ntfs_dir_inode_operations;
  366. inode->i_fop = unlikely(is_legacy_ntfs(sb)) ?
  367. &ntfs_legacy_dir_operations :
  368. &ntfs_dir_operations;
  369. ni->i_valid = 0;
  370. } else if (S_ISLNK(mode)) {
  371. ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
  372. inode->i_op = &ntfs_link_inode_operations;
  373. inode->i_fop = NULL;
  374. inode_nohighmem(inode);
  375. } else if (S_ISREG(mode)) {
  376. ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
  377. inode->i_op = &ntfs_file_inode_operations;
  378. inode->i_fop = unlikely(is_legacy_ntfs(sb)) ?
  379. &ntfs_legacy_file_operations :
  380. &ntfs_file_operations;
  381. inode->i_mapping->a_ops = is_compressed(ni) ? &ntfs_aops_cmpr :
  382. &ntfs_aops;
  383. if (ino != MFT_REC_MFT)
  384. init_rwsem(&ni->file.run_lock);
  385. } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
  386. S_ISSOCK(mode)) {
  387. inode->i_op = &ntfs_special_inode_operations;
  388. init_special_inode(inode, mode, inode->i_rdev);
  389. } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
  390. fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
  391. /* Records in $Extend are not a files or general directories. */
  392. inode->i_op = &ntfs_file_inode_operations;
  393. mode = S_IFREG;
  394. init_rwsem(&ni->file.run_lock);
  395. } else {
  396. err = -EINVAL;
  397. goto out;
  398. }
  399. if ((sbi->options->sys_immutable &&
  400. (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
  401. !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
  402. inode->i_flags |= S_IMMUTABLE;
  403. } else {
  404. inode->i_flags &= ~S_IMMUTABLE;
  405. }
  406. inode->i_mode = mode;
  407. if (!(ni->ni_flags & NI_FLAG_EA)) {
  408. /* If no xattr then no security (stored in xattr). */
  409. inode->i_flags |= S_NOSEC;
  410. }
  411. if (ino == MFT_REC_MFT && !sb->s_root)
  412. sbi->mft.ni = NULL;
  413. unlock_new_inode(inode);
  414. return inode;
  415. out:
  416. if (ino == MFT_REC_MFT && !sb->s_root)
  417. sbi->mft.ni = NULL;
  418. iget_failed(inode);
  419. return ERR_PTR(err);
  420. }
  421. /*
  422. * ntfs_test_inode
  423. *
  424. * Return: 1 if match.
  425. */
  426. static int ntfs_test_inode(struct inode *inode, void *data)
  427. {
  428. struct MFT_REF *ref = data;
  429. return ino_get(ref) == inode->i_ino;
  430. }
  431. static int ntfs_set_inode(struct inode *inode, void *data)
  432. {
  433. const struct MFT_REF *ref = data;
  434. inode->i_ino = ino_get(ref);
  435. return 0;
  436. }
  437. struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
  438. const struct cpu_str *name)
  439. {
  440. struct inode *inode;
  441. inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
  442. (void *)ref);
  443. if (unlikely(!inode))
  444. return ERR_PTR(-ENOMEM);
  445. /* If this is a freshly allocated inode, need to read it now. */
  446. if (inode_state_read_once(inode) & I_NEW)
  447. inode = ntfs_read_mft(inode, name, ref);
  448. else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
  449. /*
  450. * Sequence number is not expected.
  451. * Looks like inode was reused but caller uses the old reference
  452. */
  453. iput(inode);
  454. inode = ERR_PTR(-ESTALE);
  455. }
  456. if (IS_ERR(inode))
  457. ntfs_set_state(sb->s_fs_info, NTFS_DIRTY_ERROR);
  458. return inode;
  459. }
  460. static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
  461. {
  462. struct inode *inode = mapping->host;
  463. struct ntfs_inode *ni = ntfs_i(inode);
  464. /*
  465. * We can get here for an inline file via the FIBMAP ioctl
  466. */
  467. if (is_resident(ni))
  468. return 0;
  469. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  470. !run_is_empty(&ni->file.run_da)) {
  471. /*
  472. * With delalloc data we want to sync the file so
  473. * that we can make sure we allocate blocks for file and data
  474. * is in place for the user to see it
  475. */
  476. ni_allocate_da_blocks(ni);
  477. }
  478. return iomap_bmap(mapping, block, &ntfs_iomap_ops);
  479. }
  480. static void ntfs_iomap_read_end_io(struct bio *bio)
  481. {
  482. int error = blk_status_to_errno(bio->bi_status);
  483. struct folio_iter fi;
  484. bio_for_each_folio_all(fi, bio) {
  485. struct folio *folio = fi.folio;
  486. struct inode *inode = folio->mapping->host;
  487. struct ntfs_inode *ni = ntfs_i(inode);
  488. u64 valid = ni->i_valid;
  489. u32 f_size = folio_size(folio);
  490. loff_t f_pos = folio_pos(folio);
  491. if (valid < f_pos + f_size) {
  492. u32 z_from = valid <= f_pos ?
  493. 0 :
  494. offset_in_folio(folio, valid);
  495. /* The only thing ntfs_iomap_read_end_io used for. */
  496. folio_zero_segment(folio, z_from, f_size);
  497. }
  498. iomap_finish_folio_read(folio, fi.offset, fi.length, error);
  499. }
  500. bio_put(bio);
  501. }
  502. /*
  503. * Copied from iomap/bio.c.
  504. */
  505. static int ntfs_iomap_bio_read_folio_range(const struct iomap_iter *iter,
  506. struct iomap_read_folio_ctx *ctx,
  507. size_t plen)
  508. {
  509. struct folio *folio = ctx->cur_folio;
  510. const struct iomap *iomap = &iter->iomap;
  511. loff_t pos = iter->pos;
  512. size_t poff = offset_in_folio(folio, pos);
  513. loff_t length = iomap_length(iter);
  514. sector_t sector;
  515. struct bio *bio = ctx->read_ctx;
  516. sector = iomap_sector(iomap, pos);
  517. if (!bio || bio_end_sector(bio) != sector ||
  518. !bio_add_folio(bio, folio, plen, poff)) {
  519. gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
  520. gfp_t orig_gfp = gfp;
  521. unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
  522. if (bio)
  523. submit_bio(bio);
  524. if (ctx->rac) /* same as readahead_gfp_mask */
  525. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  526. bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs), REQ_OP_READ,
  527. gfp);
  528. /*
  529. * If the bio_alloc fails, try it again for a single page to
  530. * avoid having to deal with partial page reads. This emulates
  531. * what do_mpage_read_folio does.
  532. */
  533. if (!bio)
  534. bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ, orig_gfp);
  535. if (ctx->rac)
  536. bio->bi_opf |= REQ_RAHEAD;
  537. bio->bi_iter.bi_sector = sector;
  538. bio->bi_end_io = ntfs_iomap_read_end_io;
  539. bio_add_folio_nofail(bio, folio, plen, poff);
  540. ctx->read_ctx = bio;
  541. }
  542. return 0;
  543. }
  544. static void ntfs_iomap_bio_submit_read(struct iomap_read_folio_ctx *ctx)
  545. {
  546. struct bio *bio = ctx->read_ctx;
  547. if (bio)
  548. submit_bio(bio);
  549. }
  550. static const struct iomap_read_ops ntfs_iomap_bio_read_ops = {
  551. .read_folio_range = ntfs_iomap_bio_read_folio_range,
  552. .submit_read = ntfs_iomap_bio_submit_read,
  553. };
  554. static int ntfs_read_folio(struct file *file, struct folio *folio)
  555. {
  556. int err;
  557. struct address_space *mapping = folio->mapping;
  558. struct inode *inode = mapping->host;
  559. struct ntfs_inode *ni = ntfs_i(inode);
  560. loff_t vbo = folio_pos(folio);
  561. struct iomap_read_folio_ctx ctx = {
  562. .cur_folio = folio,
  563. .ops = &ntfs_iomap_bio_read_ops,
  564. };
  565. if (unlikely(is_bad_ni(ni))) {
  566. folio_unlock(folio);
  567. return -EIO;
  568. }
  569. if (ni->i_valid <= vbo) {
  570. folio_zero_range(folio, 0, folio_size(folio));
  571. folio_mark_uptodate(folio);
  572. folio_unlock(folio);
  573. return 0;
  574. }
  575. if (is_compressed(ni)) {
  576. /* ni_lock is taken inside ni_read_folio_cmpr after page locks */
  577. err = ni_read_folio_cmpr(ni, folio);
  578. return err;
  579. }
  580. iomap_read_folio(&ntfs_iomap_ops, &ctx, NULL);
  581. return 0;
  582. }
  583. static void ntfs_readahead(struct readahead_control *rac)
  584. {
  585. struct address_space *mapping = rac->mapping;
  586. struct inode *inode = mapping->host;
  587. struct ntfs_inode *ni = ntfs_i(inode);
  588. struct iomap_read_folio_ctx ctx = {
  589. .ops = &ntfs_iomap_bio_read_ops,
  590. .rac = rac,
  591. };
  592. if (is_resident(ni)) {
  593. /* No readahead for resident. */
  594. return;
  595. }
  596. if (is_compressed(ni)) {
  597. /* No readahead for compressed. */
  598. return;
  599. }
  600. iomap_readahead(&ntfs_iomap_ops, &ctx, NULL);
  601. }
  602. int ntfs_set_size(struct inode *inode, u64 new_size)
  603. {
  604. struct super_block *sb = inode->i_sb;
  605. struct ntfs_sb_info *sbi = sb->s_fs_info;
  606. struct ntfs_inode *ni = ntfs_i(inode);
  607. int err;
  608. /* Check for maximum file size. */
  609. if (is_sparsed(ni) || is_compressed(ni)) {
  610. if (new_size > sbi->maxbytes_sparse) {
  611. return -EFBIG;
  612. }
  613. } else if (new_size > sbi->maxbytes) {
  614. return -EFBIG;
  615. }
  616. ni_lock(ni);
  617. down_write(&ni->file.run_lock);
  618. err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
  619. &ni->i_valid, true);
  620. if (!err) {
  621. i_size_write(inode, new_size);
  622. mark_inode_dirty(inode);
  623. }
  624. up_write(&ni->file.run_lock);
  625. ni_unlock(ni);
  626. return err;
  627. }
  628. /*
  629. * Special value to detect ntfs_writeback_range call
  630. */
  631. #define WB_NO_DA (struct iomap *)1
  632. /*
  633. * Function to get mapping vbo -> lbo.
  634. * used with:
  635. * - iomap_zero_range
  636. * - iomap_truncate_page
  637. * - iomap_dio_rw
  638. * - iomap_file_buffered_write
  639. * - iomap_bmap
  640. * - iomap_fiemap
  641. * - iomap_bio_read_folio
  642. * - iomap_bio_readahead
  643. */
  644. static int ntfs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  645. unsigned int flags, struct iomap *iomap,
  646. struct iomap *srcmap)
  647. {
  648. struct ntfs_inode *ni = ntfs_i(inode);
  649. struct ntfs_sb_info *sbi = ni->mi.sbi;
  650. u8 cluster_bits = sbi->cluster_bits;
  651. CLST vcn = offset >> cluster_bits;
  652. u32 off = offset & sbi->cluster_mask;
  653. bool rw = flags & IOMAP_WRITE;
  654. loff_t endbyte = offset + length;
  655. void *res = NULL;
  656. int err;
  657. CLST lcn, clen, clen_max = 1;
  658. bool new_clst = false;
  659. bool no_da;
  660. bool zero = false;
  661. if (unlikely(ntfs3_forced_shutdown(sbi->sb)))
  662. return -EIO;
  663. if (flags & IOMAP_REPORT) {
  664. if (offset > ntfs_get_maxbytes(ni)) {
  665. /* called from fiemap/bmap. */
  666. return -EINVAL;
  667. }
  668. if (offset >= inode->i_size) {
  669. /* special code for report. */
  670. return -ENOENT;
  671. }
  672. }
  673. if (IOMAP_ZERO == flags && (endbyte & sbi->cluster_mask)) {
  674. rw = true;
  675. } else if (rw) {
  676. clen_max = bytes_to_cluster(sbi, endbyte) - vcn;
  677. }
  678. /*
  679. * Force to allocate clusters if directIO(write) or writeback_range.
  680. * NOTE: attr_data_get_block allocates clusters only for sparse file.
  681. * Normal file allocates clusters in attr_set_size.
  682. */
  683. no_da = flags == (IOMAP_DIRECT | IOMAP_WRITE) || srcmap == WB_NO_DA;
  684. err = attr_data_get_block(ni, vcn, clen_max, &lcn, &clen,
  685. rw ? &new_clst : NULL, zero, &res, no_da);
  686. if (err) {
  687. return err;
  688. }
  689. if (lcn == EOF_LCN) {
  690. /* request out of file. */
  691. if (flags & IOMAP_REPORT) {
  692. /* special code for report. */
  693. return -ENOENT;
  694. }
  695. if (rw) {
  696. /* should never be here. */
  697. return -EINVAL;
  698. }
  699. lcn = SPARSE_LCN;
  700. }
  701. iomap->flags = new_clst ? IOMAP_F_NEW : 0;
  702. if (lcn == RESIDENT_LCN) {
  703. if (offset >= clen) {
  704. kfree(res);
  705. if (flags & IOMAP_REPORT) {
  706. /* special code for report. */
  707. return -ENOENT;
  708. }
  709. return -EFAULT;
  710. }
  711. iomap->private = iomap->inline_data = res;
  712. iomap->type = IOMAP_INLINE;
  713. iomap->offset = 0;
  714. iomap->length = clen; /* resident size in bytes. */
  715. return 0;
  716. }
  717. if (!clen) {
  718. /* broken file? */
  719. return -EINVAL;
  720. }
  721. iomap->bdev = inode->i_sb->s_bdev;
  722. iomap->offset = offset;
  723. iomap->length = ((loff_t)clen << cluster_bits) - off;
  724. if (lcn == COMPRESSED_LCN) {
  725. /* should never be here. */
  726. return -EOPNOTSUPP;
  727. }
  728. if (lcn == DELALLOC_LCN) {
  729. iomap->type = IOMAP_DELALLOC;
  730. iomap->addr = IOMAP_NULL_ADDR;
  731. } else {
  732. /* Translate clusters into bytes. */
  733. iomap->addr = ((loff_t)lcn << cluster_bits) + off;
  734. if (length && iomap->length > length)
  735. iomap->length = length;
  736. else
  737. endbyte = offset + iomap->length;
  738. if (lcn == SPARSE_LCN) {
  739. iomap->addr = IOMAP_NULL_ADDR;
  740. iomap->type = IOMAP_HOLE;
  741. // if (IOMAP_ZERO == flags && !off) {
  742. // iomap->length = (endbyte - offset) &
  743. // sbi->cluster_mask_inv;
  744. // }
  745. } else if (endbyte <= ni->i_valid) {
  746. iomap->type = IOMAP_MAPPED;
  747. } else if (offset < ni->i_valid) {
  748. iomap->type = IOMAP_MAPPED;
  749. if (flags & IOMAP_REPORT)
  750. iomap->length = ni->i_valid - offset;
  751. } else if (rw || (flags & IOMAP_ZERO)) {
  752. iomap->type = IOMAP_MAPPED;
  753. } else {
  754. iomap->type = IOMAP_UNWRITTEN;
  755. }
  756. }
  757. if ((flags & IOMAP_ZERO) &&
  758. (iomap->type == IOMAP_MAPPED || iomap->type == IOMAP_DELALLOC)) {
  759. /* Avoid too large requests. */
  760. u32 tail;
  761. u32 off_a = offset & (PAGE_SIZE - 1);
  762. if (off_a)
  763. tail = PAGE_SIZE - off_a;
  764. else
  765. tail = PAGE_SIZE;
  766. if (iomap->length > tail)
  767. iomap->length = tail;
  768. }
  769. return 0;
  770. }
  771. static int ntfs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
  772. ssize_t written, unsigned int flags,
  773. struct iomap *iomap)
  774. {
  775. int err = 0;
  776. struct ntfs_inode *ni = ntfs_i(inode);
  777. loff_t endbyte = pos + written;
  778. if ((flags & IOMAP_WRITE) || (flags & IOMAP_ZERO)) {
  779. if (iomap->type == IOMAP_INLINE) {
  780. u32 data_size;
  781. struct ATTRIB *attr;
  782. struct mft_inode *mi;
  783. attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0,
  784. NULL, &mi);
  785. if (!attr || attr->non_res) {
  786. err = -EINVAL;
  787. goto out;
  788. }
  789. data_size = le32_to_cpu(attr->res.data_size);
  790. if (!(pos < data_size && endbyte <= data_size)) {
  791. err = -EINVAL;
  792. goto out;
  793. }
  794. /* Update resident data. */
  795. memcpy(resident_data(attr) + pos,
  796. iomap_inline_data(iomap, pos), written);
  797. mi->dirty = true;
  798. ni->i_valid = data_size;
  799. } else if (ni->i_valid < endbyte) {
  800. ni->i_valid = endbyte;
  801. mark_inode_dirty(inode);
  802. }
  803. }
  804. if ((flags & IOMAP_ZERO) &&
  805. (iomap->type == IOMAP_MAPPED || iomap->type == IOMAP_DELALLOC)) {
  806. /* Pair for code in ntfs_iomap_begin. */
  807. balance_dirty_pages_ratelimited(inode->i_mapping);
  808. cond_resched();
  809. }
  810. out:
  811. if (iomap->type == IOMAP_INLINE) {
  812. kfree(iomap->private);
  813. iomap->private = NULL;
  814. }
  815. return err;
  816. }
  817. /*
  818. * write_begin + put_folio + write_end.
  819. * iomap_zero_range
  820. * iomap_truncate_page
  821. * iomap_file_buffered_write
  822. */
  823. static void ntfs_iomap_put_folio(struct inode *inode, loff_t pos,
  824. unsigned int len, struct folio *folio)
  825. {
  826. struct ntfs_inode *ni = ntfs_i(inode);
  827. loff_t end = pos + len;
  828. u32 f_size = folio_size(folio);
  829. loff_t f_pos = folio_pos(folio);
  830. loff_t f_end = f_pos + f_size;
  831. if (ni->i_valid <= end && end < f_end) {
  832. /* zero range [end - f_end). */
  833. /* The only thing ntfs_iomap_put_folio used for. */
  834. folio_zero_segment(folio, offset_in_folio(folio, end), f_size);
  835. }
  836. folio_unlock(folio);
  837. folio_put(folio);
  838. }
  839. /*
  840. * iomap_writeback_ops::writeback_range
  841. */
  842. static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc,
  843. struct folio *folio, u64 offset,
  844. unsigned int len, u64 end_pos)
  845. {
  846. struct iomap *iomap = &wpc->iomap;
  847. /* Check iomap position. */
  848. if (iomap->offset + iomap->length <= offset || offset < iomap->offset) {
  849. int err;
  850. struct inode *inode = wpc->inode;
  851. struct ntfs_inode *ni = ntfs_i(inode);
  852. struct ntfs_sb_info *sbi = ntfs_sb(inode->i_sb);
  853. loff_t i_size_up = ntfs_up_cluster(sbi, inode->i_size);
  854. loff_t len_max = i_size_up - offset;
  855. err = ni->file.run_da.count ? ni_allocate_da_blocks(ni) : 0;
  856. if (!err) {
  857. /* Use local special value 'WB_NO_DA' to disable delalloc. */
  858. err = ntfs_iomap_begin(inode, offset, len_max,
  859. IOMAP_WRITE, iomap, WB_NO_DA);
  860. }
  861. if (err) {
  862. ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
  863. return err;
  864. }
  865. }
  866. return iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
  867. }
  868. static const struct iomap_writeback_ops ntfs_writeback_ops = {
  869. .writeback_range = ntfs_writeback_range,
  870. .writeback_submit = iomap_ioend_writeback_submit,
  871. };
  872. static int ntfs_resident_writepage(struct folio *folio,
  873. struct writeback_control *wbc)
  874. {
  875. struct address_space *mapping = folio->mapping;
  876. struct inode *inode = mapping->host;
  877. struct ntfs_inode *ni = ntfs_i(inode);
  878. int ret;
  879. /* Avoid any operation if inode is bad. */
  880. if (unlikely(is_bad_ni(ni)))
  881. return -EINVAL;
  882. if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
  883. return -EIO;
  884. ni_lock(ni);
  885. ret = attr_data_write_resident(ni, folio);
  886. ni_unlock(ni);
  887. if (ret != E_NTFS_NONRESIDENT)
  888. folio_unlock(folio);
  889. mapping_set_error(mapping, ret);
  890. return ret;
  891. }
  892. static int ntfs_writepages(struct address_space *mapping,
  893. struct writeback_control *wbc)
  894. {
  895. int err;
  896. struct inode *inode = mapping->host;
  897. struct ntfs_inode *ni = ntfs_i(inode);
  898. struct iomap_writepage_ctx wpc = {
  899. .inode = mapping->host,
  900. .wbc = wbc,
  901. .ops = &ntfs_writeback_ops,
  902. };
  903. /* Avoid any operation if inode is bad. */
  904. if (unlikely(is_bad_ni(ni)))
  905. return -EINVAL;
  906. if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
  907. return -EIO;
  908. if (is_resident(ni)) {
  909. struct folio *folio = NULL;
  910. while ((folio = writeback_iter(mapping, wbc, folio, &err)))
  911. err = ntfs_resident_writepage(folio, wbc);
  912. return err;
  913. }
  914. return iomap_writepages(&wpc);
  915. }
  916. int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
  917. {
  918. return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  919. }
  920. int ntfs_sync_inode(struct inode *inode)
  921. {
  922. return _ni_write_inode(inode, 1);
  923. }
  924. /*
  925. * Helper function to read file.
  926. * Used to read $AttrDef and $UpCase
  927. */
  928. int inode_read_data(struct inode *inode, void *data, size_t bytes)
  929. {
  930. pgoff_t idx;
  931. struct address_space *mapping = inode->i_mapping;
  932. for (idx = 0; bytes; idx++) {
  933. size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
  934. struct page *page = read_mapping_page(mapping, idx, NULL);
  935. void *kaddr;
  936. if (IS_ERR(page))
  937. return PTR_ERR(page);
  938. kaddr = kmap_atomic(page);
  939. memcpy(data, kaddr, op);
  940. kunmap_atomic(kaddr);
  941. put_page(page);
  942. bytes -= op;
  943. data = Add2Ptr(data, PAGE_SIZE);
  944. }
  945. return 0;
  946. }
  947. /*
  948. * ntfs_reparse_bytes
  949. *
  950. * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
  951. * for unicode string of @uni_len length.
  952. */
  953. static inline u32 ntfs_reparse_bytes(u32 uni_len, bool is_absolute)
  954. {
  955. /* Header + unicode string + decorated unicode string. */
  956. return sizeof(short) * (2 * uni_len + (is_absolute ? 4 : 0)) +
  957. offsetof(struct REPARSE_DATA_BUFFER,
  958. SymbolicLinkReparseBuffer.PathBuffer);
  959. }
  960. static struct REPARSE_DATA_BUFFER *
  961. ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
  962. u32 size, u16 *nsize)
  963. {
  964. int i, err;
  965. struct REPARSE_DATA_BUFFER *rp;
  966. __le16 *rp_name;
  967. typeof(rp->SymbolicLinkReparseBuffer) *rs;
  968. bool is_absolute;
  969. is_absolute = symname[0] && symname[1] == ':';
  970. rp = kzalloc(ntfs_reparse_bytes(2 * size + 2, is_absolute), GFP_NOFS);
  971. if (!rp)
  972. return ERR_PTR(-ENOMEM);
  973. rs = &rp->SymbolicLinkReparseBuffer;
  974. rp_name = rs->PathBuffer;
  975. /* Convert link name to UTF-16. */
  976. err = ntfs_nls_to_utf16(sbi, symname, size,
  977. (struct cpu_str *)(rp_name - 1), 2 * size,
  978. UTF16_LITTLE_ENDIAN);
  979. if (err < 0)
  980. goto out;
  981. /* err = the length of unicode name of symlink. */
  982. *nsize = ntfs_reparse_bytes(err, is_absolute);
  983. if (*nsize > sbi->reparse.max_size) {
  984. err = -EFBIG;
  985. goto out;
  986. }
  987. /* Translate Linux '/' into Windows '\'. */
  988. for (i = 0; i < err; i++) {
  989. if (rp_name[i] == cpu_to_le16('/'))
  990. rp_name[i] = cpu_to_le16('\\');
  991. }
  992. rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
  993. rp->ReparseDataLength =
  994. cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
  995. SymbolicLinkReparseBuffer));
  996. /* PrintName + SubstituteName. */
  997. rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
  998. rs->SubstituteNameLength =
  999. cpu_to_le16(sizeof(short) * err + (is_absolute ? 8 : 0));
  1000. rs->PrintNameLength = rs->SubstituteNameOffset;
  1001. /*
  1002. * TODO: Use relative path if possible to allow Windows to
  1003. * parse this path.
  1004. * 0-absolute path, 1- relative path (SYMLINK_FLAG_RELATIVE).
  1005. */
  1006. rs->Flags = cpu_to_le32(is_absolute ? 0 : SYMLINK_FLAG_RELATIVE);
  1007. memmove(rp_name + err + (is_absolute ? 4 : 0), rp_name,
  1008. sizeof(short) * err);
  1009. if (is_absolute) {
  1010. /* Decorate SubstituteName. */
  1011. rp_name += err;
  1012. rp_name[0] = cpu_to_le16('\\');
  1013. rp_name[1] = cpu_to_le16('?');
  1014. rp_name[2] = cpu_to_le16('?');
  1015. rp_name[3] = cpu_to_le16('\\');
  1016. }
  1017. return rp;
  1018. out:
  1019. kfree(rp);
  1020. return ERR_PTR(err);
  1021. }
  1022. /*
  1023. * ntfs_create_inode
  1024. *
  1025. * Helper function for:
  1026. * - ntfs_create
  1027. * - ntfs_mknod
  1028. * - ntfs_symlink
  1029. * - ntfs_mkdir
  1030. * - ntfs_atomic_open
  1031. *
  1032. * NOTE: if fnd != NULL (ntfs_atomic_open) then @dir is locked
  1033. */
  1034. int ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir,
  1035. struct dentry *dentry, const struct cpu_str *uni,
  1036. umode_t mode, dev_t dev, const char *symname, u32 size,
  1037. struct ntfs_fnd *fnd)
  1038. {
  1039. int err;
  1040. struct super_block *sb = dir->i_sb;
  1041. struct ntfs_sb_info *sbi = sb->s_fs_info;
  1042. const struct qstr *name = &dentry->d_name;
  1043. CLST ino = 0;
  1044. struct ntfs_inode *dir_ni = ntfs_i(dir);
  1045. struct ntfs_inode *ni = NULL;
  1046. struct inode *inode = NULL;
  1047. struct ATTRIB *attr;
  1048. struct ATTR_STD_INFO5 *std5;
  1049. struct ATTR_FILE_NAME *fname;
  1050. struct MFT_REC *rec;
  1051. u32 asize, dsize, sd_size;
  1052. enum FILE_ATTRIBUTE fa;
  1053. __le32 security_id = SECURITY_ID_INVALID;
  1054. CLST vcn;
  1055. const void *sd;
  1056. u16 t16, nsize = 0, aid = 0;
  1057. struct INDEX_ROOT *root, *dir_root;
  1058. struct NTFS_DE *e, *new_de = NULL;
  1059. struct REPARSE_DATA_BUFFER *rp = NULL;
  1060. bool rp_inserted = false;
  1061. /* New file will be resident or non resident. */
  1062. const bool new_file_resident = 1;
  1063. if (!fnd)
  1064. ni_lock_dir(dir_ni);
  1065. dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
  1066. if (!dir_root) {
  1067. err = -EINVAL;
  1068. goto out1;
  1069. }
  1070. if (S_ISDIR(mode)) {
  1071. /* Use parent's directory attributes. */
  1072. fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
  1073. FILE_ATTRIBUTE_ARCHIVE;
  1074. /*
  1075. * By default child directory inherits parent attributes.
  1076. * Root directory is hidden + system.
  1077. * Make an exception for children in root.
  1078. */
  1079. if (dir->i_ino == MFT_REC_ROOT)
  1080. fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
  1081. } else if (S_ISLNK(mode)) {
  1082. /* It is good idea that link should be the same type (file/dir) as target */
  1083. fa = FILE_ATTRIBUTE_REPARSE_POINT;
  1084. /*
  1085. * Linux: there are dir/file/symlink and so on.
  1086. * NTFS: symlinks are "dir + reparse" or "file + reparse"
  1087. * It is good idea to create:
  1088. * dir + reparse if 'symname' points to directory
  1089. * or
  1090. * file + reparse if 'symname' points to file
  1091. * Unfortunately kern_path hangs if symname contains 'dir'.
  1092. */
  1093. /*
  1094. * struct path path;
  1095. *
  1096. * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
  1097. * struct inode *target = d_inode(path.dentry);
  1098. *
  1099. * if (S_ISDIR(target->i_mode))
  1100. * fa |= FILE_ATTRIBUTE_DIRECTORY;
  1101. * // if ( target->i_sb == sb ){
  1102. * // use relative path?
  1103. * // }
  1104. * path_put(&path);
  1105. * }
  1106. */
  1107. } else if (S_ISREG(mode)) {
  1108. if (sbi->options->sparse) {
  1109. /* Sparsed regular file, cause option 'sparse'. */
  1110. fa = FILE_ATTRIBUTE_SPARSE_FILE |
  1111. FILE_ATTRIBUTE_ARCHIVE;
  1112. } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
  1113. /* Compressed regular file, if parent is compressed. */
  1114. fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
  1115. } else {
  1116. /* Regular file, default attributes. */
  1117. fa = FILE_ATTRIBUTE_ARCHIVE;
  1118. }
  1119. } else {
  1120. fa = FILE_ATTRIBUTE_ARCHIVE;
  1121. }
  1122. /* If option "hide_dot_files" then set hidden attribute for dot files. */
  1123. if (sbi->options->hide_dot_files && name->name[0] == '.')
  1124. fa |= FILE_ATTRIBUTE_HIDDEN;
  1125. if (!(mode & 0222))
  1126. fa |= FILE_ATTRIBUTE_READONLY;
  1127. /* Allocate PATH_MAX bytes. */
  1128. new_de = kzalloc(PATH_MAX, GFP_KERNEL);
  1129. if (!new_de) {
  1130. err = -ENOMEM;
  1131. goto out1;
  1132. }
  1133. /* Avoid any operation if inode is bad. */
  1134. if (unlikely(is_bad_ni(dir_ni))) {
  1135. err = -EINVAL;
  1136. goto out2;
  1137. }
  1138. if (unlikely(ntfs3_forced_shutdown(sb))) {
  1139. err = -EIO;
  1140. goto out2;
  1141. }
  1142. /* Mark rw ntfs as dirty. it will be cleared at umount. */
  1143. ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
  1144. /* Step 1: allocate and fill new mft record. */
  1145. err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
  1146. if (err)
  1147. goto out2;
  1148. ni = ntfs_new_inode(sbi, ino, S_ISDIR(mode) ? RECORD_FLAG_DIR : 0);
  1149. if (IS_ERR(ni)) {
  1150. err = PTR_ERR(ni);
  1151. ni = NULL;
  1152. goto out3;
  1153. }
  1154. inode = &ni->vfs_inode;
  1155. inode_init_owner(idmap, inode, dir, mode);
  1156. mode = inode->i_mode;
  1157. ni->i_crtime = current_time(inode);
  1158. rec = ni->mi.mrec;
  1159. rec->hard_links = cpu_to_le16(1);
  1160. attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
  1161. /* Get default security id. */
  1162. sd = s_default_security;
  1163. sd_size = sizeof(s_default_security);
  1164. if (is_ntfs3(sbi)) {
  1165. security_id = dir_ni->std_security_id;
  1166. if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
  1167. security_id = sbi->security.def_security_id;
  1168. if (security_id == SECURITY_ID_INVALID &&
  1169. !ntfs_insert_security(sbi, sd, sd_size,
  1170. &security_id, NULL))
  1171. sbi->security.def_security_id = security_id;
  1172. }
  1173. }
  1174. /* Insert standard info. */
  1175. std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
  1176. if (security_id == SECURITY_ID_INVALID) {
  1177. dsize = sizeof(struct ATTR_STD_INFO);
  1178. } else {
  1179. dsize = sizeof(struct ATTR_STD_INFO5);
  1180. std5->security_id = security_id;
  1181. ni->std_security_id = security_id;
  1182. }
  1183. asize = SIZEOF_RESIDENT + dsize;
  1184. attr->type = ATTR_STD;
  1185. attr->size = cpu_to_le32(asize);
  1186. attr->id = cpu_to_le16(aid++);
  1187. attr->res.data_off = SIZEOF_RESIDENT_LE;
  1188. attr->res.data_size = cpu_to_le32(dsize);
  1189. std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
  1190. kernel2nt(&ni->i_crtime);
  1191. std5->fa = ni->std_fa = fa;
  1192. attr = Add2Ptr(attr, asize);
  1193. /* Insert file name. */
  1194. err = fill_name_de(sbi, new_de, name, uni);
  1195. if (err)
  1196. goto out4;
  1197. mi_get_ref(&ni->mi, &new_de->ref);
  1198. fname = (struct ATTR_FILE_NAME *)(new_de + 1);
  1199. if (sbi->options->windows_names &&
  1200. !valid_windows_name(sbi, (struct le_str *)&fname->name_len)) {
  1201. err = -EINVAL;
  1202. goto out4;
  1203. }
  1204. mi_get_ref(&dir_ni->mi, &fname->home);
  1205. fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
  1206. fname->dup.a_time = std5->cr_time;
  1207. fname->dup.alloc_size = fname->dup.data_size = 0;
  1208. fname->dup.fa = std5->fa;
  1209. fname->dup.extend_data = S_ISLNK(mode) ? IO_REPARSE_TAG_SYMLINK : 0;
  1210. dsize = le16_to_cpu(new_de->key_size);
  1211. asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
  1212. attr->type = ATTR_NAME;
  1213. attr->size = cpu_to_le32(asize);
  1214. attr->res.data_off = SIZEOF_RESIDENT_LE;
  1215. attr->res.flags = RESIDENT_FLAG_INDEXED;
  1216. attr->id = cpu_to_le16(aid++);
  1217. attr->res.data_size = cpu_to_le32(dsize);
  1218. memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
  1219. attr = Add2Ptr(attr, asize);
  1220. if (security_id == SECURITY_ID_INVALID) {
  1221. /* Insert security attribute. */
  1222. asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
  1223. attr->type = ATTR_SECURE;
  1224. attr->size = cpu_to_le32(asize);
  1225. attr->id = cpu_to_le16(aid++);
  1226. attr->res.data_off = SIZEOF_RESIDENT_LE;
  1227. attr->res.data_size = cpu_to_le32(sd_size);
  1228. memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
  1229. attr = Add2Ptr(attr, asize);
  1230. }
  1231. attr->id = cpu_to_le16(aid++);
  1232. if (fa & FILE_ATTRIBUTE_DIRECTORY) {
  1233. /*
  1234. * Regular directory or symlink to directory.
  1235. * Create root attribute.
  1236. */
  1237. dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
  1238. asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
  1239. attr->type = ATTR_ROOT;
  1240. attr->size = cpu_to_le32(asize);
  1241. attr->name_len = ARRAY_SIZE(I30_NAME);
  1242. attr->name_off = SIZEOF_RESIDENT_LE;
  1243. attr->res.data_off =
  1244. cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
  1245. attr->res.data_size = cpu_to_le32(dsize);
  1246. memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
  1247. sizeof(I30_NAME));
  1248. root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
  1249. memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
  1250. root->ihdr.de_off = cpu_to_le32(sizeof(struct INDEX_HDR));
  1251. root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
  1252. sizeof(struct NTFS_DE));
  1253. root->ihdr.total = root->ihdr.used;
  1254. e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
  1255. e->size = cpu_to_le16(sizeof(struct NTFS_DE));
  1256. e->flags = NTFS_IE_LAST;
  1257. } else if (S_ISLNK(mode)) {
  1258. /*
  1259. * Symlink to file.
  1260. * Create empty resident data attribute.
  1261. */
  1262. asize = SIZEOF_RESIDENT;
  1263. /* Insert empty ATTR_DATA */
  1264. attr->type = ATTR_DATA;
  1265. attr->size = cpu_to_le32(SIZEOF_RESIDENT);
  1266. attr->name_off = SIZEOF_RESIDENT_LE;
  1267. attr->res.data_off = SIZEOF_RESIDENT_LE;
  1268. } else if (!new_file_resident && S_ISREG(mode)) {
  1269. /*
  1270. * Regular file. Create empty non resident data attribute.
  1271. */
  1272. attr->type = ATTR_DATA;
  1273. attr->non_res = 1;
  1274. attr->nres.evcn = cpu_to_le64(-1ll);
  1275. if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
  1276. attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
  1277. attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
  1278. attr->flags = ATTR_FLAG_SPARSED;
  1279. asize = SIZEOF_NONRESIDENT_EX + 8;
  1280. } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
  1281. attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
  1282. attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
  1283. attr->flags = ATTR_FLAG_COMPRESSED;
  1284. attr->nres.c_unit = NTFS_LZNT_CUNIT;
  1285. asize = SIZEOF_NONRESIDENT_EX + 8;
  1286. } else {
  1287. attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
  1288. attr->name_off = SIZEOF_NONRESIDENT_LE;
  1289. asize = SIZEOF_NONRESIDENT + 8;
  1290. }
  1291. attr->nres.run_off = attr->name_off;
  1292. } else {
  1293. /*
  1294. * Node. Create empty resident data attribute.
  1295. */
  1296. attr->type = ATTR_DATA;
  1297. attr->size = cpu_to_le32(SIZEOF_RESIDENT);
  1298. attr->name_off = SIZEOF_RESIDENT_LE;
  1299. if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
  1300. attr->flags = ATTR_FLAG_SPARSED;
  1301. else if (fa & FILE_ATTRIBUTE_COMPRESSED)
  1302. attr->flags = ATTR_FLAG_COMPRESSED;
  1303. attr->res.data_off = SIZEOF_RESIDENT_LE;
  1304. asize = SIZEOF_RESIDENT;
  1305. ni->ni_flags |= NI_FLAG_RESIDENT;
  1306. }
  1307. if (S_ISDIR(mode)) {
  1308. ni->ni_flags |= NI_FLAG_DIR;
  1309. err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
  1310. if (err)
  1311. goto out4;
  1312. } else if (S_ISLNK(mode)) {
  1313. rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
  1314. if (IS_ERR(rp)) {
  1315. err = PTR_ERR(rp);
  1316. rp = NULL;
  1317. goto out4;
  1318. }
  1319. /*
  1320. * Insert ATTR_REPARSE.
  1321. */
  1322. attr = Add2Ptr(attr, asize);
  1323. attr->type = ATTR_REPARSE;
  1324. attr->id = cpu_to_le16(aid++);
  1325. /* Resident or non resident? */
  1326. asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
  1327. t16 = PtrOffset(rec, attr);
  1328. /*
  1329. * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
  1330. * It is good idea to keep extended attributes resident.
  1331. */
  1332. if (asize + t16 + 0x78 + 8 > sbi->record_size) {
  1333. CLST alen;
  1334. CLST clst = bytes_to_cluster(sbi, nsize);
  1335. /* Bytes per runs. */
  1336. t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
  1337. attr->non_res = 1;
  1338. attr->nres.evcn = cpu_to_le64(clst - 1);
  1339. attr->name_off = SIZEOF_NONRESIDENT_LE;
  1340. attr->nres.run_off = attr->name_off;
  1341. attr->nres.data_size = cpu_to_le64(nsize);
  1342. attr->nres.valid_size = attr->nres.data_size;
  1343. attr->nres.alloc_size =
  1344. cpu_to_le64(ntfs_up_cluster(sbi, nsize));
  1345. err = attr_allocate_clusters(sbi, &ni->file.run, NULL,
  1346. 0, 0, clst, NULL,
  1347. ALLOCATE_DEF, &alen, 0,
  1348. NULL, NULL);
  1349. if (err)
  1350. goto out5;
  1351. err = run_pack(&ni->file.run, 0, clst,
  1352. Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
  1353. &vcn);
  1354. if (err < 0)
  1355. goto out5;
  1356. if (vcn != clst) {
  1357. err = -EINVAL;
  1358. goto out5;
  1359. }
  1360. asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
  1361. /* Write non resident data. */
  1362. err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp,
  1363. nsize, 0);
  1364. if (err)
  1365. goto out5;
  1366. } else {
  1367. attr->res.data_off = SIZEOF_RESIDENT_LE;
  1368. attr->res.data_size = cpu_to_le32(nsize);
  1369. memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
  1370. }
  1371. /* Size of symlink equals the length of input string. */
  1372. inode->i_size = size;
  1373. attr->size = cpu_to_le32(asize);
  1374. err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
  1375. &new_de->ref);
  1376. if (err)
  1377. goto out5;
  1378. rp_inserted = true;
  1379. }
  1380. attr = Add2Ptr(attr, asize);
  1381. attr->type = ATTR_END;
  1382. rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
  1383. rec->next_attr_id = cpu_to_le16(aid);
  1384. inode->i_generation = le16_to_cpu(rec->seq);
  1385. if (S_ISDIR(mode)) {
  1386. inode->i_op = &ntfs_dir_inode_operations;
  1387. inode->i_fop = unlikely(is_legacy_ntfs(sb)) ?
  1388. &ntfs_legacy_dir_operations :
  1389. &ntfs_dir_operations;
  1390. } else if (S_ISLNK(mode)) {
  1391. inode->i_op = &ntfs_link_inode_operations;
  1392. inode->i_fop = NULL;
  1393. inode->i_mapping->a_ops = &ntfs_aops;
  1394. inode->i_size = size;
  1395. inode_nohighmem(inode);
  1396. } else if (S_ISREG(mode)) {
  1397. inode->i_op = &ntfs_file_inode_operations;
  1398. inode->i_fop = unlikely(is_legacy_ntfs(sb)) ?
  1399. &ntfs_legacy_file_operations :
  1400. &ntfs_file_operations;
  1401. inode->i_mapping->a_ops = is_compressed(ni) ? &ntfs_aops_cmpr :
  1402. &ntfs_aops;
  1403. init_rwsem(&ni->file.run_lock);
  1404. } else {
  1405. inode->i_op = &ntfs_special_inode_operations;
  1406. init_special_inode(inode, mode, dev);
  1407. }
  1408. #ifdef CONFIG_NTFS3_FS_POSIX_ACL
  1409. if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
  1410. err = ntfs_init_acl(idmap, inode, dir);
  1411. if (err)
  1412. goto out5;
  1413. } else
  1414. #endif
  1415. {
  1416. inode->i_flags |= S_NOSEC;
  1417. }
  1418. if (!S_ISLNK(mode)) {
  1419. /*
  1420. * ntfs_init_acl and ntfs_save_wsl_perm update extended attribute.
  1421. * The packed size of extended attribute is stored in direntry too.
  1422. * 'fname' here points to inside new_de.
  1423. */
  1424. err = ntfs_save_wsl_perm(inode, &fname->dup.extend_data);
  1425. if (err)
  1426. goto out6;
  1427. /*
  1428. * update ea_size in file_name attribute too.
  1429. * Use ni_find_attr cause layout of MFT record may be changed
  1430. * in ntfs_init_acl and ntfs_save_wsl_perm.
  1431. */
  1432. attr = ni_find_attr(ni, NULL, NULL, ATTR_NAME, NULL, 0, NULL,
  1433. NULL);
  1434. if (attr) {
  1435. struct ATTR_FILE_NAME *fn;
  1436. fn = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
  1437. if (fn)
  1438. fn->dup.extend_data = fname->dup.extend_data;
  1439. }
  1440. }
  1441. /* We do not need to update parent directory later */
  1442. ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
  1443. /* Step 2: Add new name in index. */
  1444. err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
  1445. if (err)
  1446. goto out6;
  1447. /*
  1448. * Call 'd_instantiate' after inode->i_op is set
  1449. * but before finish_open.
  1450. */
  1451. d_instantiate(dentry, inode);
  1452. /* Set original time. inode times (i_ctime) may be changed in ntfs_init_acl. */
  1453. inode_set_atime_to_ts(inode, ni->i_crtime);
  1454. inode_set_ctime_to_ts(inode, ni->i_crtime);
  1455. inode_set_mtime_to_ts(inode, ni->i_crtime);
  1456. inode_set_mtime_to_ts(dir, ni->i_crtime);
  1457. inode_set_ctime_to_ts(dir, ni->i_crtime);
  1458. mark_inode_dirty(dir);
  1459. mark_inode_dirty(inode);
  1460. /* Normal exit. */
  1461. goto out2;
  1462. out6:
  1463. attr = ni_find_attr(ni, NULL, NULL, ATTR_EA, NULL, 0, NULL, NULL);
  1464. if (attr && attr->non_res) {
  1465. /* Delete ATTR_EA, if non-resident. */
  1466. struct runs_tree run;
  1467. run_init(&run);
  1468. attr_set_size(ni, ATTR_EA, NULL, 0, &run, 0, NULL, false);
  1469. run_close(&run);
  1470. }
  1471. if (rp_inserted)
  1472. ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
  1473. out5:
  1474. if (!S_ISDIR(mode))
  1475. run_deallocate(sbi, &ni->file.run, false);
  1476. out4:
  1477. clear_rec_inuse(rec);
  1478. clear_nlink(inode);
  1479. ni->mi.dirty = false;
  1480. discard_new_inode(inode);
  1481. out3:
  1482. ntfs_mark_rec_free(sbi, ino, false);
  1483. out2:
  1484. kfree(new_de);
  1485. kfree(rp);
  1486. out1:
  1487. if (!fnd)
  1488. ni_unlock(dir_ni);
  1489. if (!err)
  1490. unlock_new_inode(inode);
  1491. return err;
  1492. }
  1493. int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
  1494. {
  1495. int err;
  1496. struct ntfs_inode *ni = ntfs_i(inode);
  1497. struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
  1498. struct NTFS_DE *de;
  1499. /* Allocate PATH_MAX bytes. */
  1500. de = kzalloc(PATH_MAX, GFP_KERNEL);
  1501. if (!de)
  1502. return -ENOMEM;
  1503. /* Mark rw ntfs as dirty. It will be cleared at umount. */
  1504. ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
  1505. /* Construct 'de'. */
  1506. err = fill_name_de(sbi, de, &dentry->d_name, NULL);
  1507. if (err)
  1508. goto out;
  1509. err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
  1510. out:
  1511. kfree(de);
  1512. return err;
  1513. }
  1514. /*
  1515. * ntfs_unlink_inode
  1516. *
  1517. * inode_operations::unlink
  1518. * inode_operations::rmdir
  1519. */
  1520. int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
  1521. {
  1522. int err;
  1523. struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
  1524. struct inode *inode = d_inode(dentry);
  1525. struct ntfs_inode *ni = ntfs_i(inode);
  1526. struct ntfs_inode *dir_ni = ntfs_i(dir);
  1527. struct NTFS_DE *de, *de2 = NULL;
  1528. int undo_remove;
  1529. if (ntfs_is_meta_file(sbi, ni->mi.rno))
  1530. return -EINVAL;
  1531. de = kzalloc(PATH_MAX, GFP_KERNEL);
  1532. if (!de)
  1533. return -ENOMEM;
  1534. ni_lock(ni);
  1535. if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
  1536. err = -ENOTEMPTY;
  1537. goto out;
  1538. }
  1539. err = fill_name_de(sbi, de, &dentry->d_name, NULL);
  1540. if (err < 0)
  1541. goto out;
  1542. undo_remove = 0;
  1543. err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
  1544. if (!err) {
  1545. drop_nlink(inode);
  1546. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  1547. mark_inode_dirty(dir);
  1548. inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
  1549. if (inode->i_nlink)
  1550. mark_inode_dirty(inode);
  1551. } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
  1552. _ntfs_bad_inode(inode);
  1553. } else {
  1554. if (ni_is_dirty(dir))
  1555. mark_inode_dirty(dir);
  1556. if (ni_is_dirty(inode))
  1557. mark_inode_dirty(inode);
  1558. }
  1559. out:
  1560. ni_unlock(ni);
  1561. kfree(de);
  1562. return err;
  1563. }
  1564. void ntfs_evict_inode(struct inode *inode)
  1565. {
  1566. truncate_inode_pages_final(&inode->i_data);
  1567. invalidate_inode_buffers(inode);
  1568. clear_inode(inode);
  1569. ni_clear(ntfs_i(inode));
  1570. }
  1571. /*
  1572. * ntfs_translate_junction
  1573. *
  1574. * Translate a Windows junction target to the Linux equivalent.
  1575. * On junctions, targets are always absolute (they include the drive
  1576. * letter). We have no way of knowing if the target is for the current
  1577. * mounted device or not so we just assume it is.
  1578. */
  1579. static int ntfs_translate_junction(const struct super_block *sb,
  1580. const struct dentry *link_de, char *target,
  1581. int target_len, int target_max)
  1582. {
  1583. int tl_len, err = target_len;
  1584. char *link_path_buffer = NULL, *link_path;
  1585. char *translated = NULL;
  1586. char *target_start;
  1587. int copy_len;
  1588. link_path_buffer = kmalloc(PATH_MAX, GFP_NOFS);
  1589. if (!link_path_buffer) {
  1590. err = -ENOMEM;
  1591. goto out;
  1592. }
  1593. /* Get link path, relative to mount point */
  1594. link_path = dentry_path_raw(link_de, link_path_buffer, PATH_MAX);
  1595. if (IS_ERR(link_path)) {
  1596. ntfs_err(sb, "Error getting link path");
  1597. err = -EINVAL;
  1598. goto out;
  1599. }
  1600. translated = kmalloc(PATH_MAX, GFP_NOFS);
  1601. if (!translated) {
  1602. err = -ENOMEM;
  1603. goto out;
  1604. }
  1605. /* Make translated path a relative path to mount point */
  1606. strcpy(translated, "./");
  1607. ++link_path; /* Skip leading / */
  1608. for (tl_len = sizeof("./") - 1; *link_path; ++link_path) {
  1609. if (*link_path == '/') {
  1610. if (PATH_MAX - tl_len < sizeof("../")) {
  1611. ntfs_err(sb,
  1612. "Link path %s has too many components",
  1613. link_path);
  1614. err = -EINVAL;
  1615. goto out;
  1616. }
  1617. strcpy(translated + tl_len, "../");
  1618. tl_len += sizeof("../") - 1;
  1619. }
  1620. }
  1621. /* Skip drive letter */
  1622. target_start = target;
  1623. while (*target_start && *target_start != ':')
  1624. ++target_start;
  1625. if (!*target_start) {
  1626. ntfs_err(sb, "Link target (%s) missing drive separator",
  1627. target);
  1628. err = -EINVAL;
  1629. goto out;
  1630. }
  1631. /* Skip drive separator and leading /, if exists */
  1632. target_start += 1 + (target_start[1] == '/');
  1633. copy_len = target_len - (target_start - target);
  1634. if (PATH_MAX - tl_len <= copy_len) {
  1635. ntfs_err(sb, "Link target %s too large for buffer (%d <= %d)",
  1636. target_start, PATH_MAX - tl_len, copy_len);
  1637. err = -EINVAL;
  1638. goto out;
  1639. }
  1640. /* translated path has a trailing / and target_start does not */
  1641. strcpy(translated + tl_len, target_start);
  1642. tl_len += copy_len;
  1643. if (target_max <= tl_len) {
  1644. ntfs_err(sb, "Target path %s too large for buffer (%d <= %d)",
  1645. translated, target_max, tl_len);
  1646. err = -EINVAL;
  1647. goto out;
  1648. }
  1649. strcpy(target, translated);
  1650. err = tl_len;
  1651. out:
  1652. kfree(link_path_buffer);
  1653. kfree(translated);
  1654. return err;
  1655. }
  1656. static noinline int ntfs_readlink_hlp(const struct dentry *link_de,
  1657. struct inode *inode, char *buffer,
  1658. int buflen)
  1659. {
  1660. int i, err = -EINVAL;
  1661. struct ntfs_inode *ni = ntfs_i(inode);
  1662. struct super_block *sb = inode->i_sb;
  1663. struct ntfs_sb_info *sbi = sb->s_fs_info;
  1664. u64 size;
  1665. u16 ulen = 0;
  1666. void *to_free = NULL;
  1667. struct REPARSE_DATA_BUFFER *rp;
  1668. const __le16 *uname;
  1669. struct ATTRIB *attr;
  1670. /* Reparse data present. Try to parse it. */
  1671. static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
  1672. static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
  1673. *buffer = 0;
  1674. attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
  1675. if (!attr)
  1676. goto out;
  1677. if (!attr->non_res) {
  1678. rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
  1679. if (!rp)
  1680. goto out;
  1681. size = le32_to_cpu(attr->res.data_size);
  1682. } else {
  1683. size = le64_to_cpu(attr->nres.data_size);
  1684. rp = NULL;
  1685. }
  1686. if (size > sbi->reparse.max_size || size <= sizeof(u32))
  1687. goto out;
  1688. if (!rp) {
  1689. rp = kmalloc(size, GFP_NOFS);
  1690. if (!rp) {
  1691. err = -ENOMEM;
  1692. goto out;
  1693. }
  1694. to_free = rp;
  1695. /* Read into temporal buffer. */
  1696. err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
  1697. if (err)
  1698. goto out;
  1699. }
  1700. /* Microsoft Tag. */
  1701. switch (rp->ReparseTag) {
  1702. case IO_REPARSE_TAG_MOUNT_POINT:
  1703. /* Mount points and junctions. */
  1704. /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
  1705. if (size <= offsetof(struct REPARSE_DATA_BUFFER,
  1706. MountPointReparseBuffer.PathBuffer))
  1707. goto out;
  1708. uname = Add2Ptr(rp,
  1709. offsetof(struct REPARSE_DATA_BUFFER,
  1710. MountPointReparseBuffer.PathBuffer) +
  1711. le16_to_cpu(rp->MountPointReparseBuffer
  1712. .PrintNameOffset));
  1713. ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
  1714. break;
  1715. case IO_REPARSE_TAG_SYMLINK:
  1716. /* FolderSymbolicLink */
  1717. /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
  1718. if (size <= offsetof(struct REPARSE_DATA_BUFFER,
  1719. SymbolicLinkReparseBuffer.PathBuffer))
  1720. goto out;
  1721. uname = Add2Ptr(
  1722. rp, offsetof(struct REPARSE_DATA_BUFFER,
  1723. SymbolicLinkReparseBuffer.PathBuffer) +
  1724. le16_to_cpu(rp->SymbolicLinkReparseBuffer
  1725. .PrintNameOffset));
  1726. ulen = le16_to_cpu(
  1727. rp->SymbolicLinkReparseBuffer.PrintNameLength);
  1728. break;
  1729. case IO_REPARSE_TAG_CLOUD:
  1730. case IO_REPARSE_TAG_CLOUD_1:
  1731. case IO_REPARSE_TAG_CLOUD_2:
  1732. case IO_REPARSE_TAG_CLOUD_3:
  1733. case IO_REPARSE_TAG_CLOUD_4:
  1734. case IO_REPARSE_TAG_CLOUD_5:
  1735. case IO_REPARSE_TAG_CLOUD_6:
  1736. case IO_REPARSE_TAG_CLOUD_7:
  1737. case IO_REPARSE_TAG_CLOUD_8:
  1738. case IO_REPARSE_TAG_CLOUD_9:
  1739. case IO_REPARSE_TAG_CLOUD_A:
  1740. case IO_REPARSE_TAG_CLOUD_B:
  1741. case IO_REPARSE_TAG_CLOUD_C:
  1742. case IO_REPARSE_TAG_CLOUD_D:
  1743. case IO_REPARSE_TAG_CLOUD_E:
  1744. case IO_REPARSE_TAG_CLOUD_F:
  1745. err = sizeof("OneDrive") - 1;
  1746. if (err > buflen)
  1747. err = buflen;
  1748. memcpy(buffer, "OneDrive", err);
  1749. goto out;
  1750. default:
  1751. if (IsReparseTagMicrosoft(rp->ReparseTag)) {
  1752. /* Unknown Microsoft Tag. */
  1753. goto out;
  1754. }
  1755. if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
  1756. size <= sizeof(struct REPARSE_POINT)) {
  1757. goto out;
  1758. }
  1759. /* Users tag. */
  1760. uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
  1761. ulen = le16_to_cpu(rp->ReparseDataLength) -
  1762. sizeof(struct REPARSE_POINT);
  1763. }
  1764. /* Convert nlen from bytes to UNICODE chars. */
  1765. ulen >>= 1;
  1766. /* Check that name is available. */
  1767. if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
  1768. goto out;
  1769. /* If name is already zero terminated then truncate it now. */
  1770. if (!uname[ulen - 1])
  1771. ulen -= 1;
  1772. err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
  1773. if (err < 0)
  1774. goto out;
  1775. /* Translate Windows '\' into Linux '/'. */
  1776. for (i = 0; i < err; i++) {
  1777. if (buffer[i] == '\\')
  1778. buffer[i] = '/';
  1779. }
  1780. /* Always set last zero. */
  1781. buffer[err] = 0;
  1782. /* If this is a junction, translate the link target. */
  1783. if (rp->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
  1784. err = ntfs_translate_junction(sb, link_de, buffer, err, buflen);
  1785. out:
  1786. kfree(to_free);
  1787. return err;
  1788. }
  1789. static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
  1790. struct delayed_call *done)
  1791. {
  1792. int err;
  1793. char *ret;
  1794. if (!de)
  1795. return ERR_PTR(-ECHILD);
  1796. ret = kmalloc(PAGE_SIZE, GFP_NOFS);
  1797. if (!ret)
  1798. return ERR_PTR(-ENOMEM);
  1799. err = ntfs_readlink_hlp(de, inode, ret, PAGE_SIZE);
  1800. if (err < 0) {
  1801. kfree(ret);
  1802. return ERR_PTR(err);
  1803. }
  1804. set_delayed_call(done, kfree_link, ret);
  1805. return ret;
  1806. }
  1807. // clang-format off
  1808. const struct inode_operations ntfs_link_inode_operations = {
  1809. .get_link = ntfs_get_link,
  1810. .setattr = ntfs_setattr,
  1811. .listxattr = ntfs_listxattr,
  1812. };
  1813. const struct address_space_operations ntfs_aops = {
  1814. .read_folio = ntfs_read_folio,
  1815. .readahead = ntfs_readahead,
  1816. .writepages = ntfs_writepages,
  1817. .bmap = ntfs_bmap,
  1818. .dirty_folio = iomap_dirty_folio,
  1819. .migrate_folio = filemap_migrate_folio,
  1820. .release_folio = iomap_release_folio,
  1821. .invalidate_folio = iomap_invalidate_folio,
  1822. };
  1823. const struct address_space_operations ntfs_aops_cmpr = {
  1824. .read_folio = ntfs_read_folio,
  1825. .dirty_folio = iomap_dirty_folio,
  1826. .release_folio = iomap_release_folio,
  1827. .invalidate_folio = iomap_invalidate_folio,
  1828. };
  1829. const struct iomap_ops ntfs_iomap_ops = {
  1830. .iomap_begin = ntfs_iomap_begin,
  1831. .iomap_end = ntfs_iomap_end,
  1832. };
  1833. const struct iomap_write_ops ntfs_iomap_folio_ops = {
  1834. .put_folio = ntfs_iomap_put_folio,
  1835. };
  1836. // clang-format on