dir.c 43 KB

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