dir.c 43 KB

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