inode.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. RAWFS: Raw file system for NAND flash
  3. Copyright (C) 2012-2015 Perry Hsu <perry.hsu@mediatek.com>"
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #if defined(CONFIG_MT_ENG_BUILD)
  8. #define DEBUG 1
  9. #endif
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include <linux/list.h>
  13. #include <linux/hash.h>
  14. #include <linux/slab.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/crc32.h>
  17. #include <linux/mm.h>
  18. #include "rawfs.h"
  19. #define CEILING(x, y) rawfs_div(((x)+(y)-1), (y))
  20. /* file operations */
  21. static const struct file_operations rawfs_file_operations = {
  22. .read = do_sync_read,
  23. .write = do_sync_write,
  24. .mmap = generic_file_mmap,
  25. .llseek = generic_file_llseek,
  26. .aio_read = rawfs_reg_file_aio_read,
  27. .aio_write = rawfs_reg_file_aio_write,
  28. };
  29. static const struct file_operations rawfs_block_file_operations = {
  30. .read = do_sync_read,
  31. .aio_read = rawfs_block_file_aio_read,
  32. .write = do_sync_write,
  33. .aio_write = rawfs_block_file_aio_write,
  34. .mmap = generic_file_mmap,
  35. .llseek = generic_file_llseek,
  36. };
  37. static const struct file_operations rawfs_dir_operations = {
  38. .open = dcache_dir_open,
  39. .release = dcache_dir_close,
  40. .llseek = dcache_dir_lseek,
  41. .read = generic_read_dir,
  42. .iterate = rawfs_readdir,
  43. .fsync = rawfs_file_sync,
  44. };
  45. /* dentry operations */
  46. static const struct dentry_operations rawfs_dentry_ops = {
  47. .d_delete = rawfs_delete_dentry,
  48. };
  49. /* Case in-sensitive dentry operations */
  50. static const struct dentry_operations rawfs_ci_dentry_ops = {
  51. /* .d_revalidate, Not necessary for rawfs */
  52. .d_hash = rawfs_ci_hash,
  53. .d_compare = rawfs_compare_dentry,
  54. .d_delete = rawfs_delete_dentry,
  55. };
  56. /* Address Space Operations: Block file & Normal file */
  57. static const struct address_space_operations rawfs_aops = {
  58. .readpage = rawfs_readpage,
  59. .write_begin = rawfs_write_begin,
  60. .write_end = rawfs_write_end,
  61. };
  62. static struct backing_dev_info rawfs_backing_dev_info = {
  63. .ra_pages = 0, /* No readahead */
  64. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK |
  65. BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |
  66. BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP |
  67. BDI_CAP_EXEC_MAP,
  68. };
  69. static unsigned long rawfs_hash(const char *name)
  70. {
  71. unsigned long val;
  72. val = crc32(0, name, strlen(name));
  73. return hash_32(val, RAWFS_HASH_BITS);
  74. }
  75. static void rawfs_attach(struct inode *inode, const char *name)
  76. {
  77. struct rawfs_sb_info *sbi = RAWFS_SB(inode->i_sb);
  78. struct hlist_head *head = sbi->inode_hashtable + rawfs_hash(name);
  79. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_attach %s %lx to hlist[%d]=%lx\n", name,
  80. (unsigned long)(inode), (unsigned)rawfs_hash(name), (unsigned long)head);
  81. spin_lock(&sbi->inode_hash_lock);
  82. hlist_add_head(&RAWFS_I(inode)->i_rawfs_hash, head);
  83. spin_unlock(&sbi->inode_hash_lock);
  84. }
  85. static void rawfs_detach(struct inode *inode)
  86. {
  87. struct rawfs_sb_info *sbi = RAWFS_SB(inode->i_sb);
  88. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_detach %s %lx from hlist\n",
  89. RAWFS_I(inode)->i_name, (unsigned long)(inode));
  90. spin_lock(&sbi->inode_hash_lock);
  91. hlist_del_init(&RAWFS_I(inode)->i_rawfs_hash);
  92. spin_unlock(&sbi->inode_hash_lock);
  93. }
  94. /** Inode Cache */
  95. static struct kmem_cache *rawfs_inode_cachep;
  96. struct inode *rawfs_alloc_inode(struct super_block *sb)
  97. {
  98. struct rawfs_inode_info *ei;
  99. ei = kmem_cache_alloc(rawfs_inode_cachep, GFP_NOFS);
  100. if (!ei) {
  101. RAWFS_PRINT(RAWFS_DBG_INODE,
  102. "rawfs_alloc_inode, allocation failed, out of memory\n");
  103. return NULL;
  104. }
  105. init_rwsem(&ei->truncate_lock);
  106. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_alloc_inode, %lx\n", (unsigned long)(ei));
  107. return &ei->vfs_inode;
  108. }
  109. EXPORT_SYMBOL_GPL(rawfs_alloc_inode);
  110. static void rawfs_i_callback(struct rcu_head *head)
  111. {
  112. struct inode *inode = container_of(head, struct inode, i_rcu);
  113. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_i_callback, %lx\n",
  114. (unsigned long)(RAWFS_I(inode)));
  115. kmem_cache_free(rawfs_inode_cachep, RAWFS_I(inode));
  116. }
  117. void rawfs_destroy_inode(struct inode *inode)
  118. {
  119. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_destroy_inode, %lx\n",
  120. (unsigned long)(inode));
  121. remove_inode_hash(inode);
  122. rawfs_detach(inode);
  123. call_rcu(&inode->i_rcu, rawfs_i_callback);
  124. }
  125. EXPORT_SYMBOL_GPL(rawfs_destroy_inode);
  126. static void init_once(void *foo)
  127. {
  128. struct rawfs_inode_info *ei = (struct rawfs_inode_info *)foo;
  129. spin_lock_init(&ei->cache_lru_lock);
  130. ei->nr_caches = 0;
  131. ei->cache_valid_id = RAWFS_CACHE_VALID + 1;
  132. INIT_LIST_HEAD(&ei->cache_lru);
  133. INIT_HLIST_NODE(&ei->i_rawfs_hash);
  134. inode_init_once(&ei->vfs_inode);
  135. }
  136. int __init rawfs_init_inodecache(void)
  137. {
  138. rawfs_inode_cachep = kmem_cache_create("rawfs_inode_cache",
  139. sizeof(struct rawfs_inode_info),
  140. 0, (SLAB_RECLAIM_ACCOUNT|
  141. SLAB_MEM_SPREAD),
  142. init_once);
  143. if (rawfs_inode_cachep == NULL) {
  144. RAWFS_PRINT(RAWFS_DBG_INODE,
  145. "rawfs_init_inodecache, failed, -ENOMEM\n");
  146. return -ENOMEM;
  147. }
  148. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_init_inodecache\n");
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(rawfs_init_inodecache);
  152. void __exit rawfs_destroy_inodecache(void)
  153. {
  154. kmem_cache_destroy(rawfs_inode_cachep);
  155. }
  156. EXPORT_SYMBOL_GPL(rawfs_destroy_inodecache);
  157. /* ----------------------------------------------------------------------------- */
  158. static unsigned long rawfs_unique_id(struct super_block *sb, umode_t mode,
  159. const char *name, int parent_folder)
  160. {
  161. int result;
  162. int len = strlen(name);
  163. int seed = parent_folder;
  164. do {
  165. result = crc32(seed, name, len);
  166. seed++;
  167. } while (rawfs_file_list_get_by_id(sb, mode, result) != NULL);
  168. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_unique_id: new %s %s, id = %X\n",
  169. (S_ISDIR(mode))?"folder":"file", name, result);
  170. return result;
  171. }
  172. void rawfs_hash_init(struct super_block *sb)
  173. {
  174. struct rawfs_sb_info *sbi = RAWFS_SB(sb);
  175. int i;
  176. spin_lock_init(&sbi->inode_hash_lock);
  177. for (i = 0; i < RAWFS_HASH_SIZE; i++)
  178. INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
  179. }
  180. EXPORT_SYMBOL_GPL(rawfs_hash_init);
  181. /* Get inode from inode cache */
  182. struct inode *rawfs_iget(struct super_block *sb, const char *name, int folder)
  183. {
  184. struct rawfs_sb_info *sbi = RAWFS_SB(sb);
  185. struct hlist_head *head = sbi->inode_hashtable + rawfs_hash(name);
  186. /* struct hlist_node *ptr; */
  187. struct rawfs_inode_info *inode_info;
  188. struct inode *inode = NULL;
  189. int index = 0;
  190. RAWFS_PRINT(RAWFS_DBG_INODE,
  191. "rawfs_iget, by name %s @ folder %X in inode_hashtable[%ld]=%lx\n",
  192. name, folder, rawfs_hash(name), (unsigned long)head);
  193. /* Search inode list for the file */
  194. spin_lock(&sbi->inode_hash_lock);
  195. hlist_for_each_entry(inode_info, head, i_rawfs_hash) {
  196. RAWFS_PRINT(RAWFS_DBG_INODE,
  197. "rawfs_iget, inode_hashtable[%ld][%d] %s @ folder %X, %lx -> %lx\n",
  198. rawfs_hash(name), index,
  199. inode_info->i_name,
  200. inode_info->i_parent_folder_id,
  201. (unsigned long)(&inode_info->vfs_inode),
  202. (unsigned long)(inode_info));
  203. index++;
  204. if (inode_info->i_parent_folder_id != folder)
  205. continue;
  206. /* BUG_ON(i->vfs_inode.i_sb != sb); */
  207. if (strnicmp(inode_info->i_name, name, RAWFS_MAX_FILENAME_LEN+4) == 0) {
  208. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_iget, igrab %lx\n",
  209. (unsigned long)(&inode_info->vfs_inode));
  210. inode = igrab(&inode_info->vfs_inode);
  211. if (inode)
  212. break;
  213. }
  214. }
  215. if (IS_ERR_OR_NULL(inode))
  216. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_iget, %s was not found\n", name);
  217. else
  218. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_iget, %s was found at %lx\n", name,
  219. (unsigned long)(inode));
  220. spin_unlock(&sbi->inode_hash_lock);
  221. return inode;
  222. }
  223. EXPORT_SYMBOL_GPL(rawfs_iget);
  224. static int rawfs_fill_inode_blk(struct inode *inode,
  225. struct rawfs_file_info *file_info, int block_no, int page_no)
  226. {
  227. struct rawfs_inode_info *inode_info = RAWFS_I(inode);
  228. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  229. inode->i_uid = current_uid();
  230. inode->i_gid = current_gid();
  231. inode->i_size = RAWFS_SB(inode->i_sb)->block_size;
  232. inode->i_ino = RAWFS_BLOCK0_INO + block_no;
  233. inode->i_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  234. sprintf(inode_info->i_name, ".block%d", block_no);
  235. inode_info->i_location_block = block_no;
  236. inode_info->i_location_page = 0;
  237. inode_info->i_location_page_count = 0;
  238. inode_info->i_parent_folder_id = RAWFS_ROOT_DIR_ID;
  239. return 0;
  240. }
  241. static int rawfs_fill_inode_reg(struct inode *inode,
  242. struct rawfs_file_info *file_info, int block_no, int page_no)
  243. {
  244. struct rawfs_inode_info *inode_info = RAWFS_I(inode);
  245. int name_len;
  246. name_len = strlen(file_info->i_name);
  247. strncpy(inode_info->i_name, file_info->i_name, name_len+1);
  248. inode->i_atime = file_info->i_atime;
  249. inode->i_mtime = file_info->i_mtime;
  250. inode->i_ctime = file_info->i_ctime;
  251. inode->i_uid.val = file_info->i_uid;
  252. inode->i_gid.val = file_info->i_gid;
  253. inode->i_mode = file_info->i_mode; /* Use file_info ones */
  254. inode->i_ino = iunique(inode->i_sb, RAWFS_MAX_RESERVED_INO);
  255. inode_info->i_id = file_info->i_id;
  256. inode_info->i_location_page_count = file_info->i_chunk_total;
  257. inode_info->i_parent_folder_id = file_info->i_parent_folder_id;
  258. inode_info->i_location_block = block_no;
  259. inode_info->i_location_page = page_no;
  260. return 0;
  261. }
  262. int rawfs_subdirs(struct inode *dir)
  263. {
  264. struct rawfs_sb_info *sbi = RAWFS_SB(dir->i_sb);
  265. struct rawfs_file_list_entry *ptr;
  266. int folder_id = RAWFS_I(dir)->i_id;
  267. int count = 0;
  268. mutex_lock(&sbi->file_list_lock);
  269. list_for_each_entry(ptr, &sbi->folder_list, list) {
  270. if (ptr->file_info.i_parent_folder_id == folder_id)
  271. count++;
  272. }
  273. mutex_unlock(&sbi->file_list_lock);
  274. return count;
  275. }
  276. static struct inode *rawfs_build_inode(struct super_block *sb,
  277. struct rawfs_file_info *file_info, int block_no, int page_no)
  278. {
  279. struct inode *inode = NULL;
  280. umode_t mode = S_IFREG | 755;
  281. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_build_inode");
  282. if (file_info) {
  283. inode = rawfs_iget(sb, file_info->i_name,
  284. file_info->i_parent_folder_id);
  285. if (inode) {
  286. iput(inode);
  287. goto out;
  288. } else { /* Not exist in inode cache, search in file list */
  289. struct rawfs_file_list_entry *entry;
  290. entry = rawfs_file_list_get(sb, file_info->i_name,
  291. file_info->i_parent_folder_id);
  292. if (entry) {
  293. memcpy(file_info, &entry->file_info,
  294. sizeof(struct rawfs_file_info));
  295. block_no = entry->i_location_block;
  296. page_no = entry->i_location_page;
  297. } else { /* New File, or new folder */
  298. file_info->i_atime = file_info->i_mtime = file_info->i_ctime =
  299. CURRENT_TIME_SEC;
  300. file_info->i_uid = current_uid().val;
  301. file_info->i_gid = current_gid().val;
  302. file_info->i_size = 0;
  303. /* file_info->i_mode |= (S_IRUGO | S_IXUGO); */
  304. /* file_info->i_chunk_index; */
  305. file_info->i_chunk_total = 1;
  306. /* file_info->i_mode; assigned as S_IFREG */
  307. /* file_info->i_crc; */
  308. /* i_id will be the ino of the inode */
  309. file_info->i_id = rawfs_unique_id(sb, file_info->i_mode,
  310. file_info->i_name, file_info->i_parent_folder_id);
  311. }
  312. mode = file_info->i_mode;
  313. }
  314. }
  315. inode = new_inode(sb);
  316. if (IS_ERR_OR_NULL(inode)) {
  317. RAWFS_PRINT(RAWFS_DBG_INODE,
  318. "rawfs_build_inode: new_inode failed %lx\n", (unsigned long)(inode));
  319. goto out;
  320. }
  321. inode->i_version = 1;
  322. rawfs_fill_inode(inode, file_info, block_no, page_no, mode , 0);
  323. /* Default mode for Block file & Super block is S_IFREG | 755 */
  324. insert_inode_hash(inode);
  325. rawfs_attach(inode, RAWFS_I(inode)->i_name);
  326. out:
  327. return inode;
  328. }
  329. static struct inode *rawfs_build_inode_from_dentry(struct super_block *sb,
  330. struct inode *dir, struct dentry *dentry, umode_t mode)
  331. {
  332. struct rawfs_file_info file_info;
  333. struct inode *inode;
  334. strncpy(file_info.i_name, dentry->d_name.name, RAWFS_MAX_FILENAME_LEN+4);
  335. file_info.i_mode = mode;
  336. file_info.i_parent_folder_id = RAWFS_I(dir)->i_id;
  337. inode = rawfs_build_inode(sb, &file_info, -1, -1);
  338. return inode;
  339. }
  340. /*
  341. * Lookup the data, if the dentry didn't already exist, it must be
  342. * negative. Set d_op to delete negative dentries to save memory
  343. * (and since it does not help performance for in memory filesystem).
  344. */
  345. static struct dentry *rawfs_lookup(struct inode *dir, struct dentry *dentry,
  346. unsigned int flags)
  347. {
  348. struct inode *inode = NULL;
  349. static struct dentry *result;
  350. struct super_block *sb = dir->i_sb;
  351. struct rawfs_sb_info *sbi = RAWFS_SB(dir->i_sb);
  352. struct rawfs_file_list_entry *entry;
  353. /* struct nls_table *codepage = RAWFS_SB(dentry->d_inode->i_sb)->local_nls; */
  354. mutex_lock(&sbi->rawfs_lock);
  355. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_lookup: %s, len %d\n",
  356. dentry->d_name.name, dentry->d_name.len);
  357. if (sbi->flags & RAWFS_MNT_CASE)
  358. dentry->d_op = &rawfs_ci_dentry_ops; /* case insensitive */
  359. else
  360. dentry->d_op = &rawfs_dentry_ops; /* case sensitive */
  361. entry = rawfs_file_list_get(sb, dentry->d_name.name, RAWFS_I(dir)->i_id);
  362. if (entry)
  363. inode = rawfs_build_inode_from_dentry(sb, dir, dentry,
  364. entry->file_info.i_mode);
  365. d_add(dentry, inode);
  366. mutex_unlock(&sbi->rawfs_lock);
  367. return result;
  368. }
  369. static int
  370. rawfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  371. {
  372. struct inode *inode;
  373. int error = -ENOSPC;
  374. inode = rawfs_build_inode_from_dentry(dir->i_sb, dir, dentry, mode);
  375. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_mknod %s %X = %lx, i_nlink=%d\n",
  376. dentry->d_name.name, mode, (unsigned long)inode, inode->i_nlink);
  377. if (inode) {
  378. RAWFS_PRINT(RAWFS_DBG_INODE,
  379. "rawfs_mknod: parent dir ID=%X, ino=%X\n",
  380. RAWFS_I(dir)->i_id, (unsigned)dir->i_ino);
  381. if (dir->i_mode & S_ISGID) {
  382. inode->i_gid = dir->i_gid;
  383. if (S_ISDIR(mode))
  384. inode->i_mode |= S_ISGID;
  385. }
  386. d_instantiate(dentry, inode);
  387. dget(dentry); /* Extra count - pin the dentry in core */
  388. error = 0;
  389. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  390. /* real filesystems would normally use i_size_write function */
  391. dir->i_size += 0x20; /* bogus small size for each dir entry */
  392. } else {
  393. RAWFS_PRINT(RAWFS_DBG_INODE, "mknod: inode is already exist NULL\n");
  394. }
  395. return error;
  396. }
  397. static int rawfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  398. {
  399. struct super_block *sb = dentry->d_sb;
  400. struct rawfs_sb_info *sbi = RAWFS_SB(sb);
  401. struct rawfs_file_info fi;
  402. struct rawfs_file_list_entry *entry;
  403. struct rawfs_inode_info *inode_info;
  404. int result = 0;
  405. mutex_lock(&sbi->rawfs_lock);
  406. RAWFS_PRINT(RAWFS_DBG_DIR,
  407. "rawfs_mkdir: dentry name %s, parent id=%X, parent ino=%X\n", dentry->d_name.name,
  408. RAWFS_I(dir)->i_id,
  409. (unsigned)dir->i_ino);
  410. if (dir->i_ino != RAWFS_ROOT_INO) {
  411. RAWFS_PRINT(RAWFS_DBG_DIR,
  412. "rawfs_mkdir: sub-folder only allowed in root dir\n");
  413. result = -EACCES;
  414. goto out;
  415. }
  416. if (dentry->d_name.len > RAWFS_MAX_FILENAME_LEN) {
  417. RAWFS_PRINT(RAWFS_DBG_DIR,
  418. "rawfs_mkdir: foldername is too long -ENAMETOOLONG\n");
  419. result = -ENAMETOOLONG;
  420. goto out;
  421. }
  422. entry = rawfs_file_list_get(sb, dentry->d_name.name, RAWFS_I(dir)->i_id);
  423. if (entry) {
  424. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_mkdir, target already exists\n");
  425. result = -EEXIST;
  426. goto out;
  427. }
  428. /* Do GC, if there's no free_pages */
  429. result = rawfs_reserve_space(sb, 1);
  430. if (result < 0)
  431. goto out;
  432. inc_nlink(dir);
  433. result = rawfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  434. set_nlink(dentry->d_inode, 2);
  435. if (result < 0)
  436. goto out;
  437. result = rawfs_reg_file_create(dir, dentry, mode | S_IFDIR, 0);
  438. if (result < 0)
  439. goto out;
  440. /* rawfs_fill_file_info(dentry->d_inode, &fi); */
  441. rawfs_fill_fileinfo_by_dentry(dentry, &fi);
  442. inode_info = RAWFS_I(dentry->d_inode);
  443. rawfs_file_list_add(sb, &fi, inode_info->i_location_block,
  444. inode_info->i_location_page);
  445. out:
  446. mutex_unlock(&sbi->rawfs_lock);
  447. return result;
  448. }
  449. static int rawfs_rmdir(struct inode *dir, struct dentry *dentry)
  450. {
  451. struct super_block *sb = dentry->d_sb;
  452. struct rawfs_sb_info *sbi = RAWFS_SB(sb);
  453. struct rawfs_file_list_entry *ptr;
  454. struct rawfs_file_list_entry *entry = NULL;
  455. int result = 0;
  456. mutex_lock(&sbi->rawfs_lock);
  457. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_rmdir, dentry name %s, parent id=%X, parent ino=%X\n",
  458. dentry->d_name.name, RAWFS_I(dir)->i_id, (unsigned)dir->i_ino);
  459. /* Is the file exists ? */
  460. entry = rawfs_file_list_get(sb, dentry->d_name.name, RAWFS_I(dir)->i_id);
  461. if (!entry) {
  462. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_rmdir, folder not exist\n");
  463. result = -ENOENT;
  464. goto out;
  465. }
  466. if (!S_ISDIR(entry->file_info.i_mode)) {
  467. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_rmdir, target is not a folder\n");
  468. result = -ENOTDIR;
  469. goto out;
  470. }
  471. mutex_lock(&sbi->file_list_lock);
  472. /* Check the folder is empty */
  473. list_for_each_entry(ptr, &sbi->file_list, list) {
  474. if (ptr->file_info.i_parent_folder_id == RAWFS_I(dentry->d_inode)->i_id) {
  475. result = -ENOTEMPTY;
  476. break;
  477. }
  478. }
  479. mutex_unlock(&sbi->file_list_lock);
  480. if (result < 0)
  481. goto out;
  482. result = rawfs_reg_file_delete(dir, dentry);
  483. if (result < 0)
  484. goto out;
  485. /* detach from inode cache, if it exists */
  486. drop_nlink(dir);
  487. clear_nlink(dentry->d_inode);
  488. rawfs_detach(dentry->d_inode);
  489. rawfs_file_list_remove(sb, &entry->file_info);
  490. out:
  491. mutex_unlock(&sbi->rawfs_lock);
  492. return result;
  493. }
  494. static int rawfs_dir_create(struct inode *dir, struct dentry *dentry,
  495. umode_t mode, bool excl)
  496. {
  497. struct super_block *sb = dentry->d_sb;
  498. struct rawfs_sb_info *sbi = RAWFS_SB(sb);
  499. struct rawfs_file_info fi;
  500. struct rawfs_file_list_entry *entry;
  501. struct rawfs_inode_info *inode_info;
  502. int result = 0;
  503. mutex_lock(&sbi->rawfs_lock);
  504. RAWFS_PRINT(RAWFS_DBG_DIR,
  505. "rawfs_dir_create: %s, len %d\n",
  506. dentry->d_name.name, dentry->d_name.len);
  507. if (dentry->d_name.len > RAWFS_MAX_FILENAME_LEN) {
  508. RAWFS_PRINT(RAWFS_DBG_DIR,
  509. "rawfs_dir_create: filename is too long -ENAMETOOLONG\n");
  510. result = -ENAMETOOLONG;
  511. goto out;
  512. }
  513. entry = rawfs_file_list_get(sb, dentry->d_name.name, RAWFS_I(dir)->i_id);
  514. if (entry) {
  515. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_dir_create, file already exists\n");
  516. result = -EEXIST;
  517. goto out;
  518. }
  519. result = rawfs_reserve_space(sb, 1);
  520. if (result < 0)
  521. goto out;
  522. result = rawfs_mknod(dir, dentry, mode | S_IFREG, 0);
  523. if (result < 0)
  524. goto out;
  525. result = rawfs_reg_file_create(dir, dentry, mode | S_IFREG, 0);
  526. if (result < 0)
  527. goto out;
  528. /* rawfs_fill_file_info(dentry->d_inode, &fi); */
  529. rawfs_fill_fileinfo_by_dentry(dentry, &fi);
  530. inode_info = RAWFS_I(dentry->d_inode);
  531. rawfs_file_list_add(sb, &fi, inode_info->i_location_block,
  532. inode_info->i_location_page);
  533. out:
  534. mutex_unlock(&sbi->rawfs_lock);
  535. return result;
  536. }
  537. static int rawfs_dir_unlink(struct inode *dir, struct dentry *dentry)
  538. {
  539. struct super_block *sb = dentry->d_sb;
  540. struct rawfs_sb_info *sbi = RAWFS_SB(sb);
  541. struct rawfs_file_list_entry *entry;
  542. int result = 0;
  543. mutex_lock(&sbi->rawfs_lock);
  544. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_dir_unlink, dentry name %s\n",
  545. dentry->d_name.name);
  546. /* Is the file exists ? */
  547. entry = rawfs_file_list_get(sb, dentry->d_name.name, RAWFS_I(dir)->i_id);
  548. if (!entry) {
  549. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_dir_unlink, file not exist\n");
  550. result = -ENOENT;
  551. goto out;
  552. }
  553. result = rawfs_reg_file_delete(dir, dentry);
  554. if (result < 0)
  555. goto out;
  556. /* detach from inode cache, if it exists */
  557. clear_nlink(dentry->d_inode);
  558. rawfs_detach(dentry->d_inode);
  559. rawfs_file_list_remove(sb, &entry->file_info);
  560. out:
  561. mutex_unlock(&sbi->rawfs_lock);
  562. return result;
  563. }
  564. static int rawfs_dir_rename(struct inode *old_dir, struct dentry *old_dentry,
  565. struct inode *new_dir, struct dentry *new_dentry)
  566. {
  567. struct super_block *sb = old_dentry->d_sb;
  568. struct rawfs_sb_info *sbi = RAWFS_SB(sb);
  569. struct inode *inode;
  570. struct inode *old_inode, *new_inode;
  571. struct rawfs_file_list_entry *entry;
  572. struct rawfs_inode_info *inode_info;
  573. struct timespec ts;
  574. int result = 0, is_dir, update_dotdot;
  575. mutex_lock(&sbi->rawfs_lock);
  576. old_inode = old_dentry->d_inode;
  577. new_inode = new_dentry->d_inode;
  578. is_dir = S_ISDIR(old_inode->i_mode);
  579. update_dotdot = (is_dir && old_dir != new_dir);
  580. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_dir_rename, %s (%lx) -> %s (%lx)\n",
  581. old_dentry->d_name.name, (unsigned long)old_dentry,
  582. new_dentry->d_name.name, (unsigned long)new_dentry);
  583. if (new_dentry->d_name.len > RAWFS_MAX_FILENAME_LEN) {
  584. RAWFS_PRINT(RAWFS_DBG_DIR,
  585. "rawfs_dir_rename: filename is too long -ENAMETOOLONG, len %d\n",
  586. new_dentry->d_name.len);
  587. result = -ENAMETOOLONG;
  588. goto out;
  589. }
  590. /* Is target file already exist ? */
  591. entry = rawfs_file_list_get(sb, new_dentry->d_name.name, new_dir->i_ino);
  592. if (entry) {
  593. RAWFS_PRINT(RAWFS_DBG_DIR,
  594. "rawfs_dir_rename: target file already exists\n");
  595. result = -EEXIST;
  596. goto out;
  597. }
  598. /* Check source file existence */
  599. entry = rawfs_file_list_get(sb, old_dentry->d_name.name,
  600. RAWFS_I(old_dir)->i_id);
  601. if (!entry) {
  602. RAWFS_PRINT(RAWFS_DBG_DIR,
  603. "rawfs_dir_rename: source file not exists\n");
  604. result = -ENOENT;
  605. goto out;
  606. }
  607. ts = CURRENT_TIME_SEC;
  608. /* do GC, if the required space is not enough */
  609. result = rawfs_reserve_space(sb, 1);
  610. if (result < 0)
  611. goto out;
  612. /* No inodes will be created, */
  613. /* It will use original inode and dentry. */
  614. /* TODO: Copy inode properties */
  615. inode = rawfs_iget(sb, old_dentry->d_name.name, RAWFS_I(old_dir)->i_id);
  616. if (inode) {
  617. RAWFS_PRINT(RAWFS_DBG_DIR,
  618. "rawfs_dir_rename: found old inode %lx, expected %lx\n",
  619. (unsigned long)inode, (unsigned long)old_dentry->d_inode);
  620. iput(inode);
  621. rawfs_detach(old_dentry->d_inode);
  622. /* remove_inode_hash(old_dentry->d_inode); */
  623. }
  624. /* copy file with new name,
  625. this function will add new entry to the file list. */
  626. result = rawfs_reg_file_copy(old_dir, old_dentry, new_dir, new_dentry);
  627. if (result < 0)
  628. goto out;
  629. /* delete the origianl copy, if renamed to new folder */
  630. if (old_dir->i_ino != new_dir->i_ino)
  631. result = rawfs_reg_file_delete(old_dir, old_dentry);
  632. if (result < 0)
  633. goto out;
  634. /* Note: Inode info may change after copy/delete, or during, */
  635. /* We need to get latest info from file list again. */
  636. entry = rawfs_file_list_get(sb, old_dentry->d_name.name, RAWFS_I(old_dir)->i_id);
  637. if (entry)
  638. rawfs_file_list_remove(sb, &entry->file_info);
  639. old_inode->i_ctime = ts;
  640. if (inode) /* inode was detached, attached it back */
  641. rawfs_attach(inode, RAWFS_I(inode)->i_name);
  642. /* Update inode info */
  643. entry = rawfs_file_list_get(sb, new_dentry->d_name.name,
  644. RAWFS_I(new_dir)->i_id);
  645. if (entry) {
  646. inode_info = RAWFS_I(old_inode);
  647. /* inode_info = RAWFS_I(inode); */
  648. inode_info->i_location_block = entry->i_location_block;
  649. inode_info->i_location_page_count = entry->i_location_page_count;
  650. inode_info->i_location_page = entry->i_location_page;
  651. inode_info->i_parent_folder_id = entry->file_info.i_parent_folder_id;
  652. RAWFS_PRINT(RAWFS_DBG_DIR,
  653. "rawfs_dir_rename: new location block %d, page %d\n",
  654. inode_info->i_location_block, inode_info->i_location_page);
  655. } else {
  656. RAWFS_PRINT(RAWFS_DBG_DIR,
  657. "rawfs_dir_rename: new entry missing after rename\n");
  658. }
  659. if (update_dotdot) {
  660. drop_nlink(old_dir);
  661. if (!new_inode)
  662. inc_nlink(new_dir);
  663. }
  664. if (new_inode) {
  665. drop_nlink(new_inode);
  666. if (is_dir)
  667. drop_nlink(new_inode);
  668. new_inode->i_ctime = ts;
  669. }
  670. out:
  671. mutex_unlock(&sbi->rawfs_lock);
  672. return result;
  673. }
  674. static int rawfs_setattr(struct dentry *dentry, struct iattr *attr)
  675. {
  676. struct super_block *sb = dentry->d_sb;
  677. struct rawfs_sb_info *sbi = RAWFS_SB(sb);
  678. struct inode *inode = dentry->d_inode;
  679. unsigned int size_limit;
  680. struct rawfs_file_list_entry *entry = NULL;
  681. int result = 0;
  682. bool do_add = false;
  683. mutex_lock(&sbi->rawfs_lock);
  684. RAWFS_PRINT(RAWFS_DBG_DIR, "rawfs_setattr: %s %s, id = %X @ folder id %X, parent ino=%X\n",
  685. (S_ISDIR(dentry->d_inode->i_mode))?"folder":"file",
  686. dentry->d_name.name,
  687. RAWFS_I(dentry->d_inode)->i_id,
  688. RAWFS_I(dentry->d_parent->d_inode)->i_id,
  689. (unsigned)parent_ino(dentry));
  690. size_limit = ((sbi->pages_per_block-2) * (sbi->page_data_size));
  691. /* Fail if a requested resize >= 2GB */
  692. if (attr->ia_valid & ATTR_SIZE && (attr->ia_size > size_limit)) {
  693. RAWFS_PRINT(RAWFS_DBG_DIR,
  694. "rawfs_setattr: set length %llx beyond block size limit %d\n",
  695. attr->ia_size, size_limit);
  696. result = -EINVAL;
  697. goto out;
  698. }
  699. entry = rawfs_file_list_get(sb, dentry->d_name.name, RAWFS_I(dentry->d_parent->d_inode)->i_id);
  700. if (entry)
  701. do_add = true;
  702. result = inode_change_ok(inode, attr);
  703. if (result)
  704. goto out;
  705. setattr_copy(inode, attr);
  706. if (attr->ia_valid & ATTR_SIZE) {
  707. truncate_setsize(inode, attr->ia_size);
  708. inode->i_blocks = (inode->i_size + 511) >> 9;
  709. }
  710. if (do_add)
  711. rawfs_reg_file_copy(dentry->d_parent->d_inode, dentry, NULL, NULL);
  712. out:
  713. mutex_unlock(&sbi->rawfs_lock);
  714. return result;
  715. }
  716. int rawfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  717. struct kstat *stat)
  718. {
  719. struct inode *inode = dentry->d_inode;
  720. generic_fillattr(inode, stat);
  721. return 0;
  722. }
  723. struct inode_operations rawfs_file_inode_ops = {
  724. .getattr = rawfs_getattr,
  725. .setattr = rawfs_setattr,
  726. };
  727. struct inode_operations rawfs_dir_inode_ops = {
  728. .create = rawfs_dir_create,
  729. .lookup = rawfs_lookup,
  730. .unlink = rawfs_dir_unlink, /* simple_unlink, */
  731. .mkdir = rawfs_mkdir,
  732. .rmdir = rawfs_rmdir,
  733. .mknod = rawfs_mknod,
  734. .rename = rawfs_dir_rename, /* simple_rename, */
  735. .getattr = rawfs_getattr,
  736. .setattr = rawfs_setattr,
  737. };
  738. int rawfs_fill_inode(struct inode *inode, struct rawfs_file_info *file_info,
  739. int block_no, int page_no, umode_t mode, dev_t dev)
  740. {
  741. struct rawfs_sb_info *sbi = RAWFS_SB(inode->i_sb);
  742. struct rawfs_inode_info *inode_info;
  743. int subfolders;
  744. inode->i_mode = mode;
  745. inode->i_blocks = 0;
  746. inode->i_mapping->a_ops = &rawfs_aops;
  747. inode->i_mapping->backing_dev_info = &rawfs_backing_dev_info;
  748. inode_info = RAWFS_I(inode);
  749. switch (mode & S_IFMT) {
  750. default:
  751. RAWFS_PRINT(RAWFS_DBG_INODE, KERN_INFO
  752. "rawfs_fill_inode: special inode\n");
  753. init_special_inode(inode, mode, dev);
  754. break;
  755. case S_IFREG:
  756. if ((block_no >= 0) && (page_no < 0)) { /* Speical block file */
  757. RAWFS_PRINT(RAWFS_DBG_INODE,
  758. "rawfs_fill_inode: block file %d, %lx -> %lx\n",
  759. block_no, (unsigned long)inode,
  760. (unsigned long)inode_info);
  761. rawfs_fill_inode_blk(inode, file_info, block_no, page_no);
  762. inode->i_fop = &rawfs_block_file_operations;
  763. } else if (file_info != NULL) { /* Regular file */
  764. rawfs_fill_inode_reg(inode, file_info, block_no, page_no);
  765. inode->i_fop = &rawfs_file_operations;
  766. inode->i_blocks = CEILING((unsigned)inode->i_size,
  767. sbi->page_data_size) * sbi->page_size >> 9;
  768. inode->i_size = file_info->i_size;
  769. RAWFS_PRINT(RAWFS_DBG_INODE,
  770. "rawfs_fill_inode: reg file inode %s, size %lld bytes, %d blocks, i_blkbits = %d\n",
  771. file_info->i_name, inode->i_size,
  772. (unsigned)inode->i_blocks, inode->i_blkbits);
  773. }
  774. inode->i_op = &rawfs_file_inode_ops;
  775. break;
  776. case S_IFDIR:
  777. inode->i_op = &rawfs_dir_inode_ops;
  778. inode->i_fop = &rawfs_dir_operations;
  779. /* 2 for initial ".." and "." entries */
  780. subfolders = rawfs_subdirs(inode) + 2;
  781. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_fill_inode: folder, nlink=%d\n",
  782. subfolders);
  783. set_nlink(inode, subfolders);
  784. if (file_info != NULL) { /* folder */
  785. RAWFS_PRINT(RAWFS_DBG_INODE,
  786. "rawfs_fill_inode: folder inode %s\n", file_info->i_name);
  787. inode->i_blocks = sbi->sectors_per_page;
  788. rawfs_fill_inode_reg(inode, file_info, block_no, page_no);
  789. inode->i_size = 0;
  790. } else { /* Super inode */
  791. RAWFS_PRINT(RAWFS_DBG_INODE, "rawfs_fill_inode: super inode\n");
  792. inode->i_ino = RAWFS_ROOT_INO;
  793. RAWFS_I(inode)->i_id = RAWFS_ROOT_DIR_ID;
  794. }
  795. break;
  796. }
  797. return 0;
  798. }
  799. EXPORT_SYMBOL_GPL(rawfs_fill_inode);
  800. MODULE_AUTHOR("Perry Hsu <perry.hsu@mediatek.com>");
  801. MODULE_DESCRIPTION("RAW file system for NAND flash");
  802. MODULE_LICENSE("GPL");