dir.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. * Copyright (C) 2006, 2007 University of Szeged, Hungary
  6. *
  7. * Authors: Artem Bityutskiy (Битюцкий Артём)
  8. * Adrian Hunter
  9. * Zoltan Sogor
  10. */
  11. /*
  12. * This file implements directory operations.
  13. *
  14. * All FS operations in this file allocate budget before writing anything to the
  15. * media. If they fail to allocate it, the error is returned. The only
  16. * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
  17. * if they unable to allocate the budget, because deletion %-ENOSPC failure is
  18. * not what users are usually ready to get. UBIFS budgeting subsystem has some
  19. * space reserved for these purposes.
  20. *
  21. * All operations in this file write all inodes which they change straight
  22. * away, instead of marking them dirty. For example, 'ubifs_link()' changes
  23. * @i_size of the parent inode and writes the parent inode together with the
  24. * target inode. This was done to simplify file-system recovery which would
  25. * otherwise be very difficult to do. The only exception is rename which marks
  26. * the re-named inode dirty (because its @i_ctime is updated) but does not
  27. * write it, but just marks it as dirty.
  28. */
  29. #include "ubifs.h"
  30. /**
  31. * inherit_flags - inherit flags of the parent inode.
  32. * @dir: parent inode
  33. * @mode: new inode mode flags
  34. *
  35. * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
  36. * parent directory inode @dir. UBIFS inodes inherit the following flags:
  37. * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
  38. * sub-directory basis;
  39. * o %UBIFS_SYNC_FL - useful for the same reasons;
  40. * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
  41. *
  42. * This function returns the inherited flags.
  43. */
  44. static int inherit_flags(const struct inode *dir, umode_t mode)
  45. {
  46. int flags;
  47. const struct ubifs_inode *ui = ubifs_inode(dir);
  48. if (!S_ISDIR(dir->i_mode))
  49. /*
  50. * The parent is not a directory, which means that an extended
  51. * attribute inode is being created. No flags.
  52. */
  53. return 0;
  54. flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
  55. if (!S_ISDIR(mode))
  56. /* The "DIRSYNC" flag only applies to directories */
  57. flags &= ~UBIFS_DIRSYNC_FL;
  58. return flags;
  59. }
  60. /**
  61. * ubifs_new_inode - allocate new UBIFS inode object.
  62. * @c: UBIFS file-system description object
  63. * @dir: parent directory inode
  64. * @mode: inode mode flags
  65. *
  66. * This function finds an unused inode number, allocates new inode and
  67. * initializes it. Returns new inode in case of success and an error code in
  68. * case of failure.
  69. */
  70. struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
  71. umode_t mode)
  72. {
  73. int err;
  74. struct inode *inode;
  75. struct ubifs_inode *ui;
  76. bool encrypted = false;
  77. inode = new_inode(c->vfs_sb);
  78. ui = ubifs_inode(inode);
  79. if (!inode)
  80. return ERR_PTR(-ENOMEM);
  81. /*
  82. * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
  83. * marking them dirty in file write path (see 'file_update_time()').
  84. * UBIFS has to fully control "clean <-> dirty" transitions of inodes
  85. * to make budgeting work.
  86. */
  87. inode->i_flags |= S_NOCMTIME;
  88. inode_init_owner(inode, dir, mode);
  89. inode->i_mtime = inode->i_atime = inode->i_ctime =
  90. current_time(inode);
  91. inode->i_mapping->nrpages = 0;
  92. err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
  93. if (err) {
  94. ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
  95. goto out_iput;
  96. }
  97. switch (mode & S_IFMT) {
  98. case S_IFREG:
  99. inode->i_mapping->a_ops = &ubifs_file_address_operations;
  100. inode->i_op = &ubifs_file_inode_operations;
  101. inode->i_fop = &ubifs_file_operations;
  102. break;
  103. case S_IFDIR:
  104. inode->i_op = &ubifs_dir_inode_operations;
  105. inode->i_fop = &ubifs_dir_operations;
  106. inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
  107. break;
  108. case S_IFLNK:
  109. inode->i_op = &ubifs_symlink_inode_operations;
  110. break;
  111. case S_IFSOCK:
  112. case S_IFIFO:
  113. case S_IFBLK:
  114. case S_IFCHR:
  115. inode->i_op = &ubifs_file_inode_operations;
  116. break;
  117. default:
  118. BUG();
  119. }
  120. ui->flags = inherit_flags(dir, mode);
  121. ubifs_set_inode_flags(inode);
  122. if (S_ISREG(mode))
  123. ui->compr_type = c->default_compr;
  124. else
  125. ui->compr_type = UBIFS_COMPR_NONE;
  126. ui->synced_i_size = 0;
  127. spin_lock(&c->cnt_lock);
  128. /* Inode number overflow is currently not supported */
  129. if (c->highest_inum >= INUM_WARN_WATERMARK) {
  130. if (c->highest_inum >= INUM_WATERMARK) {
  131. spin_unlock(&c->cnt_lock);
  132. ubifs_err(c, "out of inode numbers");
  133. err = -EINVAL;
  134. goto out_iput;
  135. }
  136. ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
  137. (unsigned long)c->highest_inum, INUM_WATERMARK);
  138. }
  139. inode->i_ino = ++c->highest_inum;
  140. /*
  141. * The creation sequence number remains with this inode for its
  142. * lifetime. All nodes for this inode have a greater sequence number,
  143. * and so it is possible to distinguish obsolete nodes belonging to a
  144. * previous incarnation of the same inode number - for example, for the
  145. * purpose of rebuilding the index.
  146. */
  147. ui->creat_sqnum = ++c->max_sqnum;
  148. spin_unlock(&c->cnt_lock);
  149. if (encrypted) {
  150. err = fscrypt_set_context(inode, NULL);
  151. if (err) {
  152. ubifs_err(c, "fscrypt_set_context failed: %i", err);
  153. goto out_iput;
  154. }
  155. }
  156. return inode;
  157. out_iput:
  158. make_bad_inode(inode);
  159. iput(inode);
  160. return ERR_PTR(err);
  161. }
  162. static int dbg_check_name(const struct ubifs_info *c,
  163. const struct ubifs_dent_node *dent,
  164. const struct fscrypt_name *nm)
  165. {
  166. if (!dbg_is_chk_gen(c))
  167. return 0;
  168. if (le16_to_cpu(dent->nlen) != fname_len(nm))
  169. return -EINVAL;
  170. if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
  171. return -EINVAL;
  172. return 0;
  173. }
  174. static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
  175. unsigned int flags)
  176. {
  177. int err;
  178. union ubifs_key key;
  179. struct inode *inode = NULL;
  180. struct ubifs_dent_node *dent = NULL;
  181. struct ubifs_info *c = dir->i_sb->s_fs_info;
  182. struct fscrypt_name nm;
  183. dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
  184. err = fscrypt_prepare_lookup(dir, dentry, &nm);
  185. generic_set_encrypted_ci_d_ops(dentry);
  186. if (err == -ENOENT)
  187. return d_splice_alias(NULL, dentry);
  188. if (err)
  189. return ERR_PTR(err);
  190. if (fname_len(&nm) > UBIFS_MAX_NLEN) {
  191. inode = ERR_PTR(-ENAMETOOLONG);
  192. goto done;
  193. }
  194. dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  195. if (!dent) {
  196. inode = ERR_PTR(-ENOMEM);
  197. goto done;
  198. }
  199. if (fname_name(&nm) == NULL) {
  200. if (nm.hash & ~UBIFS_S_KEY_HASH_MASK)
  201. goto done; /* ENOENT */
  202. dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
  203. err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
  204. } else {
  205. dent_key_init(c, &key, dir->i_ino, &nm);
  206. err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
  207. }
  208. if (err) {
  209. if (err == -ENOENT)
  210. dbg_gen("not found");
  211. else
  212. inode = ERR_PTR(err);
  213. goto done;
  214. }
  215. if (dbg_check_name(c, dent, &nm)) {
  216. inode = ERR_PTR(-EINVAL);
  217. goto done;
  218. }
  219. inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
  220. if (IS_ERR(inode)) {
  221. /*
  222. * This should not happen. Probably the file-system needs
  223. * checking.
  224. */
  225. err = PTR_ERR(inode);
  226. ubifs_err(c, "dead directory entry '%pd', error %d",
  227. dentry, err);
  228. ubifs_ro_mode(c, err);
  229. goto done;
  230. }
  231. if (IS_ENCRYPTED(dir) &&
  232. (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
  233. !fscrypt_has_permitted_context(dir, inode)) {
  234. ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
  235. dir->i_ino, inode->i_ino);
  236. iput(inode);
  237. inode = ERR_PTR(-EPERM);
  238. }
  239. done:
  240. kfree(dent);
  241. fscrypt_free_filename(&nm);
  242. return d_splice_alias(inode, dentry);
  243. }
  244. static int ubifs_prepare_create(struct inode *dir, struct dentry *dentry,
  245. struct fscrypt_name *nm)
  246. {
  247. if (fscrypt_is_nokey_name(dentry))
  248. return -ENOKEY;
  249. return fscrypt_setup_filename(dir, &dentry->d_name, 0, nm);
  250. }
  251. static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  252. bool excl)
  253. {
  254. struct inode *inode;
  255. struct ubifs_info *c = dir->i_sb->s_fs_info;
  256. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  257. .dirtied_ino = 1 };
  258. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  259. struct fscrypt_name nm;
  260. int err, sz_change;
  261. /*
  262. * Budget request settings: new inode, new direntry, changing the
  263. * parent directory inode.
  264. */
  265. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  266. dentry, mode, dir->i_ino);
  267. err = ubifs_budget_space(c, &req);
  268. if (err)
  269. return err;
  270. err = ubifs_prepare_create(dir, dentry, &nm);
  271. if (err)
  272. goto out_budg;
  273. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  274. inode = ubifs_new_inode(c, dir, mode);
  275. if (IS_ERR(inode)) {
  276. err = PTR_ERR(inode);
  277. goto out_fname;
  278. }
  279. err = ubifs_init_security(dir, inode, &dentry->d_name);
  280. if (err)
  281. goto out_inode;
  282. mutex_lock(&dir_ui->ui_mutex);
  283. dir->i_size += sz_change;
  284. dir_ui->ui_size = dir->i_size;
  285. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  286. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  287. if (err)
  288. goto out_cancel;
  289. mutex_unlock(&dir_ui->ui_mutex);
  290. ubifs_release_budget(c, &req);
  291. fscrypt_free_filename(&nm);
  292. insert_inode_hash(inode);
  293. d_instantiate(dentry, inode);
  294. return 0;
  295. out_cancel:
  296. dir->i_size -= sz_change;
  297. dir_ui->ui_size = dir->i_size;
  298. mutex_unlock(&dir_ui->ui_mutex);
  299. out_inode:
  300. make_bad_inode(inode);
  301. iput(inode);
  302. out_fname:
  303. fscrypt_free_filename(&nm);
  304. out_budg:
  305. ubifs_release_budget(c, &req);
  306. ubifs_err(c, "cannot create regular file, error %d", err);
  307. return err;
  308. }
  309. static int do_tmpfile(struct inode *dir, struct dentry *dentry,
  310. umode_t mode, struct inode **whiteout)
  311. {
  312. struct inode *inode;
  313. struct ubifs_info *c = dir->i_sb->s_fs_info;
  314. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  315. .dirtied_ino = 1};
  316. struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
  317. struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
  318. int err, instantiated = 0;
  319. struct fscrypt_name nm;
  320. /*
  321. * Budget request settings: new inode, new direntry, changing the
  322. * parent directory inode.
  323. * Allocate budget separately for new dirtied inode, the budget will
  324. * be released via writeback.
  325. */
  326. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  327. dentry, mode, dir->i_ino);
  328. err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
  329. if (err)
  330. return err;
  331. err = ubifs_budget_space(c, &req);
  332. if (err) {
  333. fscrypt_free_filename(&nm);
  334. return err;
  335. }
  336. err = ubifs_budget_space(c, &ino_req);
  337. if (err) {
  338. ubifs_release_budget(c, &req);
  339. fscrypt_free_filename(&nm);
  340. return err;
  341. }
  342. inode = ubifs_new_inode(c, dir, mode);
  343. if (IS_ERR(inode)) {
  344. err = PTR_ERR(inode);
  345. goto out_budg;
  346. }
  347. ui = ubifs_inode(inode);
  348. if (whiteout) {
  349. init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
  350. ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations);
  351. }
  352. err = ubifs_init_security(dir, inode, &dentry->d_name);
  353. if (err)
  354. goto out_inode;
  355. mutex_lock(&ui->ui_mutex);
  356. insert_inode_hash(inode);
  357. if (whiteout) {
  358. mark_inode_dirty(inode);
  359. drop_nlink(inode);
  360. *whiteout = inode;
  361. } else {
  362. d_tmpfile(dentry, inode);
  363. }
  364. ubifs_assert(c, ui->dirty);
  365. instantiated = 1;
  366. mutex_unlock(&ui->ui_mutex);
  367. mutex_lock(&dir_ui->ui_mutex);
  368. err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
  369. if (err)
  370. goto out_cancel;
  371. mutex_unlock(&dir_ui->ui_mutex);
  372. ubifs_release_budget(c, &req);
  373. return 0;
  374. out_cancel:
  375. mutex_unlock(&dir_ui->ui_mutex);
  376. out_inode:
  377. make_bad_inode(inode);
  378. if (!instantiated)
  379. iput(inode);
  380. else if (whiteout)
  381. iput(*whiteout);
  382. out_budg:
  383. ubifs_release_budget(c, &req);
  384. if (!instantiated)
  385. ubifs_release_budget(c, &ino_req);
  386. fscrypt_free_filename(&nm);
  387. ubifs_err(c, "cannot create temporary file, error %d", err);
  388. return err;
  389. }
  390. static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
  391. umode_t mode)
  392. {
  393. return do_tmpfile(dir, dentry, mode, NULL);
  394. }
  395. /**
  396. * vfs_dent_type - get VFS directory entry type.
  397. * @type: UBIFS directory entry type
  398. *
  399. * This function converts UBIFS directory entry type into VFS directory entry
  400. * type.
  401. */
  402. static unsigned int vfs_dent_type(uint8_t type)
  403. {
  404. switch (type) {
  405. case UBIFS_ITYPE_REG:
  406. return DT_REG;
  407. case UBIFS_ITYPE_DIR:
  408. return DT_DIR;
  409. case UBIFS_ITYPE_LNK:
  410. return DT_LNK;
  411. case UBIFS_ITYPE_BLK:
  412. return DT_BLK;
  413. case UBIFS_ITYPE_CHR:
  414. return DT_CHR;
  415. case UBIFS_ITYPE_FIFO:
  416. return DT_FIFO;
  417. case UBIFS_ITYPE_SOCK:
  418. return DT_SOCK;
  419. default:
  420. BUG();
  421. }
  422. return 0;
  423. }
  424. /*
  425. * The classical Unix view for directory is that it is a linear array of
  426. * (name, inode number) entries. Linux/VFS assumes this model as well.
  427. * Particularly, 'readdir()' call wants us to return a directory entry offset
  428. * which later may be used to continue 'readdir()'ing the directory or to
  429. * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
  430. * model because directory entries are identified by keys, which may collide.
  431. *
  432. * UBIFS uses directory entry hash value for directory offsets, so
  433. * 'seekdir()'/'telldir()' may not always work because of possible key
  434. * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
  435. * properly by means of saving full directory entry name in the private field
  436. * of the file description object.
  437. *
  438. * This means that UBIFS cannot support NFS which requires full
  439. * 'seekdir()'/'telldir()' support.
  440. */
  441. static int ubifs_readdir(struct file *file, struct dir_context *ctx)
  442. {
  443. int fstr_real_len = 0, err = 0;
  444. struct fscrypt_name nm;
  445. struct fscrypt_str fstr = {0};
  446. union ubifs_key key;
  447. struct ubifs_dent_node *dent;
  448. struct inode *dir = file_inode(file);
  449. struct ubifs_info *c = dir->i_sb->s_fs_info;
  450. bool encrypted = IS_ENCRYPTED(dir);
  451. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
  452. if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
  453. /*
  454. * The directory was seek'ed to a senseless position or there
  455. * are no more entries.
  456. */
  457. return 0;
  458. if (encrypted) {
  459. err = fscrypt_prepare_readdir(dir);
  460. if (err)
  461. return err;
  462. err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr);
  463. if (err)
  464. return err;
  465. fstr_real_len = fstr.len;
  466. }
  467. if (file->f_version == 0) {
  468. /*
  469. * The file was seek'ed, which means that @file->private_data
  470. * is now invalid. This may also be just the first
  471. * 'ubifs_readdir()' invocation, in which case
  472. * @file->private_data is NULL, and the below code is
  473. * basically a no-op.
  474. */
  475. kfree(file->private_data);
  476. file->private_data = NULL;
  477. }
  478. /*
  479. * 'generic_file_llseek()' unconditionally sets @file->f_version to
  480. * zero, and we use this for detecting whether the file was seek'ed.
  481. */
  482. file->f_version = 1;
  483. /* File positions 0 and 1 correspond to "." and ".." */
  484. if (ctx->pos < 2) {
  485. ubifs_assert(c, !file->private_data);
  486. if (!dir_emit_dots(file, ctx)) {
  487. if (encrypted)
  488. fscrypt_fname_free_buffer(&fstr);
  489. return 0;
  490. }
  491. /* Find the first entry in TNC and save it */
  492. lowest_dent_key(c, &key, dir->i_ino);
  493. fname_len(&nm) = 0;
  494. dent = ubifs_tnc_next_ent(c, &key, &nm);
  495. if (IS_ERR(dent)) {
  496. err = PTR_ERR(dent);
  497. goto out;
  498. }
  499. ctx->pos = key_hash_flash(c, &dent->key);
  500. file->private_data = dent;
  501. }
  502. dent = file->private_data;
  503. if (!dent) {
  504. /*
  505. * The directory was seek'ed to and is now readdir'ed.
  506. * Find the entry corresponding to @ctx->pos or the closest one.
  507. */
  508. dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
  509. fname_len(&nm) = 0;
  510. dent = ubifs_tnc_next_ent(c, &key, &nm);
  511. if (IS_ERR(dent)) {
  512. err = PTR_ERR(dent);
  513. goto out;
  514. }
  515. ctx->pos = key_hash_flash(c, &dent->key);
  516. file->private_data = dent;
  517. }
  518. while (1) {
  519. dbg_gen("ino %llu, new f_pos %#x",
  520. (unsigned long long)le64_to_cpu(dent->inum),
  521. key_hash_flash(c, &dent->key));
  522. ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) >
  523. ubifs_inode(dir)->creat_sqnum);
  524. fname_len(&nm) = le16_to_cpu(dent->nlen);
  525. fname_name(&nm) = dent->name;
  526. if (encrypted) {
  527. fstr.len = fstr_real_len;
  528. err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
  529. &dent->key),
  530. le32_to_cpu(dent->cookie),
  531. &nm.disk_name, &fstr);
  532. if (err)
  533. goto out;
  534. } else {
  535. fstr.len = fname_len(&nm);
  536. fstr.name = fname_name(&nm);
  537. }
  538. if (!dir_emit(ctx, fstr.name, fstr.len,
  539. le64_to_cpu(dent->inum),
  540. vfs_dent_type(dent->type))) {
  541. if (encrypted)
  542. fscrypt_fname_free_buffer(&fstr);
  543. return 0;
  544. }
  545. /* Switch to the next entry */
  546. key_read(c, &dent->key, &key);
  547. dent = ubifs_tnc_next_ent(c, &key, &nm);
  548. if (IS_ERR(dent)) {
  549. err = PTR_ERR(dent);
  550. goto out;
  551. }
  552. kfree(file->private_data);
  553. ctx->pos = key_hash_flash(c, &dent->key);
  554. file->private_data = dent;
  555. cond_resched();
  556. }
  557. out:
  558. kfree(file->private_data);
  559. file->private_data = NULL;
  560. if (encrypted)
  561. fscrypt_fname_free_buffer(&fstr);
  562. if (err != -ENOENT)
  563. ubifs_err(c, "cannot find next direntry, error %d", err);
  564. else
  565. /*
  566. * -ENOENT is a non-fatal error in this context, the TNC uses
  567. * it to indicate that the cursor moved past the current directory
  568. * and readdir() has to stop.
  569. */
  570. err = 0;
  571. /* 2 is a special value indicating that there are no more direntries */
  572. ctx->pos = 2;
  573. return err;
  574. }
  575. /* Free saved readdir() state when the directory is closed */
  576. static int ubifs_dir_release(struct inode *dir, struct file *file)
  577. {
  578. kfree(file->private_data);
  579. file->private_data = NULL;
  580. return 0;
  581. }
  582. /**
  583. * lock_2_inodes - a wrapper for locking two UBIFS inodes.
  584. * @inode1: first inode
  585. * @inode2: second inode
  586. *
  587. * We do not implement any tricks to guarantee strict lock ordering, because
  588. * VFS has already done it for us on the @i_mutex. So this is just a simple
  589. * wrapper function.
  590. */
  591. static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
  592. {
  593. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  594. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  595. }
  596. /**
  597. * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
  598. * @inode1: first inode
  599. * @inode2: second inode
  600. */
  601. static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
  602. {
  603. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  604. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  605. }
  606. static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
  607. struct dentry *dentry)
  608. {
  609. struct ubifs_info *c = dir->i_sb->s_fs_info;
  610. struct inode *inode = d_inode(old_dentry);
  611. struct ubifs_inode *ui = ubifs_inode(inode);
  612. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  613. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  614. struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
  615. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  616. struct fscrypt_name nm;
  617. /*
  618. * Budget request settings: new direntry, changing the target inode,
  619. * changing the parent inode.
  620. */
  621. dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
  622. dentry, inode->i_ino,
  623. inode->i_nlink, dir->i_ino);
  624. ubifs_assert(c, inode_is_locked(dir));
  625. ubifs_assert(c, inode_is_locked(inode));
  626. err = fscrypt_prepare_link(old_dentry, dir, dentry);
  627. if (err)
  628. return err;
  629. err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
  630. if (err)
  631. return err;
  632. err = dbg_check_synced_i_size(c, inode);
  633. if (err)
  634. goto out_fname;
  635. err = ubifs_budget_space(c, &req);
  636. if (err)
  637. goto out_fname;
  638. lock_2_inodes(dir, inode);
  639. /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
  640. if (inode->i_nlink == 0)
  641. ubifs_delete_orphan(c, inode->i_ino);
  642. inc_nlink(inode);
  643. ihold(inode);
  644. inode->i_ctime = current_time(inode);
  645. dir->i_size += sz_change;
  646. dir_ui->ui_size = dir->i_size;
  647. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  648. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  649. if (err)
  650. goto out_cancel;
  651. unlock_2_inodes(dir, inode);
  652. ubifs_release_budget(c, &req);
  653. d_instantiate(dentry, inode);
  654. fscrypt_free_filename(&nm);
  655. return 0;
  656. out_cancel:
  657. dir->i_size -= sz_change;
  658. dir_ui->ui_size = dir->i_size;
  659. drop_nlink(inode);
  660. if (inode->i_nlink == 0)
  661. ubifs_add_orphan(c, inode->i_ino);
  662. unlock_2_inodes(dir, inode);
  663. ubifs_release_budget(c, &req);
  664. iput(inode);
  665. out_fname:
  666. fscrypt_free_filename(&nm);
  667. return err;
  668. }
  669. static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
  670. {
  671. struct ubifs_info *c = dir->i_sb->s_fs_info;
  672. struct inode *inode = d_inode(dentry);
  673. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  674. int err, sz_change, budgeted = 1;
  675. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  676. unsigned int saved_nlink = inode->i_nlink;
  677. struct fscrypt_name nm;
  678. /*
  679. * Budget request settings: deletion direntry, deletion inode (+1 for
  680. * @dirtied_ino), changing the parent directory inode. If budgeting
  681. * fails, go ahead anyway because we have extra space reserved for
  682. * deletions.
  683. */
  684. dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
  685. dentry, inode->i_ino,
  686. inode->i_nlink, dir->i_ino);
  687. err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
  688. if (err)
  689. return err;
  690. err = ubifs_purge_xattrs(inode);
  691. if (err)
  692. return err;
  693. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  694. ubifs_assert(c, inode_is_locked(dir));
  695. ubifs_assert(c, inode_is_locked(inode));
  696. err = dbg_check_synced_i_size(c, inode);
  697. if (err)
  698. goto out_fname;
  699. err = ubifs_budget_space(c, &req);
  700. if (err) {
  701. if (err != -ENOSPC)
  702. goto out_fname;
  703. budgeted = 0;
  704. }
  705. lock_2_inodes(dir, inode);
  706. inode->i_ctime = current_time(dir);
  707. drop_nlink(inode);
  708. dir->i_size -= sz_change;
  709. dir_ui->ui_size = dir->i_size;
  710. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  711. err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
  712. if (err)
  713. goto out_cancel;
  714. unlock_2_inodes(dir, inode);
  715. if (budgeted)
  716. ubifs_release_budget(c, &req);
  717. else {
  718. /* We've deleted something - clean the "no space" flags */
  719. c->bi.nospace = c->bi.nospace_rp = 0;
  720. smp_wmb();
  721. }
  722. fscrypt_free_filename(&nm);
  723. return 0;
  724. out_cancel:
  725. dir->i_size += sz_change;
  726. dir_ui->ui_size = dir->i_size;
  727. set_nlink(inode, saved_nlink);
  728. unlock_2_inodes(dir, inode);
  729. if (budgeted)
  730. ubifs_release_budget(c, &req);
  731. out_fname:
  732. fscrypt_free_filename(&nm);
  733. return err;
  734. }
  735. /**
  736. * check_dir_empty - check if a directory is empty or not.
  737. * @dir: VFS inode object of the directory to check
  738. *
  739. * This function checks if directory @dir is empty. Returns zero if the
  740. * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
  741. * in case of of errors.
  742. */
  743. int ubifs_check_dir_empty(struct inode *dir)
  744. {
  745. struct ubifs_info *c = dir->i_sb->s_fs_info;
  746. struct fscrypt_name nm = { 0 };
  747. struct ubifs_dent_node *dent;
  748. union ubifs_key key;
  749. int err;
  750. lowest_dent_key(c, &key, dir->i_ino);
  751. dent = ubifs_tnc_next_ent(c, &key, &nm);
  752. if (IS_ERR(dent)) {
  753. err = PTR_ERR(dent);
  754. if (err == -ENOENT)
  755. err = 0;
  756. } else {
  757. kfree(dent);
  758. err = -ENOTEMPTY;
  759. }
  760. return err;
  761. }
  762. static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
  763. {
  764. struct ubifs_info *c = dir->i_sb->s_fs_info;
  765. struct inode *inode = d_inode(dentry);
  766. int err, sz_change, budgeted = 1;
  767. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  768. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  769. struct fscrypt_name nm;
  770. /*
  771. * Budget request settings: deletion direntry, deletion inode and
  772. * changing the parent inode. If budgeting fails, go ahead anyway
  773. * because we have extra space reserved for deletions.
  774. */
  775. dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
  776. inode->i_ino, dir->i_ino);
  777. ubifs_assert(c, inode_is_locked(dir));
  778. ubifs_assert(c, inode_is_locked(inode));
  779. err = ubifs_check_dir_empty(d_inode(dentry));
  780. if (err)
  781. return err;
  782. err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
  783. if (err)
  784. return err;
  785. err = ubifs_purge_xattrs(inode);
  786. if (err)
  787. return err;
  788. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  789. err = ubifs_budget_space(c, &req);
  790. if (err) {
  791. if (err != -ENOSPC)
  792. goto out_fname;
  793. budgeted = 0;
  794. }
  795. lock_2_inodes(dir, inode);
  796. inode->i_ctime = current_time(dir);
  797. clear_nlink(inode);
  798. drop_nlink(dir);
  799. dir->i_size -= sz_change;
  800. dir_ui->ui_size = dir->i_size;
  801. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  802. err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
  803. if (err)
  804. goto out_cancel;
  805. unlock_2_inodes(dir, inode);
  806. if (budgeted)
  807. ubifs_release_budget(c, &req);
  808. else {
  809. /* We've deleted something - clean the "no space" flags */
  810. c->bi.nospace = c->bi.nospace_rp = 0;
  811. smp_wmb();
  812. }
  813. fscrypt_free_filename(&nm);
  814. return 0;
  815. out_cancel:
  816. dir->i_size += sz_change;
  817. dir_ui->ui_size = dir->i_size;
  818. inc_nlink(dir);
  819. set_nlink(inode, 2);
  820. unlock_2_inodes(dir, inode);
  821. if (budgeted)
  822. ubifs_release_budget(c, &req);
  823. out_fname:
  824. fscrypt_free_filename(&nm);
  825. return err;
  826. }
  827. static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  828. {
  829. struct inode *inode;
  830. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  831. struct ubifs_info *c = dir->i_sb->s_fs_info;
  832. int err, sz_change;
  833. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  834. .dirtied_ino = 1};
  835. struct fscrypt_name nm;
  836. /*
  837. * Budget request settings: new inode, new direntry and changing parent
  838. * directory inode.
  839. */
  840. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  841. dentry, mode, dir->i_ino);
  842. err = ubifs_budget_space(c, &req);
  843. if (err)
  844. return err;
  845. err = ubifs_prepare_create(dir, dentry, &nm);
  846. if (err)
  847. goto out_budg;
  848. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  849. inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
  850. if (IS_ERR(inode)) {
  851. err = PTR_ERR(inode);
  852. goto out_fname;
  853. }
  854. err = ubifs_init_security(dir, inode, &dentry->d_name);
  855. if (err)
  856. goto out_inode;
  857. mutex_lock(&dir_ui->ui_mutex);
  858. insert_inode_hash(inode);
  859. inc_nlink(inode);
  860. inc_nlink(dir);
  861. dir->i_size += sz_change;
  862. dir_ui->ui_size = dir->i_size;
  863. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  864. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  865. if (err) {
  866. ubifs_err(c, "cannot create directory, error %d", err);
  867. goto out_cancel;
  868. }
  869. mutex_unlock(&dir_ui->ui_mutex);
  870. ubifs_release_budget(c, &req);
  871. d_instantiate(dentry, inode);
  872. fscrypt_free_filename(&nm);
  873. return 0;
  874. out_cancel:
  875. dir->i_size -= sz_change;
  876. dir_ui->ui_size = dir->i_size;
  877. drop_nlink(dir);
  878. mutex_unlock(&dir_ui->ui_mutex);
  879. out_inode:
  880. make_bad_inode(inode);
  881. iput(inode);
  882. out_fname:
  883. fscrypt_free_filename(&nm);
  884. out_budg:
  885. ubifs_release_budget(c, &req);
  886. return err;
  887. }
  888. static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
  889. umode_t mode, dev_t rdev)
  890. {
  891. struct inode *inode;
  892. struct ubifs_inode *ui;
  893. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  894. struct ubifs_info *c = dir->i_sb->s_fs_info;
  895. union ubifs_dev_desc *dev = NULL;
  896. int sz_change;
  897. int err, devlen = 0;
  898. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  899. .dirtied_ino = 1 };
  900. struct fscrypt_name nm;
  901. /*
  902. * Budget request settings: new inode, new direntry and changing parent
  903. * directory inode.
  904. */
  905. dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
  906. if (S_ISBLK(mode) || S_ISCHR(mode)) {
  907. dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  908. if (!dev)
  909. return -ENOMEM;
  910. devlen = ubifs_encode_dev(dev, rdev);
  911. }
  912. req.new_ino_d = ALIGN(devlen, 8);
  913. err = ubifs_budget_space(c, &req);
  914. if (err) {
  915. kfree(dev);
  916. return err;
  917. }
  918. err = ubifs_prepare_create(dir, dentry, &nm);
  919. if (err) {
  920. kfree(dev);
  921. goto out_budg;
  922. }
  923. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  924. inode = ubifs_new_inode(c, dir, mode);
  925. if (IS_ERR(inode)) {
  926. kfree(dev);
  927. err = PTR_ERR(inode);
  928. goto out_fname;
  929. }
  930. init_special_inode(inode, inode->i_mode, rdev);
  931. inode->i_size = ubifs_inode(inode)->ui_size = devlen;
  932. ui = ubifs_inode(inode);
  933. ui->data = dev;
  934. ui->data_len = devlen;
  935. err = ubifs_init_security(dir, inode, &dentry->d_name);
  936. if (err)
  937. goto out_inode;
  938. mutex_lock(&dir_ui->ui_mutex);
  939. dir->i_size += sz_change;
  940. dir_ui->ui_size = dir->i_size;
  941. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  942. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  943. if (err)
  944. goto out_cancel;
  945. mutex_unlock(&dir_ui->ui_mutex);
  946. ubifs_release_budget(c, &req);
  947. insert_inode_hash(inode);
  948. d_instantiate(dentry, inode);
  949. fscrypt_free_filename(&nm);
  950. return 0;
  951. out_cancel:
  952. dir->i_size -= sz_change;
  953. dir_ui->ui_size = dir->i_size;
  954. mutex_unlock(&dir_ui->ui_mutex);
  955. out_inode:
  956. make_bad_inode(inode);
  957. iput(inode);
  958. out_fname:
  959. fscrypt_free_filename(&nm);
  960. out_budg:
  961. ubifs_release_budget(c, &req);
  962. return err;
  963. }
  964. static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
  965. const char *symname)
  966. {
  967. struct inode *inode;
  968. struct ubifs_inode *ui;
  969. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  970. struct ubifs_info *c = dir->i_sb->s_fs_info;
  971. int err, sz_change, len = strlen(symname);
  972. struct fscrypt_str disk_link;
  973. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  974. .new_ino_d = ALIGN(len, 8),
  975. .dirtied_ino = 1 };
  976. struct fscrypt_name nm;
  977. dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
  978. symname, dir->i_ino);
  979. err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA,
  980. &disk_link);
  981. if (err)
  982. return err;
  983. /*
  984. * Budget request settings: new inode, new direntry and changing parent
  985. * directory inode.
  986. */
  987. err = ubifs_budget_space(c, &req);
  988. if (err)
  989. return err;
  990. err = ubifs_prepare_create(dir, dentry, &nm);
  991. if (err)
  992. goto out_budg;
  993. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  994. inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
  995. if (IS_ERR(inode)) {
  996. err = PTR_ERR(inode);
  997. goto out_fname;
  998. }
  999. ui = ubifs_inode(inode);
  1000. ui->data = kmalloc(disk_link.len, GFP_NOFS);
  1001. if (!ui->data) {
  1002. err = -ENOMEM;
  1003. goto out_inode;
  1004. }
  1005. if (IS_ENCRYPTED(inode)) {
  1006. disk_link.name = ui->data; /* encrypt directly into ui->data */
  1007. err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
  1008. if (err)
  1009. goto out_inode;
  1010. } else {
  1011. memcpy(ui->data, disk_link.name, disk_link.len);
  1012. inode->i_link = ui->data;
  1013. }
  1014. /*
  1015. * The terminating zero byte is not written to the flash media and it
  1016. * is put just to make later in-memory string processing simpler. Thus,
  1017. * data length is @disk_link.len - 1, not @disk_link.len.
  1018. */
  1019. ui->data_len = disk_link.len - 1;
  1020. inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
  1021. err = ubifs_init_security(dir, inode, &dentry->d_name);
  1022. if (err)
  1023. goto out_inode;
  1024. mutex_lock(&dir_ui->ui_mutex);
  1025. dir->i_size += sz_change;
  1026. dir_ui->ui_size = dir->i_size;
  1027. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  1028. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  1029. if (err)
  1030. goto out_cancel;
  1031. mutex_unlock(&dir_ui->ui_mutex);
  1032. insert_inode_hash(inode);
  1033. d_instantiate(dentry, inode);
  1034. err = 0;
  1035. goto out_fname;
  1036. out_cancel:
  1037. dir->i_size -= sz_change;
  1038. dir_ui->ui_size = dir->i_size;
  1039. mutex_unlock(&dir_ui->ui_mutex);
  1040. out_inode:
  1041. make_bad_inode(inode);
  1042. iput(inode);
  1043. out_fname:
  1044. fscrypt_free_filename(&nm);
  1045. out_budg:
  1046. ubifs_release_budget(c, &req);
  1047. return err;
  1048. }
  1049. /**
  1050. * lock_4_inodes - a wrapper for locking three UBIFS inodes.
  1051. * @inode1: first inode
  1052. * @inode2: second inode
  1053. * @inode3: third inode
  1054. * @inode4: fouth inode
  1055. *
  1056. * This function is used for 'ubifs_rename()' and @inode1 may be the same as
  1057. * @inode2 whereas @inode3 and @inode4 may be %NULL.
  1058. *
  1059. * We do not implement any tricks to guarantee strict lock ordering, because
  1060. * VFS has already done it for us on the @i_mutex. So this is just a simple
  1061. * wrapper function.
  1062. */
  1063. static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
  1064. struct inode *inode3, struct inode *inode4)
  1065. {
  1066. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  1067. if (inode2 != inode1)
  1068. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  1069. if (inode3)
  1070. mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
  1071. if (inode4)
  1072. mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
  1073. }
  1074. /**
  1075. * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
  1076. * @inode1: first inode
  1077. * @inode2: second inode
  1078. * @inode3: third inode
  1079. * @inode4: fouth inode
  1080. */
  1081. static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
  1082. struct inode *inode3, struct inode *inode4)
  1083. {
  1084. if (inode4)
  1085. mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
  1086. if (inode3)
  1087. mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
  1088. if (inode1 != inode2)
  1089. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  1090. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  1091. }
  1092. static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
  1093. struct inode *new_dir, struct dentry *new_dentry,
  1094. unsigned int flags)
  1095. {
  1096. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  1097. struct inode *old_inode = d_inode(old_dentry);
  1098. struct inode *new_inode = d_inode(new_dentry);
  1099. struct inode *whiteout = NULL;
  1100. struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
  1101. struct ubifs_inode *whiteout_ui = NULL;
  1102. int err, release, sync = 0, move = (new_dir != old_dir);
  1103. int is_dir = S_ISDIR(old_inode->i_mode);
  1104. int unlink = !!new_inode, new_sz, old_sz;
  1105. struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  1106. .dirtied_ino = 3 };
  1107. struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
  1108. .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
  1109. struct timespec64 time;
  1110. unsigned int saved_nlink;
  1111. struct fscrypt_name old_nm, new_nm;
  1112. /*
  1113. * Budget request settings: deletion direntry, new direntry, removing
  1114. * the old inode, and changing old and new parent directory inodes.
  1115. *
  1116. * However, this operation also marks the target inode as dirty and
  1117. * does not write it, so we allocate budget for the target inode
  1118. * separately.
  1119. */
  1120. dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
  1121. old_dentry, old_inode->i_ino, old_dir->i_ino,
  1122. new_dentry, new_dir->i_ino, flags);
  1123. if (unlink) {
  1124. ubifs_assert(c, inode_is_locked(new_inode));
  1125. err = ubifs_purge_xattrs(new_inode);
  1126. if (err)
  1127. return err;
  1128. }
  1129. if (unlink && is_dir) {
  1130. err = ubifs_check_dir_empty(new_inode);
  1131. if (err)
  1132. return err;
  1133. }
  1134. err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
  1135. if (err)
  1136. return err;
  1137. err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
  1138. if (err) {
  1139. fscrypt_free_filename(&old_nm);
  1140. return err;
  1141. }
  1142. new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
  1143. old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
  1144. err = ubifs_budget_space(c, &req);
  1145. if (err) {
  1146. fscrypt_free_filename(&old_nm);
  1147. fscrypt_free_filename(&new_nm);
  1148. return err;
  1149. }
  1150. err = ubifs_budget_space(c, &ino_req);
  1151. if (err) {
  1152. fscrypt_free_filename(&old_nm);
  1153. fscrypt_free_filename(&new_nm);
  1154. ubifs_release_budget(c, &req);
  1155. return err;
  1156. }
  1157. if (flags & RENAME_WHITEOUT) {
  1158. union ubifs_dev_desc *dev = NULL;
  1159. struct ubifs_budget_req wht_req;
  1160. dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  1161. if (!dev) {
  1162. err = -ENOMEM;
  1163. goto out_release;
  1164. }
  1165. err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
  1166. if (err) {
  1167. kfree(dev);
  1168. goto out_release;
  1169. }
  1170. spin_lock(&whiteout->i_lock);
  1171. whiteout->i_state |= I_LINKABLE;
  1172. spin_unlock(&whiteout->i_lock);
  1173. whiteout_ui = ubifs_inode(whiteout);
  1174. whiteout_ui->data = dev;
  1175. whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
  1176. ubifs_assert(c, !whiteout_ui->dirty);
  1177. memset(&wht_req, 0, sizeof(struct ubifs_budget_req));
  1178. wht_req.dirtied_ino = 1;
  1179. wht_req.dirtied_ino_d = ALIGN(whiteout_ui->data_len, 8);
  1180. /*
  1181. * To avoid deadlock between space budget (holds ui_mutex and
  1182. * waits wb work) and writeback work(waits ui_mutex), do space
  1183. * budget before ubifs inodes locked.
  1184. */
  1185. err = ubifs_budget_space(c, &wht_req);
  1186. if (err) {
  1187. iput(whiteout);
  1188. goto out_release;
  1189. }
  1190. /* Add the old_dentry size to the old_dir size. */
  1191. old_sz -= CALC_DENT_SIZE(fname_len(&old_nm));
  1192. }
  1193. lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1194. /*
  1195. * Like most other Unix systems, set the @i_ctime for inodes on a
  1196. * rename.
  1197. */
  1198. time = current_time(old_dir);
  1199. old_inode->i_ctime = time;
  1200. /* We must adjust parent link count when renaming directories */
  1201. if (is_dir) {
  1202. if (move) {
  1203. /*
  1204. * @old_dir loses a link because we are moving
  1205. * @old_inode to a different directory.
  1206. */
  1207. drop_nlink(old_dir);
  1208. /*
  1209. * @new_dir only gains a link if we are not also
  1210. * overwriting an existing directory.
  1211. */
  1212. if (!unlink)
  1213. inc_nlink(new_dir);
  1214. } else {
  1215. /*
  1216. * @old_inode is not moving to a different directory,
  1217. * but @old_dir still loses a link if we are
  1218. * overwriting an existing directory.
  1219. */
  1220. if (unlink)
  1221. drop_nlink(old_dir);
  1222. }
  1223. }
  1224. old_dir->i_size -= old_sz;
  1225. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  1226. old_dir->i_mtime = old_dir->i_ctime = time;
  1227. new_dir->i_mtime = new_dir->i_ctime = time;
  1228. /*
  1229. * And finally, if we unlinked a direntry which happened to have the
  1230. * same name as the moved direntry, we have to decrement @i_nlink of
  1231. * the unlinked inode and change its ctime.
  1232. */
  1233. if (unlink) {
  1234. /*
  1235. * Directories cannot have hard-links, so if this is a
  1236. * directory, just clear @i_nlink.
  1237. */
  1238. saved_nlink = new_inode->i_nlink;
  1239. if (is_dir)
  1240. clear_nlink(new_inode);
  1241. else
  1242. drop_nlink(new_inode);
  1243. new_inode->i_ctime = time;
  1244. } else {
  1245. new_dir->i_size += new_sz;
  1246. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  1247. }
  1248. /*
  1249. * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
  1250. * is dirty, because this will be done later on at the end of
  1251. * 'ubifs_rename()'.
  1252. */
  1253. if (IS_SYNC(old_inode)) {
  1254. sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  1255. if (unlink && IS_SYNC(new_inode))
  1256. sync = 1;
  1257. }
  1258. if (whiteout) {
  1259. inc_nlink(whiteout);
  1260. mark_inode_dirty(whiteout);
  1261. spin_lock(&whiteout->i_lock);
  1262. whiteout->i_state &= ~I_LINKABLE;
  1263. spin_unlock(&whiteout->i_lock);
  1264. iput(whiteout);
  1265. }
  1266. err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
  1267. new_inode, &new_nm, whiteout, sync);
  1268. if (err)
  1269. goto out_cancel;
  1270. unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1271. ubifs_release_budget(c, &req);
  1272. mutex_lock(&old_inode_ui->ui_mutex);
  1273. release = old_inode_ui->dirty;
  1274. mark_inode_dirty_sync(old_inode);
  1275. mutex_unlock(&old_inode_ui->ui_mutex);
  1276. if (release)
  1277. ubifs_release_budget(c, &ino_req);
  1278. if (IS_SYNC(old_inode))
  1279. err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
  1280. fscrypt_free_filename(&old_nm);
  1281. fscrypt_free_filename(&new_nm);
  1282. return err;
  1283. out_cancel:
  1284. if (unlink) {
  1285. set_nlink(new_inode, saved_nlink);
  1286. } else {
  1287. new_dir->i_size -= new_sz;
  1288. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  1289. }
  1290. old_dir->i_size += old_sz;
  1291. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  1292. if (is_dir) {
  1293. if (move) {
  1294. inc_nlink(old_dir);
  1295. if (!unlink)
  1296. drop_nlink(new_dir);
  1297. } else {
  1298. if (unlink)
  1299. inc_nlink(old_dir);
  1300. }
  1301. }
  1302. if (whiteout) {
  1303. drop_nlink(whiteout);
  1304. iput(whiteout);
  1305. }
  1306. unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1307. out_release:
  1308. ubifs_release_budget(c, &ino_req);
  1309. ubifs_release_budget(c, &req);
  1310. fscrypt_free_filename(&old_nm);
  1311. fscrypt_free_filename(&new_nm);
  1312. return err;
  1313. }
  1314. static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
  1315. struct inode *new_dir, struct dentry *new_dentry)
  1316. {
  1317. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  1318. struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  1319. .dirtied_ino = 2 };
  1320. int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  1321. struct inode *fst_inode = d_inode(old_dentry);
  1322. struct inode *snd_inode = d_inode(new_dentry);
  1323. struct timespec64 time;
  1324. int err;
  1325. struct fscrypt_name fst_nm, snd_nm;
  1326. ubifs_assert(c, fst_inode && snd_inode);
  1327. err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
  1328. if (err)
  1329. return err;
  1330. err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
  1331. if (err) {
  1332. fscrypt_free_filename(&fst_nm);
  1333. return err;
  1334. }
  1335. lock_4_inodes(old_dir, new_dir, NULL, NULL);
  1336. time = current_time(old_dir);
  1337. fst_inode->i_ctime = time;
  1338. snd_inode->i_ctime = time;
  1339. old_dir->i_mtime = old_dir->i_ctime = time;
  1340. new_dir->i_mtime = new_dir->i_ctime = time;
  1341. if (old_dir != new_dir) {
  1342. if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
  1343. inc_nlink(new_dir);
  1344. drop_nlink(old_dir);
  1345. }
  1346. else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
  1347. drop_nlink(new_dir);
  1348. inc_nlink(old_dir);
  1349. }
  1350. }
  1351. err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
  1352. snd_inode, &snd_nm, sync);
  1353. unlock_4_inodes(old_dir, new_dir, NULL, NULL);
  1354. ubifs_release_budget(c, &req);
  1355. fscrypt_free_filename(&fst_nm);
  1356. fscrypt_free_filename(&snd_nm);
  1357. return err;
  1358. }
  1359. static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1360. struct inode *new_dir, struct dentry *new_dentry,
  1361. unsigned int flags)
  1362. {
  1363. int err;
  1364. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  1365. if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
  1366. return -EINVAL;
  1367. ubifs_assert(c, inode_is_locked(old_dir));
  1368. ubifs_assert(c, inode_is_locked(new_dir));
  1369. err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
  1370. flags);
  1371. if (err)
  1372. return err;
  1373. if (flags & RENAME_EXCHANGE)
  1374. return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
  1375. return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  1376. }
  1377. int ubifs_getattr(const struct path *path, struct kstat *stat,
  1378. u32 request_mask, unsigned int flags)
  1379. {
  1380. loff_t size;
  1381. struct inode *inode = d_inode(path->dentry);
  1382. struct ubifs_inode *ui = ubifs_inode(inode);
  1383. mutex_lock(&ui->ui_mutex);
  1384. if (ui->flags & UBIFS_APPEND_FL)
  1385. stat->attributes |= STATX_ATTR_APPEND;
  1386. if (ui->flags & UBIFS_COMPR_FL)
  1387. stat->attributes |= STATX_ATTR_COMPRESSED;
  1388. if (ui->flags & UBIFS_CRYPT_FL)
  1389. stat->attributes |= STATX_ATTR_ENCRYPTED;
  1390. if (ui->flags & UBIFS_IMMUTABLE_FL)
  1391. stat->attributes |= STATX_ATTR_IMMUTABLE;
  1392. stat->attributes_mask |= (STATX_ATTR_APPEND |
  1393. STATX_ATTR_COMPRESSED |
  1394. STATX_ATTR_ENCRYPTED |
  1395. STATX_ATTR_IMMUTABLE);
  1396. generic_fillattr(inode, stat);
  1397. stat->blksize = UBIFS_BLOCK_SIZE;
  1398. stat->size = ui->ui_size;
  1399. /*
  1400. * Unfortunately, the 'stat()' system call was designed for block
  1401. * device based file systems, and it is not appropriate for UBIFS,
  1402. * because UBIFS does not have notion of "block". For example, it is
  1403. * difficult to tell how many block a directory takes - it actually
  1404. * takes less than 300 bytes, but we have to round it to block size,
  1405. * which introduces large mistake. This makes utilities like 'du' to
  1406. * report completely senseless numbers. This is the reason why UBIFS
  1407. * goes the same way as JFFS2 - it reports zero blocks for everything
  1408. * but regular files, which makes more sense than reporting completely
  1409. * wrong sizes.
  1410. */
  1411. if (S_ISREG(inode->i_mode)) {
  1412. size = ui->xattr_size;
  1413. size += stat->size;
  1414. size = ALIGN(size, UBIFS_BLOCK_SIZE);
  1415. /*
  1416. * Note, user-space expects 512-byte blocks count irrespectively
  1417. * of what was reported in @stat->size.
  1418. */
  1419. stat->blocks = size >> 9;
  1420. } else
  1421. stat->blocks = 0;
  1422. mutex_unlock(&ui->ui_mutex);
  1423. return 0;
  1424. }
  1425. const struct inode_operations ubifs_dir_inode_operations = {
  1426. .lookup = ubifs_lookup,
  1427. .create = ubifs_create,
  1428. .link = ubifs_link,
  1429. .symlink = ubifs_symlink,
  1430. .unlink = ubifs_unlink,
  1431. .mkdir = ubifs_mkdir,
  1432. .rmdir = ubifs_rmdir,
  1433. .mknod = ubifs_mknod,
  1434. .rename = ubifs_rename,
  1435. .setattr = ubifs_setattr,
  1436. .getattr = ubifs_getattr,
  1437. #ifdef CONFIG_UBIFS_FS_XATTR
  1438. .listxattr = ubifs_listxattr,
  1439. #endif
  1440. .update_time = ubifs_update_time,
  1441. .tmpfile = ubifs_tmpfile,
  1442. };
  1443. const struct file_operations ubifs_dir_operations = {
  1444. .llseek = generic_file_llseek,
  1445. .release = ubifs_dir_release,
  1446. .read = generic_read_dir,
  1447. .iterate_shared = ubifs_readdir,
  1448. .fsync = ubifs_fsync,
  1449. .unlocked_ioctl = ubifs_ioctl,
  1450. #ifdef CONFIG_COMPAT
  1451. .compat_ioctl = ubifs_compat_ioctl,
  1452. #endif
  1453. };