dir.c 44 KB

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