dir.c 46 KB

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