dir.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /**
  2. * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2007 Anton Altaparmakov
  5. * Copyright (c) 2002 Richard Russon
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/buffer_head.h>
  23. #include "dir.h"
  24. #include "aops.h"
  25. #include "attrib.h"
  26. #include "mft.h"
  27. #include "debug.h"
  28. #include "ntfs.h"
  29. /**
  30. * The little endian Unicode string $I30 as a global constant.
  31. */
  32. ntfschar I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
  33. cpu_to_le16('3'), cpu_to_le16('0'), 0 };
  34. /**
  35. * ntfs_lookup_inode_by_name - find an inode in a directory given its name
  36. * @dir_ni: ntfs inode of the directory in which to search for the name
  37. * @uname: Unicode name for which to search in the directory
  38. * @uname_len: length of the name @uname in Unicode characters
  39. * @res: return the found file name if necessary (see below)
  40. *
  41. * Look for an inode with name @uname in the directory with inode @dir_ni.
  42. * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
  43. * the Unicode name. If the name is found in the directory, the corresponding
  44. * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
  45. * is a 64-bit number containing the sequence number.
  46. *
  47. * On error, a negative value is returned corresponding to the error code. In
  48. * particular if the inode is not found -ENOENT is returned. Note that you
  49. * can't just check the return value for being negative, you have to check the
  50. * inode number for being negative which you can extract using MREC(return
  51. * value).
  52. *
  53. * Note, @uname_len does not include the (optional) terminating NULL character.
  54. *
  55. * Note, we look for a case sensitive match first but we also look for a case
  56. * insensitive match at the same time. If we find a case insensitive match, we
  57. * save that for the case that we don't find an exact match, where we return
  58. * the case insensitive match and setup @res (which we allocate!) with the mft
  59. * reference, the file name type, length and with a copy of the little endian
  60. * Unicode file name itself. If we match a file name which is in the DOS name
  61. * space, we only return the mft reference and file name type in @res.
  62. * ntfs_lookup() then uses this to find the long file name in the inode itself.
  63. * This is to avoid polluting the dcache with short file names. We want them to
  64. * work but we don't care for how quickly one can access them. This also fixes
  65. * the dcache aliasing issues.
  66. *
  67. * Locking: - Caller must hold i_mutex on the directory.
  68. * - Each page cache page in the index allocation mapping must be
  69. * locked whilst being accessed otherwise we may find a corrupt
  70. * page due to it being under ->writepage at the moment which
  71. * applies the mst protection fixups before writing out and then
  72. * removes them again after the write is complete after which it
  73. * unlocks the page.
  74. */
  75. MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
  76. const int uname_len, ntfs_name **res)
  77. {
  78. ntfs_volume *vol = dir_ni->vol;
  79. struct super_block *sb = vol->sb;
  80. MFT_RECORD *m;
  81. INDEX_ROOT *ir;
  82. INDEX_ENTRY *ie;
  83. INDEX_ALLOCATION *ia;
  84. u8 *index_end;
  85. u64 mref;
  86. ntfs_attr_search_ctx *ctx;
  87. int err, rc;
  88. VCN vcn, old_vcn;
  89. struct address_space *ia_mapping;
  90. struct page *page;
  91. u8 *kaddr;
  92. ntfs_name *name = NULL;
  93. BUG_ON(!S_ISDIR(VFS_I(dir_ni)->i_mode));
  94. BUG_ON(NInoAttr(dir_ni));
  95. /* Get hold of the mft record for the directory. */
  96. m = map_mft_record(dir_ni);
  97. if (IS_ERR(m)) {
  98. ntfs_error(sb, "map_mft_record() failed with error code %ld.",
  99. -PTR_ERR(m));
  100. return ERR_MREF(PTR_ERR(m));
  101. }
  102. ctx = ntfs_attr_get_search_ctx(dir_ni, m);
  103. if (unlikely(!ctx)) {
  104. err = -ENOMEM;
  105. goto err_out;
  106. }
  107. /* Find the index root attribute in the mft record. */
  108. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
  109. 0, ctx);
  110. if (unlikely(err)) {
  111. if (err == -ENOENT) {
  112. ntfs_error(sb, "Index root attribute missing in "
  113. "directory inode 0x%lx.",
  114. dir_ni->mft_no);
  115. err = -EIO;
  116. }
  117. goto err_out;
  118. }
  119. /* Get to the index root value (it's been verified in read_inode). */
  120. ir = (INDEX_ROOT*)((u8*)ctx->attr +
  121. le16_to_cpu(ctx->attr->data.resident.value_offset));
  122. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  123. /* The first index entry. */
  124. ie = (INDEX_ENTRY*)((u8*)&ir->index +
  125. le32_to_cpu(ir->index.entries_offset));
  126. /*
  127. * Loop until we exceed valid memory (corruption case) or until we
  128. * reach the last entry.
  129. */
  130. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  131. /* Bounds checks. */
  132. if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
  133. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  134. (u8*)ie + le16_to_cpu(ie->key_length) >
  135. index_end)
  136. goto dir_err_out;
  137. /*
  138. * The last entry cannot contain a name. It can however contain
  139. * a pointer to a child node in the B+tree so we just break out.
  140. */
  141. if (ie->flags & INDEX_ENTRY_END)
  142. break;
  143. /*
  144. * We perform a case sensitive comparison and if that matches
  145. * we are done and return the mft reference of the inode (i.e.
  146. * the inode number together with the sequence number for
  147. * consistency checking). We convert it to cpu format before
  148. * returning.
  149. */
  150. if (ntfs_are_names_equal(uname, uname_len,
  151. (ntfschar*)&ie->key.file_name.file_name,
  152. ie->key.file_name.file_name_length,
  153. CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
  154. found_it:
  155. /*
  156. * We have a perfect match, so we don't need to care
  157. * about having matched imperfectly before, so we can
  158. * free name and set *res to NULL.
  159. * However, if the perfect match is a short file name,
  160. * we need to signal this through *res, so that
  161. * ntfs_lookup() can fix dcache aliasing issues.
  162. * As an optimization we just reuse an existing
  163. * allocation of *res.
  164. */
  165. if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
  166. if (!name) {
  167. name = kmalloc(sizeof(ntfs_name),
  168. GFP_NOFS);
  169. if (!name) {
  170. err = -ENOMEM;
  171. goto err_out;
  172. }
  173. }
  174. name->mref = le64_to_cpu(
  175. ie->data.dir.indexed_file);
  176. name->type = FILE_NAME_DOS;
  177. name->len = 0;
  178. *res = name;
  179. } else {
  180. kfree(name);
  181. *res = NULL;
  182. }
  183. mref = le64_to_cpu(ie->data.dir.indexed_file);
  184. ntfs_attr_put_search_ctx(ctx);
  185. unmap_mft_record(dir_ni);
  186. return mref;
  187. }
  188. /*
  189. * For a case insensitive mount, we also perform a case
  190. * insensitive comparison (provided the file name is not in the
  191. * POSIX namespace). If the comparison matches, and the name is
  192. * in the WIN32 namespace, we cache the filename in *res so
  193. * that the caller, ntfs_lookup(), can work on it. If the
  194. * comparison matches, and the name is in the DOS namespace, we
  195. * only cache the mft reference and the file name type (we set
  196. * the name length to zero for simplicity).
  197. */
  198. if (!NVolCaseSensitive(vol) &&
  199. ie->key.file_name.file_name_type &&
  200. ntfs_are_names_equal(uname, uname_len,
  201. (ntfschar*)&ie->key.file_name.file_name,
  202. ie->key.file_name.file_name_length,
  203. IGNORE_CASE, vol->upcase, vol->upcase_len)) {
  204. int name_size = sizeof(ntfs_name);
  205. u8 type = ie->key.file_name.file_name_type;
  206. u8 len = ie->key.file_name.file_name_length;
  207. /* Only one case insensitive matching name allowed. */
  208. if (name) {
  209. ntfs_error(sb, "Found already allocated name "
  210. "in phase 1. Please run chkdsk "
  211. "and if that doesn't find any "
  212. "errors please report you saw "
  213. "this message to "
  214. "linux-ntfs-dev@lists."
  215. "sourceforge.net.");
  216. goto dir_err_out;
  217. }
  218. if (type != FILE_NAME_DOS)
  219. name_size += len * sizeof(ntfschar);
  220. name = kmalloc(name_size, GFP_NOFS);
  221. if (!name) {
  222. err = -ENOMEM;
  223. goto err_out;
  224. }
  225. name->mref = le64_to_cpu(ie->data.dir.indexed_file);
  226. name->type = type;
  227. if (type != FILE_NAME_DOS) {
  228. name->len = len;
  229. memcpy(name->name, ie->key.file_name.file_name,
  230. len * sizeof(ntfschar));
  231. } else
  232. name->len = 0;
  233. *res = name;
  234. }
  235. /*
  236. * Not a perfect match, need to do full blown collation so we
  237. * know which way in the B+tree we have to go.
  238. */
  239. rc = ntfs_collate_names(uname, uname_len,
  240. (ntfschar*)&ie->key.file_name.file_name,
  241. ie->key.file_name.file_name_length, 1,
  242. IGNORE_CASE, vol->upcase, vol->upcase_len);
  243. /*
  244. * If uname collates before the name of the current entry, there
  245. * is definitely no such name in this index but we might need to
  246. * descend into the B+tree so we just break out of the loop.
  247. */
  248. if (rc == -1)
  249. break;
  250. /* The names are not equal, continue the search. */
  251. if (rc)
  252. continue;
  253. /*
  254. * Names match with case insensitive comparison, now try the
  255. * case sensitive comparison, which is required for proper
  256. * collation.
  257. */
  258. rc = ntfs_collate_names(uname, uname_len,
  259. (ntfschar*)&ie->key.file_name.file_name,
  260. ie->key.file_name.file_name_length, 1,
  261. CASE_SENSITIVE, vol->upcase, vol->upcase_len);
  262. if (rc == -1)
  263. break;
  264. if (rc)
  265. continue;
  266. /*
  267. * Perfect match, this will never happen as the
  268. * ntfs_are_names_equal() call will have gotten a match but we
  269. * still treat it correctly.
  270. */
  271. goto found_it;
  272. }
  273. /*
  274. * We have finished with this index without success. Check for the
  275. * presence of a child node and if not present return -ENOENT, unless
  276. * we have got a matching name cached in name in which case return the
  277. * mft reference associated with it.
  278. */
  279. if (!(ie->flags & INDEX_ENTRY_NODE)) {
  280. if (name) {
  281. ntfs_attr_put_search_ctx(ctx);
  282. unmap_mft_record(dir_ni);
  283. return name->mref;
  284. }
  285. ntfs_debug("Entry not found.");
  286. err = -ENOENT;
  287. goto err_out;
  288. } /* Child node present, descend into it. */
  289. /* Consistency check: Verify that an index allocation exists. */
  290. if (!NInoIndexAllocPresent(dir_ni)) {
  291. ntfs_error(sb, "No index allocation attribute but index entry "
  292. "requires one. Directory inode 0x%lx is "
  293. "corrupt or driver bug.", dir_ni->mft_no);
  294. goto err_out;
  295. }
  296. /* Get the starting vcn of the index_block holding the child node. */
  297. vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
  298. ia_mapping = VFS_I(dir_ni)->i_mapping;
  299. /*
  300. * We are done with the index root and the mft record. Release them,
  301. * otherwise we deadlock with ntfs_map_page().
  302. */
  303. ntfs_attr_put_search_ctx(ctx);
  304. unmap_mft_record(dir_ni);
  305. m = NULL;
  306. ctx = NULL;
  307. descend_into_child_node:
  308. /*
  309. * Convert vcn to index into the index allocation attribute in units
  310. * of PAGE_CACHE_SIZE and map the page cache page, reading it from
  311. * disk if necessary.
  312. */
  313. page = ntfs_map_page(ia_mapping, vcn <<
  314. dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
  315. if (IS_ERR(page)) {
  316. ntfs_error(sb, "Failed to map directory index page, error %ld.",
  317. -PTR_ERR(page));
  318. err = PTR_ERR(page);
  319. goto err_out;
  320. }
  321. lock_page(page);
  322. kaddr = (u8*)page_address(page);
  323. fast_descend_into_child_node:
  324. /* Get to the index allocation block. */
  325. ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
  326. dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
  327. /* Bounds checks. */
  328. if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
  329. ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
  330. "inode 0x%lx or driver bug.", dir_ni->mft_no);
  331. goto unm_err_out;
  332. }
  333. /* Catch multi sector transfer fixup errors. */
  334. if (unlikely(!ntfs_is_indx_record(ia->magic))) {
  335. ntfs_error(sb, "Directory index record with vcn 0x%llx is "
  336. "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
  337. (unsigned long long)vcn, dir_ni->mft_no);
  338. goto unm_err_out;
  339. }
  340. if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
  341. ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
  342. "different from expected VCN (0x%llx). "
  343. "Directory inode 0x%lx is corrupt or driver "
  344. "bug.", (unsigned long long)
  345. sle64_to_cpu(ia->index_block_vcn),
  346. (unsigned long long)vcn, dir_ni->mft_no);
  347. goto unm_err_out;
  348. }
  349. if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
  350. dir_ni->itype.index.block_size) {
  351. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  352. "0x%lx has a size (%u) differing from the "
  353. "directory specified size (%u). Directory "
  354. "inode is corrupt or driver bug.",
  355. (unsigned long long)vcn, dir_ni->mft_no,
  356. le32_to_cpu(ia->index.allocated_size) + 0x18,
  357. dir_ni->itype.index.block_size);
  358. goto unm_err_out;
  359. }
  360. index_end = (u8*)ia + dir_ni->itype.index.block_size;
  361. if (index_end > kaddr + PAGE_CACHE_SIZE) {
  362. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  363. "0x%lx crosses page boundary. Impossible! "
  364. "Cannot access! This is probably a bug in the "
  365. "driver.", (unsigned long long)vcn,
  366. dir_ni->mft_no);
  367. goto unm_err_out;
  368. }
  369. index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
  370. if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
  371. ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
  372. "inode 0x%lx exceeds maximum size.",
  373. (unsigned long long)vcn, dir_ni->mft_no);
  374. goto unm_err_out;
  375. }
  376. /* The first index entry. */
  377. ie = (INDEX_ENTRY*)((u8*)&ia->index +
  378. le32_to_cpu(ia->index.entries_offset));
  379. /*
  380. * Iterate similar to above big loop but applied to index buffer, thus
  381. * loop until we exceed valid memory (corruption case) or until we
  382. * reach the last entry.
  383. */
  384. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  385. /* Bounds check. */
  386. if ((u8*)ie < (u8*)ia || (u8*)ie +
  387. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  388. (u8*)ie + le16_to_cpu(ie->key_length) >
  389. index_end) {
  390. ntfs_error(sb, "Index entry out of bounds in "
  391. "directory inode 0x%lx.",
  392. dir_ni->mft_no);
  393. goto unm_err_out;
  394. }
  395. /*
  396. * The last entry cannot contain a name. It can however contain
  397. * a pointer to a child node in the B+tree so we just break out.
  398. */
  399. if (ie->flags & INDEX_ENTRY_END)
  400. break;
  401. /*
  402. * We perform a case sensitive comparison and if that matches
  403. * we are done and return the mft reference of the inode (i.e.
  404. * the inode number together with the sequence number for
  405. * consistency checking). We convert it to cpu format before
  406. * returning.
  407. */
  408. if (ntfs_are_names_equal(uname, uname_len,
  409. (ntfschar*)&ie->key.file_name.file_name,
  410. ie->key.file_name.file_name_length,
  411. CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
  412. found_it2:
  413. /*
  414. * We have a perfect match, so we don't need to care
  415. * about having matched imperfectly before, so we can
  416. * free name and set *res to NULL.
  417. * However, if the perfect match is a short file name,
  418. * we need to signal this through *res, so that
  419. * ntfs_lookup() can fix dcache aliasing issues.
  420. * As an optimization we just reuse an existing
  421. * allocation of *res.
  422. */
  423. if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
  424. if (!name) {
  425. name = kmalloc(sizeof(ntfs_name),
  426. GFP_NOFS);
  427. if (!name) {
  428. err = -ENOMEM;
  429. goto unm_err_out;
  430. }
  431. }
  432. name->mref = le64_to_cpu(
  433. ie->data.dir.indexed_file);
  434. name->type = FILE_NAME_DOS;
  435. name->len = 0;
  436. *res = name;
  437. } else {
  438. kfree(name);
  439. *res = NULL;
  440. }
  441. mref = le64_to_cpu(ie->data.dir.indexed_file);
  442. unlock_page(page);
  443. ntfs_unmap_page(page);
  444. return mref;
  445. }
  446. /*
  447. * For a case insensitive mount, we also perform a case
  448. * insensitive comparison (provided the file name is not in the
  449. * POSIX namespace). If the comparison matches, and the name is
  450. * in the WIN32 namespace, we cache the filename in *res so
  451. * that the caller, ntfs_lookup(), can work on it. If the
  452. * comparison matches, and the name is in the DOS namespace, we
  453. * only cache the mft reference and the file name type (we set
  454. * the name length to zero for simplicity).
  455. */
  456. if (!NVolCaseSensitive(vol) &&
  457. ie->key.file_name.file_name_type &&
  458. ntfs_are_names_equal(uname, uname_len,
  459. (ntfschar*)&ie->key.file_name.file_name,
  460. ie->key.file_name.file_name_length,
  461. IGNORE_CASE, vol->upcase, vol->upcase_len)) {
  462. int name_size = sizeof(ntfs_name);
  463. u8 type = ie->key.file_name.file_name_type;
  464. u8 len = ie->key.file_name.file_name_length;
  465. /* Only one case insensitive matching name allowed. */
  466. if (name) {
  467. ntfs_error(sb, "Found already allocated name "
  468. "in phase 2. Please run chkdsk "
  469. "and if that doesn't find any "
  470. "errors please report you saw "
  471. "this message to "
  472. "linux-ntfs-dev@lists."
  473. "sourceforge.net.");
  474. unlock_page(page);
  475. ntfs_unmap_page(page);
  476. goto dir_err_out;
  477. }
  478. if (type != FILE_NAME_DOS)
  479. name_size += len * sizeof(ntfschar);
  480. name = kmalloc(name_size, GFP_NOFS);
  481. if (!name) {
  482. err = -ENOMEM;
  483. goto unm_err_out;
  484. }
  485. name->mref = le64_to_cpu(ie->data.dir.indexed_file);
  486. name->type = type;
  487. if (type != FILE_NAME_DOS) {
  488. name->len = len;
  489. memcpy(name->name, ie->key.file_name.file_name,
  490. len * sizeof(ntfschar));
  491. } else
  492. name->len = 0;
  493. *res = name;
  494. }
  495. /*
  496. * Not a perfect match, need to do full blown collation so we
  497. * know which way in the B+tree we have to go.
  498. */
  499. rc = ntfs_collate_names(uname, uname_len,
  500. (ntfschar*)&ie->key.file_name.file_name,
  501. ie->key.file_name.file_name_length, 1,
  502. IGNORE_CASE, vol->upcase, vol->upcase_len);
  503. /*
  504. * If uname collates before the name of the current entry, there
  505. * is definitely no such name in this index but we might need to
  506. * descend into the B+tree so we just break out of the loop.
  507. */
  508. if (rc == -1)
  509. break;
  510. /* The names are not equal, continue the search. */
  511. if (rc)
  512. continue;
  513. /*
  514. * Names match with case insensitive comparison, now try the
  515. * case sensitive comparison, which is required for proper
  516. * collation.
  517. */
  518. rc = ntfs_collate_names(uname, uname_len,
  519. (ntfschar*)&ie->key.file_name.file_name,
  520. ie->key.file_name.file_name_length, 1,
  521. CASE_SENSITIVE, vol->upcase, vol->upcase_len);
  522. if (rc == -1)
  523. break;
  524. if (rc)
  525. continue;
  526. /*
  527. * Perfect match, this will never happen as the
  528. * ntfs_are_names_equal() call will have gotten a match but we
  529. * still treat it correctly.
  530. */
  531. goto found_it2;
  532. }
  533. /*
  534. * We have finished with this index buffer without success. Check for
  535. * the presence of a child node.
  536. */
  537. if (ie->flags & INDEX_ENTRY_NODE) {
  538. if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
  539. ntfs_error(sb, "Index entry with child node found in "
  540. "a leaf node in directory inode 0x%lx.",
  541. dir_ni->mft_no);
  542. goto unm_err_out;
  543. }
  544. /* Child node present, descend into it. */
  545. old_vcn = vcn;
  546. vcn = sle64_to_cpup((sle64*)((u8*)ie +
  547. le16_to_cpu(ie->length) - 8));
  548. if (vcn >= 0) {
  549. /* If vcn is in the same page cache page as old_vcn we
  550. * recycle the mapped page. */
  551. if (old_vcn << vol->cluster_size_bits >>
  552. PAGE_CACHE_SHIFT == vcn <<
  553. vol->cluster_size_bits >>
  554. PAGE_CACHE_SHIFT)
  555. goto fast_descend_into_child_node;
  556. unlock_page(page);
  557. ntfs_unmap_page(page);
  558. goto descend_into_child_node;
  559. }
  560. ntfs_error(sb, "Negative child node vcn in directory inode "
  561. "0x%lx.", dir_ni->mft_no);
  562. goto unm_err_out;
  563. }
  564. /*
  565. * No child node present, return -ENOENT, unless we have got a matching
  566. * name cached in name in which case return the mft reference
  567. * associated with it.
  568. */
  569. if (name) {
  570. unlock_page(page);
  571. ntfs_unmap_page(page);
  572. return name->mref;
  573. }
  574. ntfs_debug("Entry not found.");
  575. err = -ENOENT;
  576. unm_err_out:
  577. unlock_page(page);
  578. ntfs_unmap_page(page);
  579. err_out:
  580. if (!err)
  581. err = -EIO;
  582. if (ctx)
  583. ntfs_attr_put_search_ctx(ctx);
  584. if (m)
  585. unmap_mft_record(dir_ni);
  586. if (name) {
  587. kfree(name);
  588. *res = NULL;
  589. }
  590. return ERR_MREF(err);
  591. dir_err_out:
  592. ntfs_error(sb, "Corrupt directory. Aborting lookup.");
  593. goto err_out;
  594. }
  595. #if 0
  596. // TODO: (AIA)
  597. // The algorithm embedded in this code will be required for the time when we
  598. // want to support adding of entries to directories, where we require correct
  599. // collation of file names in order not to cause corruption of the filesystem.
  600. /**
  601. * ntfs_lookup_inode_by_name - find an inode in a directory given its name
  602. * @dir_ni: ntfs inode of the directory in which to search for the name
  603. * @uname: Unicode name for which to search in the directory
  604. * @uname_len: length of the name @uname in Unicode characters
  605. *
  606. * Look for an inode with name @uname in the directory with inode @dir_ni.
  607. * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
  608. * the Unicode name. If the name is found in the directory, the corresponding
  609. * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
  610. * is a 64-bit number containing the sequence number.
  611. *
  612. * On error, a negative value is returned corresponding to the error code. In
  613. * particular if the inode is not found -ENOENT is returned. Note that you
  614. * can't just check the return value for being negative, you have to check the
  615. * inode number for being negative which you can extract using MREC(return
  616. * value).
  617. *
  618. * Note, @uname_len does not include the (optional) terminating NULL character.
  619. */
  620. u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
  621. const int uname_len)
  622. {
  623. ntfs_volume *vol = dir_ni->vol;
  624. struct super_block *sb = vol->sb;
  625. MFT_RECORD *m;
  626. INDEX_ROOT *ir;
  627. INDEX_ENTRY *ie;
  628. INDEX_ALLOCATION *ia;
  629. u8 *index_end;
  630. u64 mref;
  631. ntfs_attr_search_ctx *ctx;
  632. int err, rc;
  633. IGNORE_CASE_BOOL ic;
  634. VCN vcn, old_vcn;
  635. struct address_space *ia_mapping;
  636. struct page *page;
  637. u8 *kaddr;
  638. /* Get hold of the mft record for the directory. */
  639. m = map_mft_record(dir_ni);
  640. if (IS_ERR(m)) {
  641. ntfs_error(sb, "map_mft_record() failed with error code %ld.",
  642. -PTR_ERR(m));
  643. return ERR_MREF(PTR_ERR(m));
  644. }
  645. ctx = ntfs_attr_get_search_ctx(dir_ni, m);
  646. if (!ctx) {
  647. err = -ENOMEM;
  648. goto err_out;
  649. }
  650. /* Find the index root attribute in the mft record. */
  651. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
  652. 0, ctx);
  653. if (unlikely(err)) {
  654. if (err == -ENOENT) {
  655. ntfs_error(sb, "Index root attribute missing in "
  656. "directory inode 0x%lx.",
  657. dir_ni->mft_no);
  658. err = -EIO;
  659. }
  660. goto err_out;
  661. }
  662. /* Get to the index root value (it's been verified in read_inode). */
  663. ir = (INDEX_ROOT*)((u8*)ctx->attr +
  664. le16_to_cpu(ctx->attr->data.resident.value_offset));
  665. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  666. /* The first index entry. */
  667. ie = (INDEX_ENTRY*)((u8*)&ir->index +
  668. le32_to_cpu(ir->index.entries_offset));
  669. /*
  670. * Loop until we exceed valid memory (corruption case) or until we
  671. * reach the last entry.
  672. */
  673. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  674. /* Bounds checks. */
  675. if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
  676. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  677. (u8*)ie + le16_to_cpu(ie->key_length) >
  678. index_end)
  679. goto dir_err_out;
  680. /*
  681. * The last entry cannot contain a name. It can however contain
  682. * a pointer to a child node in the B+tree so we just break out.
  683. */
  684. if (ie->flags & INDEX_ENTRY_END)
  685. break;
  686. /*
  687. * If the current entry has a name type of POSIX, the name is
  688. * case sensitive and not otherwise. This has the effect of us
  689. * not being able to access any POSIX file names which collate
  690. * after the non-POSIX one when they only differ in case, but
  691. * anyone doing screwy stuff like that deserves to burn in
  692. * hell... Doing that kind of stuff on NT4 actually causes
  693. * corruption on the partition even when using SP6a and Linux
  694. * is not involved at all.
  695. */
  696. ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
  697. CASE_SENSITIVE;
  698. /*
  699. * If the names match perfectly, we are done and return the
  700. * mft reference of the inode (i.e. the inode number together
  701. * with the sequence number for consistency checking. We
  702. * convert it to cpu format before returning.
  703. */
  704. if (ntfs_are_names_equal(uname, uname_len,
  705. (ntfschar*)&ie->key.file_name.file_name,
  706. ie->key.file_name.file_name_length, ic,
  707. vol->upcase, vol->upcase_len)) {
  708. found_it:
  709. mref = le64_to_cpu(ie->data.dir.indexed_file);
  710. ntfs_attr_put_search_ctx(ctx);
  711. unmap_mft_record(dir_ni);
  712. return mref;
  713. }
  714. /*
  715. * Not a perfect match, need to do full blown collation so we
  716. * know which way in the B+tree we have to go.
  717. */
  718. rc = ntfs_collate_names(uname, uname_len,
  719. (ntfschar*)&ie->key.file_name.file_name,
  720. ie->key.file_name.file_name_length, 1,
  721. IGNORE_CASE, vol->upcase, vol->upcase_len);
  722. /*
  723. * If uname collates before the name of the current entry, there
  724. * is definitely no such name in this index but we might need to
  725. * descend into the B+tree so we just break out of the loop.
  726. */
  727. if (rc == -1)
  728. break;
  729. /* The names are not equal, continue the search. */
  730. if (rc)
  731. continue;
  732. /*
  733. * Names match with case insensitive comparison, now try the
  734. * case sensitive comparison, which is required for proper
  735. * collation.
  736. */
  737. rc = ntfs_collate_names(uname, uname_len,
  738. (ntfschar*)&ie->key.file_name.file_name,
  739. ie->key.file_name.file_name_length, 1,
  740. CASE_SENSITIVE, vol->upcase, vol->upcase_len);
  741. if (rc == -1)
  742. break;
  743. if (rc)
  744. continue;
  745. /*
  746. * Perfect match, this will never happen as the
  747. * ntfs_are_names_equal() call will have gotten a match but we
  748. * still treat it correctly.
  749. */
  750. goto found_it;
  751. }
  752. /*
  753. * We have finished with this index without success. Check for the
  754. * presence of a child node.
  755. */
  756. if (!(ie->flags & INDEX_ENTRY_NODE)) {
  757. /* No child node, return -ENOENT. */
  758. err = -ENOENT;
  759. goto err_out;
  760. } /* Child node present, descend into it. */
  761. /* Consistency check: Verify that an index allocation exists. */
  762. if (!NInoIndexAllocPresent(dir_ni)) {
  763. ntfs_error(sb, "No index allocation attribute but index entry "
  764. "requires one. Directory inode 0x%lx is "
  765. "corrupt or driver bug.", dir_ni->mft_no);
  766. goto err_out;
  767. }
  768. /* Get the starting vcn of the index_block holding the child node. */
  769. vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
  770. ia_mapping = VFS_I(dir_ni)->i_mapping;
  771. /*
  772. * We are done with the index root and the mft record. Release them,
  773. * otherwise we deadlock with ntfs_map_page().
  774. */
  775. ntfs_attr_put_search_ctx(ctx);
  776. unmap_mft_record(dir_ni);
  777. m = NULL;
  778. ctx = NULL;
  779. descend_into_child_node:
  780. /*
  781. * Convert vcn to index into the index allocation attribute in units
  782. * of PAGE_CACHE_SIZE and map the page cache page, reading it from
  783. * disk if necessary.
  784. */
  785. page = ntfs_map_page(ia_mapping, vcn <<
  786. dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
  787. if (IS_ERR(page)) {
  788. ntfs_error(sb, "Failed to map directory index page, error %ld.",
  789. -PTR_ERR(page));
  790. err = PTR_ERR(page);
  791. goto err_out;
  792. }
  793. lock_page(page);
  794. kaddr = (u8*)page_address(page);
  795. fast_descend_into_child_node:
  796. /* Get to the index allocation block. */
  797. ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
  798. dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
  799. /* Bounds checks. */
  800. if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
  801. ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
  802. "inode 0x%lx or driver bug.", dir_ni->mft_no);
  803. goto unm_err_out;
  804. }
  805. /* Catch multi sector transfer fixup errors. */
  806. if (unlikely(!ntfs_is_indx_record(ia->magic))) {
  807. ntfs_error(sb, "Directory index record with vcn 0x%llx is "
  808. "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
  809. (unsigned long long)vcn, dir_ni->mft_no);
  810. goto unm_err_out;
  811. }
  812. if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
  813. ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
  814. "different from expected VCN (0x%llx). "
  815. "Directory inode 0x%lx is corrupt or driver "
  816. "bug.", (unsigned long long)
  817. sle64_to_cpu(ia->index_block_vcn),
  818. (unsigned long long)vcn, dir_ni->mft_no);
  819. goto unm_err_out;
  820. }
  821. if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
  822. dir_ni->itype.index.block_size) {
  823. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  824. "0x%lx has a size (%u) differing from the "
  825. "directory specified size (%u). Directory "
  826. "inode is corrupt or driver bug.",
  827. (unsigned long long)vcn, dir_ni->mft_no,
  828. le32_to_cpu(ia->index.allocated_size) + 0x18,
  829. dir_ni->itype.index.block_size);
  830. goto unm_err_out;
  831. }
  832. index_end = (u8*)ia + dir_ni->itype.index.block_size;
  833. if (index_end > kaddr + PAGE_CACHE_SIZE) {
  834. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  835. "0x%lx crosses page boundary. Impossible! "
  836. "Cannot access! This is probably a bug in the "
  837. "driver.", (unsigned long long)vcn,
  838. dir_ni->mft_no);
  839. goto unm_err_out;
  840. }
  841. index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
  842. if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
  843. ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
  844. "inode 0x%lx exceeds maximum size.",
  845. (unsigned long long)vcn, dir_ni->mft_no);
  846. goto unm_err_out;
  847. }
  848. /* The first index entry. */
  849. ie = (INDEX_ENTRY*)((u8*)&ia->index +
  850. le32_to_cpu(ia->index.entries_offset));
  851. /*
  852. * Iterate similar to above big loop but applied to index buffer, thus
  853. * loop until we exceed valid memory (corruption case) or until we
  854. * reach the last entry.
  855. */
  856. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  857. /* Bounds check. */
  858. if ((u8*)ie < (u8*)ia || (u8*)ie +
  859. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  860. (u8*)ie + le16_to_cpu(ie->key_length) >
  861. index_end) {
  862. ntfs_error(sb, "Index entry out of bounds in "
  863. "directory inode 0x%lx.",
  864. dir_ni->mft_no);
  865. goto unm_err_out;
  866. }
  867. /*
  868. * The last entry cannot contain a name. It can however contain
  869. * a pointer to a child node in the B+tree so we just break out.
  870. */
  871. if (ie->flags & INDEX_ENTRY_END)
  872. break;
  873. /*
  874. * If the current entry has a name type of POSIX, the name is
  875. * case sensitive and not otherwise. This has the effect of us
  876. * not being able to access any POSIX file names which collate
  877. * after the non-POSIX one when they only differ in case, but
  878. * anyone doing screwy stuff like that deserves to burn in
  879. * hell... Doing that kind of stuff on NT4 actually causes
  880. * corruption on the partition even when using SP6a and Linux
  881. * is not involved at all.
  882. */
  883. ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
  884. CASE_SENSITIVE;
  885. /*
  886. * If the names match perfectly, we are done and return the
  887. * mft reference of the inode (i.e. the inode number together
  888. * with the sequence number for consistency checking. We
  889. * convert it to cpu format before returning.
  890. */
  891. if (ntfs_are_names_equal(uname, uname_len,
  892. (ntfschar*)&ie->key.file_name.file_name,
  893. ie->key.file_name.file_name_length, ic,
  894. vol->upcase, vol->upcase_len)) {
  895. found_it2:
  896. mref = le64_to_cpu(ie->data.dir.indexed_file);
  897. unlock_page(page);
  898. ntfs_unmap_page(page);
  899. return mref;
  900. }
  901. /*
  902. * Not a perfect match, need to do full blown collation so we
  903. * know which way in the B+tree we have to go.
  904. */
  905. rc = ntfs_collate_names(uname, uname_len,
  906. (ntfschar*)&ie->key.file_name.file_name,
  907. ie->key.file_name.file_name_length, 1,
  908. IGNORE_CASE, vol->upcase, vol->upcase_len);
  909. /*
  910. * If uname collates before the name of the current entry, there
  911. * is definitely no such name in this index but we might need to
  912. * descend into the B+tree so we just break out of the loop.
  913. */
  914. if (rc == -1)
  915. break;
  916. /* The names are not equal, continue the search. */
  917. if (rc)
  918. continue;
  919. /*
  920. * Names match with case insensitive comparison, now try the
  921. * case sensitive comparison, which is required for proper
  922. * collation.
  923. */
  924. rc = ntfs_collate_names(uname, uname_len,
  925. (ntfschar*)&ie->key.file_name.file_name,
  926. ie->key.file_name.file_name_length, 1,
  927. CASE_SENSITIVE, vol->upcase, vol->upcase_len);
  928. if (rc == -1)
  929. break;
  930. if (rc)
  931. continue;
  932. /*
  933. * Perfect match, this will never happen as the
  934. * ntfs_are_names_equal() call will have gotten a match but we
  935. * still treat it correctly.
  936. */
  937. goto found_it2;
  938. }
  939. /*
  940. * We have finished with this index buffer without success. Check for
  941. * the presence of a child node.
  942. */
  943. if (ie->flags & INDEX_ENTRY_NODE) {
  944. if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
  945. ntfs_error(sb, "Index entry with child node found in "
  946. "a leaf node in directory inode 0x%lx.",
  947. dir_ni->mft_no);
  948. goto unm_err_out;
  949. }
  950. /* Child node present, descend into it. */
  951. old_vcn = vcn;
  952. vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
  953. if (vcn >= 0) {
  954. /* If vcn is in the same page cache page as old_vcn we
  955. * recycle the mapped page. */
  956. if (old_vcn << vol->cluster_size_bits >>
  957. PAGE_CACHE_SHIFT == vcn <<
  958. vol->cluster_size_bits >>
  959. PAGE_CACHE_SHIFT)
  960. goto fast_descend_into_child_node;
  961. unlock_page(page);
  962. ntfs_unmap_page(page);
  963. goto descend_into_child_node;
  964. }
  965. ntfs_error(sb, "Negative child node vcn in directory inode "
  966. "0x%lx.", dir_ni->mft_no);
  967. goto unm_err_out;
  968. }
  969. /* No child node, return -ENOENT. */
  970. ntfs_debug("Entry not found.");
  971. err = -ENOENT;
  972. unm_err_out:
  973. unlock_page(page);
  974. ntfs_unmap_page(page);
  975. err_out:
  976. if (!err)
  977. err = -EIO;
  978. if (ctx)
  979. ntfs_attr_put_search_ctx(ctx);
  980. if (m)
  981. unmap_mft_record(dir_ni);
  982. return ERR_MREF(err);
  983. dir_err_out:
  984. ntfs_error(sb, "Corrupt directory. Aborting lookup.");
  985. goto err_out;
  986. }
  987. #endif
  988. /**
  989. * ntfs_filldir - ntfs specific filldir method
  990. * @vol: current ntfs volume
  991. * @fpos: position in the directory
  992. * @ndir: ntfs inode of current directory
  993. * @ia_page: page in which the index allocation buffer @ie is in resides
  994. * @ie: current index entry
  995. * @name: buffer to use for the converted name
  996. * @dirent: vfs filldir callback context
  997. * @filldir: vfs filldir callback
  998. *
  999. * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
  1000. * callback.
  1001. *
  1002. * If @ia_page is not NULL it is the locked page containing the index
  1003. * allocation block containing the index entry @ie.
  1004. *
  1005. * Note, we drop (and then reacquire) the page lock on @ia_page across the
  1006. * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
  1007. * since ntfs_lookup() will lock the same page. As an optimization, we do not
  1008. * retake the lock if we are returning a non-zero value as ntfs_readdir()
  1009. * would need to drop the lock immediately anyway.
  1010. */
  1011. static inline int ntfs_filldir(ntfs_volume *vol, loff_t fpos,
  1012. ntfs_inode *ndir, struct page *ia_page, INDEX_ENTRY *ie,
  1013. u8 *name, void *dirent, filldir_t filldir)
  1014. {
  1015. unsigned long mref;
  1016. int name_len, rc;
  1017. unsigned dt_type;
  1018. FILE_NAME_TYPE_FLAGS name_type;
  1019. name_type = ie->key.file_name.file_name_type;
  1020. if (name_type == FILE_NAME_DOS) {
  1021. ntfs_debug("Skipping DOS name space entry.");
  1022. return 0;
  1023. }
  1024. if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
  1025. ntfs_debug("Skipping root directory self reference entry.");
  1026. return 0;
  1027. }
  1028. if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
  1029. !NVolShowSystemFiles(vol)) {
  1030. ntfs_debug("Skipping system file.");
  1031. return 0;
  1032. }
  1033. name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name,
  1034. ie->key.file_name.file_name_length, &name,
  1035. NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
  1036. if (name_len <= 0) {
  1037. ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.",
  1038. (long long)MREF_LE(ie->data.dir.indexed_file));
  1039. return 0;
  1040. }
  1041. if (ie->key.file_name.file_attributes &
  1042. FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
  1043. dt_type = DT_DIR;
  1044. else
  1045. dt_type = DT_REG;
  1046. mref = MREF_LE(ie->data.dir.indexed_file);
  1047. /*
  1048. * Drop the page lock otherwise we deadlock with NFS when it calls
  1049. * ->lookup since ntfs_lookup() will lock the same page.
  1050. */
  1051. if (ia_page)
  1052. unlock_page(ia_page);
  1053. ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
  1054. "0x%lx, DT_%s.", name, name_len, fpos, mref,
  1055. dt_type == DT_DIR ? "DIR" : "REG");
  1056. rc = filldir(dirent, name, name_len, fpos, mref, dt_type);
  1057. /* Relock the page but not if we are aborting ->readdir. */
  1058. if (!rc && ia_page)
  1059. lock_page(ia_page);
  1060. return rc;
  1061. }
  1062. /*
  1063. * We use the same basic approach as the old NTFS driver, i.e. we parse the
  1064. * index root entries and then the index allocation entries that are marked
  1065. * as in use in the index bitmap.
  1066. *
  1067. * While this will return the names in random order this doesn't matter for
  1068. * ->readdir but OTOH results in a faster ->readdir.
  1069. *
  1070. * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS
  1071. * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
  1072. * modifications).
  1073. *
  1074. * Locking: - Caller must hold i_mutex on the directory.
  1075. * - Each page cache page in the index allocation mapping must be
  1076. * locked whilst being accessed otherwise we may find a corrupt
  1077. * page due to it being under ->writepage at the moment which
  1078. * applies the mst protection fixups before writing out and then
  1079. * removes them again after the write is complete after which it
  1080. * unlocks the page.
  1081. */
  1082. static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  1083. {
  1084. s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
  1085. loff_t fpos, i_size;
  1086. struct inode *bmp_vi, *vdir = filp->f_path.dentry->d_inode;
  1087. struct super_block *sb = vdir->i_sb;
  1088. ntfs_inode *ndir = NTFS_I(vdir);
  1089. ntfs_volume *vol = NTFS_SB(sb);
  1090. MFT_RECORD *m;
  1091. INDEX_ROOT *ir = NULL;
  1092. INDEX_ENTRY *ie;
  1093. INDEX_ALLOCATION *ia;
  1094. u8 *name = NULL;
  1095. int rc, err, ir_pos, cur_bmp_pos;
  1096. struct address_space *ia_mapping, *bmp_mapping;
  1097. struct page *bmp_page = NULL, *ia_page = NULL;
  1098. u8 *kaddr, *bmp, *index_end;
  1099. ntfs_attr_search_ctx *ctx;
  1100. fpos = filp->f_pos;
  1101. ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
  1102. vdir->i_ino, fpos);
  1103. rc = err = 0;
  1104. /* Are we at end of dir yet? */
  1105. i_size = i_size_read(vdir);
  1106. if (fpos >= i_size + vol->mft_record_size)
  1107. goto done;
  1108. /* Emulate . and .. for all directories. */
  1109. if (!fpos) {
  1110. ntfs_debug("Calling filldir for . with len 1, fpos 0x0, "
  1111. "inode 0x%lx, DT_DIR.", vdir->i_ino);
  1112. rc = filldir(dirent, ".", 1, fpos, vdir->i_ino, DT_DIR);
  1113. if (rc)
  1114. goto done;
  1115. fpos++;
  1116. }
  1117. if (fpos == 1) {
  1118. ntfs_debug("Calling filldir for .. with len 2, fpos 0x1, "
  1119. "inode 0x%lx, DT_DIR.",
  1120. (unsigned long)parent_ino(filp->f_path.dentry));
  1121. rc = filldir(dirent, "..", 2, fpos,
  1122. parent_ino(filp->f_path.dentry), DT_DIR);
  1123. if (rc)
  1124. goto done;
  1125. fpos++;
  1126. }
  1127. m = NULL;
  1128. ctx = NULL;
  1129. /*
  1130. * Allocate a buffer to store the current name being processed
  1131. * converted to format determined by current NLS.
  1132. */
  1133. name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS);
  1134. if (unlikely(!name)) {
  1135. err = -ENOMEM;
  1136. goto err_out;
  1137. }
  1138. /* Are we jumping straight into the index allocation attribute? */
  1139. if (fpos >= vol->mft_record_size)
  1140. goto skip_index_root;
  1141. /* Get hold of the mft record for the directory. */
  1142. m = map_mft_record(ndir);
  1143. if (IS_ERR(m)) {
  1144. err = PTR_ERR(m);
  1145. m = NULL;
  1146. goto err_out;
  1147. }
  1148. ctx = ntfs_attr_get_search_ctx(ndir, m);
  1149. if (unlikely(!ctx)) {
  1150. err = -ENOMEM;
  1151. goto err_out;
  1152. }
  1153. /* Get the offset into the index root attribute. */
  1154. ir_pos = (s64)fpos;
  1155. /* Find the index root attribute in the mft record. */
  1156. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
  1157. 0, ctx);
  1158. if (unlikely(err)) {
  1159. ntfs_error(sb, "Index root attribute missing in directory "
  1160. "inode 0x%lx.", vdir->i_ino);
  1161. goto err_out;
  1162. }
  1163. /*
  1164. * Copy the index root attribute value to a buffer so that we can put
  1165. * the search context and unmap the mft record before calling the
  1166. * filldir() callback. We need to do this because of NFSd which calls
  1167. * ->lookup() from its filldir callback() and this causes NTFS to
  1168. * deadlock as ntfs_lookup() maps the mft record of the directory and
  1169. * we have got it mapped here already. The only solution is for us to
  1170. * unmap the mft record here so that a call to ntfs_lookup() is able to
  1171. * map the mft record without deadlocking.
  1172. */
  1173. rc = le32_to_cpu(ctx->attr->data.resident.value_length);
  1174. ir = kmalloc(rc, GFP_NOFS);
  1175. if (unlikely(!ir)) {
  1176. err = -ENOMEM;
  1177. goto err_out;
  1178. }
  1179. /* Copy the index root value (it has been verified in read_inode). */
  1180. memcpy(ir, (u8*)ctx->attr +
  1181. le16_to_cpu(ctx->attr->data.resident.value_offset), rc);
  1182. ntfs_attr_put_search_ctx(ctx);
  1183. unmap_mft_record(ndir);
  1184. ctx = NULL;
  1185. m = NULL;
  1186. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  1187. /* The first index entry. */
  1188. ie = (INDEX_ENTRY*)((u8*)&ir->index +
  1189. le32_to_cpu(ir->index.entries_offset));
  1190. /*
  1191. * Loop until we exceed valid memory (corruption case) or until we
  1192. * reach the last entry or until filldir tells us it has had enough
  1193. * or signals an error (both covered by the rc test).
  1194. */
  1195. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  1196. ntfs_debug("In index root, offset 0x%zx.", (u8*)ie - (u8*)ir);
  1197. /* Bounds checks. */
  1198. if (unlikely((u8*)ie < (u8*)ir || (u8*)ie +
  1199. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  1200. (u8*)ie + le16_to_cpu(ie->key_length) >
  1201. index_end))
  1202. goto err_out;
  1203. /* The last entry cannot contain a name. */
  1204. if (ie->flags & INDEX_ENTRY_END)
  1205. break;
  1206. /* Skip index root entry if continuing previous readdir. */
  1207. if (ir_pos > (u8*)ie - (u8*)ir)
  1208. continue;
  1209. /* Advance the position even if going to skip the entry. */
  1210. fpos = (u8*)ie - (u8*)ir;
  1211. /* Submit the name to the filldir callback. */
  1212. rc = ntfs_filldir(vol, fpos, ndir, NULL, ie, name, dirent,
  1213. filldir);
  1214. if (rc) {
  1215. kfree(ir);
  1216. goto abort;
  1217. }
  1218. }
  1219. /* We are done with the index root and can free the buffer. */
  1220. kfree(ir);
  1221. ir = NULL;
  1222. /* If there is no index allocation attribute we are finished. */
  1223. if (!NInoIndexAllocPresent(ndir))
  1224. goto EOD;
  1225. /* Advance fpos to the beginning of the index allocation. */
  1226. fpos = vol->mft_record_size;
  1227. skip_index_root:
  1228. kaddr = NULL;
  1229. prev_ia_pos = -1LL;
  1230. /* Get the offset into the index allocation attribute. */
  1231. ia_pos = (s64)fpos - vol->mft_record_size;
  1232. ia_mapping = vdir->i_mapping;
  1233. ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir->i_ino);
  1234. bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
  1235. if (IS_ERR(bmp_vi)) {
  1236. ntfs_error(sb, "Failed to get bitmap attribute.");
  1237. err = PTR_ERR(bmp_vi);
  1238. goto err_out;
  1239. }
  1240. bmp_mapping = bmp_vi->i_mapping;
  1241. /* Get the starting bitmap bit position and sanity check it. */
  1242. bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
  1243. if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) {
  1244. ntfs_error(sb, "Current index allocation position exceeds "
  1245. "index bitmap size.");
  1246. goto iput_err_out;
  1247. }
  1248. /* Get the starting bit position in the current bitmap page. */
  1249. cur_bmp_pos = bmp_pos & ((PAGE_CACHE_SIZE * 8) - 1);
  1250. bmp_pos &= ~(u64)((PAGE_CACHE_SIZE * 8) - 1);
  1251. get_next_bmp_page:
  1252. ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
  1253. (unsigned long long)bmp_pos >> (3 + PAGE_CACHE_SHIFT),
  1254. (unsigned long long)bmp_pos &
  1255. (unsigned long long)((PAGE_CACHE_SIZE * 8) - 1));
  1256. bmp_page = ntfs_map_page(bmp_mapping,
  1257. bmp_pos >> (3 + PAGE_CACHE_SHIFT));
  1258. if (IS_ERR(bmp_page)) {
  1259. ntfs_error(sb, "Reading index bitmap failed.");
  1260. err = PTR_ERR(bmp_page);
  1261. bmp_page = NULL;
  1262. goto iput_err_out;
  1263. }
  1264. bmp = (u8*)page_address(bmp_page);
  1265. /* Find next index block in use. */
  1266. while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
  1267. find_next_index_buffer:
  1268. cur_bmp_pos++;
  1269. /*
  1270. * If we have reached the end of the bitmap page, get the next
  1271. * page, and put away the old one.
  1272. */
  1273. if (unlikely((cur_bmp_pos >> 3) >= PAGE_CACHE_SIZE)) {
  1274. ntfs_unmap_page(bmp_page);
  1275. bmp_pos += PAGE_CACHE_SIZE * 8;
  1276. cur_bmp_pos = 0;
  1277. goto get_next_bmp_page;
  1278. }
  1279. /* If we have reached the end of the bitmap, we are done. */
  1280. if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
  1281. goto unm_EOD;
  1282. ia_pos = (bmp_pos + cur_bmp_pos) <<
  1283. ndir->itype.index.block_size_bits;
  1284. }
  1285. ntfs_debug("Handling index buffer 0x%llx.",
  1286. (unsigned long long)bmp_pos + cur_bmp_pos);
  1287. /* If the current index buffer is in the same page we reuse the page. */
  1288. if ((prev_ia_pos & (s64)PAGE_CACHE_MASK) !=
  1289. (ia_pos & (s64)PAGE_CACHE_MASK)) {
  1290. prev_ia_pos = ia_pos;
  1291. if (likely(ia_page != NULL)) {
  1292. unlock_page(ia_page);
  1293. ntfs_unmap_page(ia_page);
  1294. }
  1295. /*
  1296. * Map the page cache page containing the current ia_pos,
  1297. * reading it from disk if necessary.
  1298. */
  1299. ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_CACHE_SHIFT);
  1300. if (IS_ERR(ia_page)) {
  1301. ntfs_error(sb, "Reading index allocation data failed.");
  1302. err = PTR_ERR(ia_page);
  1303. ia_page = NULL;
  1304. goto err_out;
  1305. }
  1306. lock_page(ia_page);
  1307. kaddr = (u8*)page_address(ia_page);
  1308. }
  1309. /* Get the current index buffer. */
  1310. ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_CACHE_MASK &
  1311. ~(s64)(ndir->itype.index.block_size - 1)));
  1312. /* Bounds checks. */
  1313. if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE)) {
  1314. ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
  1315. "inode 0x%lx or driver bug.", vdir->i_ino);
  1316. goto err_out;
  1317. }
  1318. /* Catch multi sector transfer fixup errors. */
  1319. if (unlikely(!ntfs_is_indx_record(ia->magic))) {
  1320. ntfs_error(sb, "Directory index record with vcn 0x%llx is "
  1321. "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
  1322. (unsigned long long)ia_pos >>
  1323. ndir->itype.index.vcn_size_bits, vdir->i_ino);
  1324. goto err_out;
  1325. }
  1326. if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
  1327. ~(s64)(ndir->itype.index.block_size - 1)) >>
  1328. ndir->itype.index.vcn_size_bits)) {
  1329. ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
  1330. "different from expected VCN (0x%llx). "
  1331. "Directory inode 0x%lx is corrupt or driver "
  1332. "bug. ", (unsigned long long)
  1333. sle64_to_cpu(ia->index_block_vcn),
  1334. (unsigned long long)ia_pos >>
  1335. ndir->itype.index.vcn_size_bits, vdir->i_ino);
  1336. goto err_out;
  1337. }
  1338. if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
  1339. ndir->itype.index.block_size)) {
  1340. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  1341. "0x%lx has a size (%u) differing from the "
  1342. "directory specified size (%u). Directory "
  1343. "inode is corrupt or driver bug.",
  1344. (unsigned long long)ia_pos >>
  1345. ndir->itype.index.vcn_size_bits, vdir->i_ino,
  1346. le32_to_cpu(ia->index.allocated_size) + 0x18,
  1347. ndir->itype.index.block_size);
  1348. goto err_out;
  1349. }
  1350. index_end = (u8*)ia + ndir->itype.index.block_size;
  1351. if (unlikely(index_end > kaddr + PAGE_CACHE_SIZE)) {
  1352. ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
  1353. "0x%lx crosses page boundary. Impossible! "
  1354. "Cannot access! This is probably a bug in the "
  1355. "driver.", (unsigned long long)ia_pos >>
  1356. ndir->itype.index.vcn_size_bits, vdir->i_ino);
  1357. goto err_out;
  1358. }
  1359. ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1);
  1360. index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
  1361. if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) {
  1362. ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
  1363. "inode 0x%lx exceeds maximum size.",
  1364. (unsigned long long)ia_pos >>
  1365. ndir->itype.index.vcn_size_bits, vdir->i_ino);
  1366. goto err_out;
  1367. }
  1368. /* The first index entry in this index buffer. */
  1369. ie = (INDEX_ENTRY*)((u8*)&ia->index +
  1370. le32_to_cpu(ia->index.entries_offset));
  1371. /*
  1372. * Loop until we exceed valid memory (corruption case) or until we
  1373. * reach the last entry or until filldir tells us it has had enough
  1374. * or signals an error (both covered by the rc test).
  1375. */
  1376. for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
  1377. ntfs_debug("In index allocation, offset 0x%llx.",
  1378. (unsigned long long)ia_start +
  1379. (unsigned long long)((u8*)ie - (u8*)ia));
  1380. /* Bounds checks. */
  1381. if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
  1382. sizeof(INDEX_ENTRY_HEADER) > index_end ||
  1383. (u8*)ie + le16_to_cpu(ie->key_length) >
  1384. index_end))
  1385. goto err_out;
  1386. /* The last entry cannot contain a name. */
  1387. if (ie->flags & INDEX_ENTRY_END)
  1388. break;
  1389. /* Skip index block entry if continuing previous readdir. */
  1390. if (ia_pos - ia_start > (u8*)ie - (u8*)ia)
  1391. continue;
  1392. /* Advance the position even if going to skip the entry. */
  1393. fpos = (u8*)ie - (u8*)ia +
  1394. (sle64_to_cpu(ia->index_block_vcn) <<
  1395. ndir->itype.index.vcn_size_bits) +
  1396. vol->mft_record_size;
  1397. /*
  1398. * Submit the name to the @filldir callback. Note,
  1399. * ntfs_filldir() drops the lock on @ia_page but it retakes it
  1400. * before returning, unless a non-zero value is returned in
  1401. * which case the page is left unlocked.
  1402. */
  1403. rc = ntfs_filldir(vol, fpos, ndir, ia_page, ie, name, dirent,
  1404. filldir);
  1405. if (rc) {
  1406. /* @ia_page is already unlocked in this case. */
  1407. ntfs_unmap_page(ia_page);
  1408. ntfs_unmap_page(bmp_page);
  1409. iput(bmp_vi);
  1410. goto abort;
  1411. }
  1412. }
  1413. goto find_next_index_buffer;
  1414. unm_EOD:
  1415. if (ia_page) {
  1416. unlock_page(ia_page);
  1417. ntfs_unmap_page(ia_page);
  1418. }
  1419. ntfs_unmap_page(bmp_page);
  1420. iput(bmp_vi);
  1421. EOD:
  1422. /* We are finished, set fpos to EOD. */
  1423. fpos = i_size + vol->mft_record_size;
  1424. abort:
  1425. kfree(name);
  1426. done:
  1427. #ifdef DEBUG
  1428. if (!rc)
  1429. ntfs_debug("EOD, fpos 0x%llx, returning 0.", fpos);
  1430. else
  1431. ntfs_debug("filldir returned %i, fpos 0x%llx, returning 0.",
  1432. rc, fpos);
  1433. #endif
  1434. filp->f_pos = fpos;
  1435. return 0;
  1436. err_out:
  1437. if (bmp_page) {
  1438. ntfs_unmap_page(bmp_page);
  1439. iput_err_out:
  1440. iput(bmp_vi);
  1441. }
  1442. if (ia_page) {
  1443. unlock_page(ia_page);
  1444. ntfs_unmap_page(ia_page);
  1445. }
  1446. kfree(ir);
  1447. kfree(name);
  1448. if (ctx)
  1449. ntfs_attr_put_search_ctx(ctx);
  1450. if (m)
  1451. unmap_mft_record(ndir);
  1452. if (!err)
  1453. err = -EIO;
  1454. ntfs_debug("Failed. Returning error code %i.", -err);
  1455. filp->f_pos = fpos;
  1456. return err;
  1457. }
  1458. /**
  1459. * ntfs_dir_open - called when an inode is about to be opened
  1460. * @vi: inode to be opened
  1461. * @filp: file structure describing the inode
  1462. *
  1463. * Limit directory size to the page cache limit on architectures where unsigned
  1464. * long is 32-bits. This is the most we can do for now without overflowing the
  1465. * page cache page index. Doing it this way means we don't run into problems
  1466. * because of existing too large directories. It would be better to allow the
  1467. * user to read the accessible part of the directory but I doubt very much
  1468. * anyone is going to hit this check on a 32-bit architecture, so there is no
  1469. * point in adding the extra complexity required to support this.
  1470. *
  1471. * On 64-bit architectures, the check is hopefully optimized away by the
  1472. * compiler.
  1473. */
  1474. static int ntfs_dir_open(struct inode *vi, struct file *filp)
  1475. {
  1476. if (sizeof(unsigned long) < 8) {
  1477. if (i_size_read(vi) > MAX_LFS_FILESIZE)
  1478. return -EFBIG;
  1479. }
  1480. return 0;
  1481. }
  1482. #ifdef NTFS_RW
  1483. /**
  1484. * ntfs_dir_fsync - sync a directory to disk
  1485. * @filp: directory to be synced
  1486. * @dentry: dentry describing the directory to sync
  1487. * @datasync: if non-zero only flush user data and not metadata
  1488. *
  1489. * Data integrity sync of a directory to disk. Used for fsync, fdatasync, and
  1490. * msync system calls. This function is based on file.c::ntfs_file_fsync().
  1491. *
  1492. * Write the mft record and all associated extent mft records as well as the
  1493. * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device.
  1494. *
  1495. * If @datasync is true, we do not wait on the inode(s) to be written out
  1496. * but we always wait on the page cache pages to be written out.
  1497. *
  1498. * Note: In the past @filp could be NULL so we ignore it as we don't need it
  1499. * anyway.
  1500. *
  1501. * Locking: Caller must hold i_mutex on the inode.
  1502. *
  1503. * TODO: We should probably also write all attribute/index inodes associated
  1504. * with this inode but since we have no simple way of getting to them we ignore
  1505. * this problem for now. We do write the $BITMAP attribute if it is present
  1506. * which is the important one for a directory so things are not too bad.
  1507. */
  1508. static int ntfs_dir_fsync(struct file *filp, struct dentry *dentry,
  1509. int datasync)
  1510. {
  1511. struct inode *bmp_vi, *vi = dentry->d_inode;
  1512. int err, ret;
  1513. ntfs_attr na;
  1514. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  1515. BUG_ON(!S_ISDIR(vi->i_mode));
  1516. /* If the bitmap attribute inode is in memory sync it, too. */
  1517. na.mft_no = vi->i_ino;
  1518. na.type = AT_BITMAP;
  1519. na.name = I30;
  1520. na.name_len = 4;
  1521. bmp_vi = ilookup5(vi->i_sb, vi->i_ino, (test_t)ntfs_test_inode, &na);
  1522. if (bmp_vi) {
  1523. write_inode_now(bmp_vi, !datasync);
  1524. iput(bmp_vi);
  1525. }
  1526. ret = ntfs_write_inode(vi, 1);
  1527. write_inode_now(vi, !datasync);
  1528. err = sync_blockdev(vi->i_sb->s_bdev);
  1529. if (unlikely(err && !ret))
  1530. ret = err;
  1531. if (likely(!ret))
  1532. ntfs_debug("Done.");
  1533. else
  1534. ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error "
  1535. "%u.", datasync ? "data" : "", vi->i_ino, -ret);
  1536. return ret;
  1537. }
  1538. #endif /* NTFS_RW */
  1539. const struct file_operations ntfs_dir_ops = {
  1540. .llseek = generic_file_llseek, /* Seek inside directory. */
  1541. .read = generic_read_dir, /* Return -EISDIR. */
  1542. .readdir = ntfs_readdir, /* Read directory contents. */
  1543. #ifdef NTFS_RW
  1544. .fsync = ntfs_dir_fsync, /* Sync a directory to disk. */
  1545. /*.aio_fsync = ,*/ /* Sync all outstanding async
  1546. i/o operations on a kiocb. */
  1547. #endif /* NTFS_RW */
  1548. /*.ioctl = ,*/ /* Perform function on the
  1549. mounted filesystem. */
  1550. .open = ntfs_dir_open, /* Open directory. */
  1551. };