inode.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. /*
  2. * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/crypto.h>
  18. #include <linux/fs.h>
  19. #include <linux/jhash.h>
  20. #include <linux/hash.h>
  21. #include <linux/ktime.h>
  22. #include <linux/mm.h>
  23. #include <linux/mount.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/pagevec.h>
  26. #include <linux/parser.h>
  27. #include <linux/swap.h>
  28. #include <linux/slab.h>
  29. #include <linux/statfs.h>
  30. #include <linux/writeback.h>
  31. #include <linux/prefetch.h>
  32. #include "netfs.h"
  33. #define POHMELFS_MAGIC_NUM 0x504f482e
  34. static struct kmem_cache *pohmelfs_inode_cache;
  35. static atomic_t psb_bdi_num = ATOMIC_INIT(0);
  36. /*
  37. * Removes inode from all trees, drops local name cache and removes all queued
  38. * requests for object removal.
  39. */
  40. void pohmelfs_inode_del_inode(struct pohmelfs_sb *psb, struct pohmelfs_inode *pi)
  41. {
  42. mutex_lock(&pi->offset_lock);
  43. pohmelfs_free_names(pi);
  44. mutex_unlock(&pi->offset_lock);
  45. dprintk("%s: deleted stuff in ino: %llu.\n", __func__, pi->ino);
  46. }
  47. /*
  48. * Sync inode to server.
  49. * Returns zero in success and negative error value otherwise.
  50. * It will gather path to root directory into structures containing
  51. * creation mode, permissions and names, so that the whole path
  52. * to given inode could be created using only single network command.
  53. */
  54. int pohmelfs_write_inode_create(struct inode *inode, struct netfs_trans *trans)
  55. {
  56. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  57. int err = -ENOMEM, size;
  58. struct netfs_cmd *cmd;
  59. void *data;
  60. int cur_len = netfs_trans_cur_len(trans);
  61. if (unlikely(cur_len < 0))
  62. return -ETOOSMALL;
  63. cmd = netfs_trans_current(trans);
  64. cur_len -= sizeof(struct netfs_cmd);
  65. data = (void *)(cmd + 1);
  66. err = pohmelfs_construct_path_string(pi, data, cur_len);
  67. if (err < 0)
  68. goto err_out_exit;
  69. size = err;
  70. cmd->start = i_size_read(inode);
  71. cmd->cmd = NETFS_CREATE;
  72. cmd->size = size;
  73. cmd->id = pi->ino;
  74. cmd->ext = inode->i_mode;
  75. netfs_convert_cmd(cmd);
  76. netfs_trans_update(cmd, trans, size);
  77. return 0;
  78. err_out_exit:
  79. printk("%s: completed ino: %llu, err: %d.\n", __func__, pi->ino, err);
  80. return err;
  81. }
  82. static int pohmelfs_write_trans_complete(struct page **pages, unsigned int page_num,
  83. void *private, int err)
  84. {
  85. unsigned i;
  86. dprintk("%s: pages: %lu-%lu, page_num: %u, err: %d.\n",
  87. __func__, pages[0]->index, pages[page_num-1]->index,
  88. page_num, err);
  89. for (i = 0; i < page_num; i++) {
  90. struct page *page = pages[i];
  91. if (!page)
  92. continue;
  93. end_page_writeback(page);
  94. if (err < 0) {
  95. SetPageError(page);
  96. set_page_dirty(page);
  97. }
  98. unlock_page(page);
  99. page_cache_release(page);
  100. /* dprintk("%s: %3u/%u: page: %p.\n", __func__, i, page_num, page); */
  101. }
  102. return err;
  103. }
  104. static int pohmelfs_inode_has_dirty_pages(struct address_space *mapping, pgoff_t index)
  105. {
  106. int ret;
  107. struct page *page;
  108. rcu_read_lock();
  109. ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
  110. (void **)&page, index, 1, PAGECACHE_TAG_DIRTY);
  111. rcu_read_unlock();
  112. return ret;
  113. }
  114. static int pohmelfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  115. {
  116. struct inode *inode = mapping->host;
  117. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  118. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  119. int err = 0;
  120. int done = 0;
  121. int nr_pages;
  122. pgoff_t index;
  123. pgoff_t end; /* Inclusive */
  124. int scanned = 0;
  125. int range_whole = 0;
  126. if (wbc->range_cyclic) {
  127. index = mapping->writeback_index; /* Start from prev offset */
  128. end = -1;
  129. } else {
  130. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  131. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  132. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  133. range_whole = 1;
  134. scanned = 1;
  135. }
  136. retry:
  137. while (!done && (index <= end)) {
  138. unsigned int i = min(end - index, (pgoff_t)psb->trans_max_pages);
  139. int path_len;
  140. struct netfs_trans *trans;
  141. err = pohmelfs_inode_has_dirty_pages(mapping, index);
  142. if (!err)
  143. break;
  144. err = pohmelfs_path_length(pi);
  145. if (err < 0)
  146. break;
  147. path_len = err;
  148. if (path_len <= 2) {
  149. err = -ENOENT;
  150. break;
  151. }
  152. trans = netfs_trans_alloc(psb, path_len, 0, i);
  153. if (!trans) {
  154. err = -ENOMEM;
  155. break;
  156. }
  157. trans->complete = &pohmelfs_write_trans_complete;
  158. trans->page_num = nr_pages = find_get_pages_tag(mapping, &index,
  159. PAGECACHE_TAG_DIRTY, trans->page_num,
  160. trans->pages);
  161. dprintk("%s: t: %p, nr_pages: %u, end: %lu, index: %lu, max: %u.\n",
  162. __func__, trans, nr_pages, end, index, trans->page_num);
  163. if (!nr_pages)
  164. goto err_out_reset;
  165. err = pohmelfs_write_inode_create(inode, trans);
  166. if (err)
  167. goto err_out_reset;
  168. err = 0;
  169. scanned = 1;
  170. for (i = 0; i < trans->page_num; i++) {
  171. struct page *page = trans->pages[i];
  172. lock_page(page);
  173. if (unlikely(page->mapping != mapping))
  174. goto out_continue;
  175. if (!wbc->range_cyclic && page->index > end) {
  176. done = 1;
  177. goto out_continue;
  178. }
  179. if (wbc->sync_mode != WB_SYNC_NONE)
  180. wait_on_page_writeback(page);
  181. if (PageWriteback(page) ||
  182. !clear_page_dirty_for_io(page)) {
  183. dprintk("%s: not clear for io page: %p, writeback: %d.\n",
  184. __func__, page, PageWriteback(page));
  185. goto out_continue;
  186. }
  187. set_page_writeback(page);
  188. trans->attached_size += page_private(page);
  189. trans->attached_pages++;
  190. #if 0
  191. dprintk("%s: %u/%u added trans: %p, gen: %u, page: %p, [High: %d], size: %lu, idx: %lu.\n",
  192. __func__, i, trans->page_num, trans, trans->gen, page,
  193. !!PageHighMem(page), page_private(page), page->index);
  194. #endif
  195. wbc->nr_to_write--;
  196. if (wbc->nr_to_write <= 0)
  197. done = 1;
  198. continue;
  199. out_continue:
  200. unlock_page(page);
  201. trans->pages[i] = NULL;
  202. }
  203. err = netfs_trans_finish(trans, psb);
  204. if (err)
  205. break;
  206. continue;
  207. err_out_reset:
  208. trans->result = err;
  209. netfs_trans_reset(trans);
  210. netfs_trans_put(trans);
  211. break;
  212. }
  213. if (!scanned && !done) {
  214. /*
  215. * We hit the last page and there is more work to be done: wrap
  216. * back to the start of the file
  217. */
  218. scanned = 1;
  219. index = 0;
  220. goto retry;
  221. }
  222. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  223. mapping->writeback_index = index;
  224. return err;
  225. }
  226. /*
  227. * Inode writeback creation completion callback.
  228. * Only invoked for just created inodes, which do not have pages attached,
  229. * like dirs and empty files.
  230. */
  231. static int pohmelfs_write_inode_complete(struct page **pages, unsigned int page_num,
  232. void *private, int err)
  233. {
  234. struct inode *inode = private;
  235. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  236. if (inode) {
  237. if (err) {
  238. mark_inode_dirty(inode);
  239. clear_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state);
  240. } else {
  241. set_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state);
  242. }
  243. pohmelfs_put_inode(pi);
  244. }
  245. return err;
  246. }
  247. int pohmelfs_write_create_inode(struct pohmelfs_inode *pi)
  248. {
  249. struct netfs_trans *t;
  250. struct inode *inode = &pi->vfs_inode;
  251. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  252. int err;
  253. if (test_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state))
  254. return 0;
  255. dprintk("%s: started ino: %llu.\n", __func__, pi->ino);
  256. err = pohmelfs_path_length(pi);
  257. if (err < 0)
  258. goto err_out_exit;
  259. t = netfs_trans_alloc(psb, err + 1, 0, 0);
  260. if (!t) {
  261. err = -ENOMEM;
  262. goto err_out_exit;
  263. }
  264. t->complete = pohmelfs_write_inode_complete;
  265. t->private = igrab(inode);
  266. if (!t->private) {
  267. err = -ENOENT;
  268. goto err_out_put;
  269. }
  270. err = pohmelfs_write_inode_create(inode, t);
  271. if (err)
  272. goto err_out_put;
  273. netfs_trans_finish(t, POHMELFS_SB(inode->i_sb));
  274. return 0;
  275. err_out_put:
  276. t->result = err;
  277. netfs_trans_put(t);
  278. err_out_exit:
  279. return err;
  280. }
  281. /*
  282. * Sync all not-yet-created children in given directory to the server.
  283. */
  284. static int pohmelfs_write_inode_create_children(struct inode *inode)
  285. {
  286. struct pohmelfs_inode *parent = POHMELFS_I(inode);
  287. struct super_block *sb = inode->i_sb;
  288. struct pohmelfs_name *n;
  289. while (!list_empty(&parent->sync_create_list)) {
  290. n = NULL;
  291. mutex_lock(&parent->offset_lock);
  292. if (!list_empty(&parent->sync_create_list)) {
  293. n = list_first_entry(&parent->sync_create_list,
  294. struct pohmelfs_name, sync_create_entry);
  295. list_del_init(&n->sync_create_entry);
  296. }
  297. mutex_unlock(&parent->offset_lock);
  298. if (!n)
  299. break;
  300. inode = ilookup(sb, n->ino);
  301. dprintk("%s: parent: %llu, ino: %llu, inode: %p.\n",
  302. __func__, parent->ino, n->ino, inode);
  303. if (inode && (inode->i_state & I_DIRTY)) {
  304. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  305. pohmelfs_write_create_inode(pi);
  306. /* pohmelfs_meta_command(pi, NETFS_INODE_INFO, 0, NULL, NULL, 0); */
  307. iput(inode);
  308. }
  309. }
  310. return 0;
  311. }
  312. /*
  313. * Removes given child from given inode on server.
  314. */
  315. int pohmelfs_remove_child(struct pohmelfs_inode *pi, struct pohmelfs_name *n)
  316. {
  317. return pohmelfs_meta_command_data(pi, pi->ino, NETFS_REMOVE, NULL, 0, NULL, NULL, 0);
  318. }
  319. /*
  320. * Writeback for given inode.
  321. */
  322. static int pohmelfs_write_inode(struct inode *inode,
  323. struct writeback_control *wbc)
  324. {
  325. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  326. pohmelfs_write_create_inode(pi);
  327. pohmelfs_write_inode_create_children(inode);
  328. return 0;
  329. }
  330. /*
  331. * It is not exported, sorry...
  332. */
  333. static inline wait_queue_head_t *page_waitqueue(struct page *page)
  334. {
  335. const struct zone *zone = page_zone(page);
  336. return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
  337. }
  338. static int pohmelfs_wait_on_page_locked(struct page *page)
  339. {
  340. struct pohmelfs_sb *psb = POHMELFS_SB(page->mapping->host->i_sb);
  341. long ret = psb->wait_on_page_timeout;
  342. DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
  343. int err = 0;
  344. if (!PageLocked(page))
  345. return 0;
  346. for (;;) {
  347. prepare_to_wait(page_waitqueue(page),
  348. &wait.wait, TASK_INTERRUPTIBLE);
  349. dprintk("%s: page: %p, locked: %d, uptodate: %d, error: %d, flags: %lx.\n",
  350. __func__, page, PageLocked(page), PageUptodate(page),
  351. PageError(page), page->flags);
  352. if (!PageLocked(page))
  353. break;
  354. if (!signal_pending(current)) {
  355. ret = schedule_timeout(ret);
  356. if (!ret)
  357. break;
  358. continue;
  359. }
  360. ret = -ERESTARTSYS;
  361. break;
  362. }
  363. finish_wait(page_waitqueue(page), &wait.wait);
  364. if (!ret)
  365. err = -ETIMEDOUT;
  366. if (!err)
  367. SetPageUptodate(page);
  368. if (err)
  369. printk("%s: page: %p, uptodate: %d, locked: %d, err: %d.\n",
  370. __func__, page, PageUptodate(page), PageLocked(page), err);
  371. return err;
  372. }
  373. static int pohmelfs_read_page_complete(struct page **pages, unsigned int page_num,
  374. void *private, int err)
  375. {
  376. struct page *page = private;
  377. if (PageChecked(page))
  378. return err;
  379. if (err < 0) {
  380. dprintk("%s: page: %p, err: %d.\n", __func__, page, err);
  381. SetPageError(page);
  382. }
  383. unlock_page(page);
  384. return err;
  385. }
  386. /*
  387. * Read a page from remote server.
  388. * Function will wait until page is unlocked.
  389. */
  390. static int pohmelfs_readpage(struct file *file, struct page *page)
  391. {
  392. struct inode *inode = page->mapping->host;
  393. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  394. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  395. struct netfs_trans *t;
  396. struct netfs_cmd *cmd;
  397. int err, path_len;
  398. void *data;
  399. u64 isize;
  400. err = pohmelfs_data_lock(pi, page->index << PAGE_CACHE_SHIFT,
  401. PAGE_SIZE, POHMELFS_READ_LOCK);
  402. if (err)
  403. goto err_out_exit;
  404. isize = i_size_read(inode);
  405. if (isize <= page->index << PAGE_CACHE_SHIFT) {
  406. SetPageUptodate(page);
  407. unlock_page(page);
  408. return 0;
  409. }
  410. path_len = pohmelfs_path_length(pi);
  411. if (path_len < 0) {
  412. err = path_len;
  413. goto err_out_exit;
  414. }
  415. t = netfs_trans_alloc(psb, path_len, NETFS_TRANS_SINGLE_DST, 0);
  416. if (!t) {
  417. err = -ENOMEM;
  418. goto err_out_exit;
  419. }
  420. t->complete = pohmelfs_read_page_complete;
  421. t->private = page;
  422. cmd = netfs_trans_current(t);
  423. data = (void *)(cmd + 1);
  424. err = pohmelfs_construct_path_string(pi, data, path_len);
  425. if (err < 0)
  426. goto err_out_free;
  427. path_len = err;
  428. cmd->id = pi->ino;
  429. cmd->start = page->index;
  430. cmd->start <<= PAGE_CACHE_SHIFT;
  431. cmd->size = PAGE_CACHE_SIZE + path_len;
  432. cmd->cmd = NETFS_READ_PAGE;
  433. cmd->ext = path_len;
  434. dprintk("%s: path: '%s', page: %p, ino: %llu, start: %llu, size: %lu.\n",
  435. __func__, (char *)data, page, pi->ino, cmd->start, PAGE_CACHE_SIZE);
  436. netfs_convert_cmd(cmd);
  437. netfs_trans_update(cmd, t, path_len);
  438. err = netfs_trans_finish(t, psb);
  439. if (err)
  440. goto err_out_return;
  441. return pohmelfs_wait_on_page_locked(page);
  442. err_out_free:
  443. t->result = err;
  444. netfs_trans_put(t);
  445. err_out_exit:
  446. SetPageError(page);
  447. if (PageLocked(page))
  448. unlock_page(page);
  449. err_out_return:
  450. printk("%s: page: %p, start: %lu, size: %lu, err: %d.\n",
  451. __func__, page, page->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE, err);
  452. return err;
  453. }
  454. /*
  455. * Write begin/end magic.
  456. * Allocates a page and writes inode if it was not synced to server before.
  457. */
  458. static int pohmelfs_write_begin(struct file *file, struct address_space *mapping,
  459. loff_t pos, unsigned len, unsigned flags,
  460. struct page **pagep, void **fsdata)
  461. {
  462. struct inode *inode = mapping->host;
  463. struct page *page;
  464. pgoff_t index;
  465. unsigned start, end;
  466. int err;
  467. *pagep = NULL;
  468. index = pos >> PAGE_CACHE_SHIFT;
  469. start = pos & (PAGE_CACHE_SIZE - 1);
  470. end = start + len;
  471. page = grab_cache_page(mapping, index);
  472. #if 0
  473. dprintk("%s: page: %p pos: %llu, len: %u, index: %lu, start: %u, end: %u, uptodate: %d.\n",
  474. __func__, page, pos, len, index, start, end, PageUptodate(page));
  475. #endif
  476. if (!page) {
  477. err = -ENOMEM;
  478. goto err_out_exit;
  479. }
  480. while (!PageUptodate(page)) {
  481. if (start && test_bit(NETFS_INODE_REMOTE_SYNCED, &POHMELFS_I(inode)->state)) {
  482. err = pohmelfs_readpage(file, page);
  483. if (err)
  484. goto err_out_exit;
  485. lock_page(page);
  486. continue;
  487. }
  488. if (len != PAGE_CACHE_SIZE) {
  489. void *kaddr = kmap_atomic(page, KM_USER0);
  490. memset(kaddr + start, 0, PAGE_CACHE_SIZE - start);
  491. flush_dcache_page(page);
  492. kunmap_atomic(kaddr, KM_USER0);
  493. }
  494. SetPageUptodate(page);
  495. }
  496. set_page_private(page, end);
  497. *pagep = page;
  498. return 0;
  499. err_out_exit:
  500. page_cache_release(page);
  501. *pagep = NULL;
  502. return err;
  503. }
  504. static int pohmelfs_write_end(struct file *file, struct address_space *mapping,
  505. loff_t pos, unsigned len, unsigned copied,
  506. struct page *page, void *fsdata)
  507. {
  508. struct inode *inode = mapping->host;
  509. if (copied != len) {
  510. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  511. void *kaddr = kmap_atomic(page, KM_USER0);
  512. memset(kaddr + from + copied, 0, len - copied);
  513. flush_dcache_page(page);
  514. kunmap_atomic(kaddr, KM_USER0);
  515. }
  516. SetPageUptodate(page);
  517. set_page_dirty(page);
  518. #if 0
  519. dprintk("%s: page: %p [U: %d, D: %d, L: %d], pos: %llu, len: %u, copied: %u.\n",
  520. __func__, page,
  521. PageUptodate(page), PageDirty(page), PageLocked(page),
  522. pos, len, copied);
  523. #endif
  524. flush_dcache_page(page);
  525. unlock_page(page);
  526. page_cache_release(page);
  527. if (pos + copied > inode->i_size) {
  528. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  529. psb->avail_size -= pos + copied - inode->i_size;
  530. i_size_write(inode, pos + copied);
  531. }
  532. return copied;
  533. }
  534. static int pohmelfs_readpages_trans_complete(struct page **__pages, unsigned int page_num,
  535. void *private, int err)
  536. {
  537. struct pohmelfs_inode *pi = private;
  538. unsigned int i, num;
  539. struct page **pages, *page = (struct page *)__pages;
  540. loff_t index = page->index;
  541. pages = kzalloc(sizeof(void *) * page_num, GFP_NOIO);
  542. if (!pages)
  543. return -ENOMEM;
  544. num = find_get_pages_contig(pi->vfs_inode.i_mapping, index, page_num, pages);
  545. if (num <= 0) {
  546. err = num;
  547. goto err_out_free;
  548. }
  549. for (i = 0; i < num; ++i) {
  550. page = pages[i];
  551. if (err)
  552. printk("%s: %u/%u: page: %p, index: %lu, uptodate: %d, locked: %d, err: %d.\n",
  553. __func__, i, num, page, page->index,
  554. PageUptodate(page), PageLocked(page), err);
  555. if (!PageChecked(page)) {
  556. if (err < 0)
  557. SetPageError(page);
  558. unlock_page(page);
  559. }
  560. page_cache_release(page);
  561. page_cache_release(page);
  562. }
  563. err_out_free:
  564. kfree(pages);
  565. return err;
  566. }
  567. static int pohmelfs_send_readpages(struct pohmelfs_inode *pi, struct page *first, unsigned int num)
  568. {
  569. struct netfs_trans *t;
  570. struct netfs_cmd *cmd;
  571. struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb);
  572. int err, path_len;
  573. void *data;
  574. err = pohmelfs_data_lock(pi, first->index << PAGE_CACHE_SHIFT,
  575. num * PAGE_SIZE, POHMELFS_READ_LOCK);
  576. if (err)
  577. goto err_out_exit;
  578. path_len = pohmelfs_path_length(pi);
  579. if (path_len < 0) {
  580. err = path_len;
  581. goto err_out_exit;
  582. }
  583. t = netfs_trans_alloc(psb, path_len, NETFS_TRANS_SINGLE_DST, 0);
  584. if (!t) {
  585. err = -ENOMEM;
  586. goto err_out_exit;
  587. }
  588. cmd = netfs_trans_current(t);
  589. data = (void *)(cmd + 1);
  590. t->complete = pohmelfs_readpages_trans_complete;
  591. t->private = pi;
  592. t->page_num = num;
  593. t->pages = (struct page **)first;
  594. err = pohmelfs_construct_path_string(pi, data, path_len);
  595. if (err < 0)
  596. goto err_out_put;
  597. path_len = err;
  598. cmd->cmd = NETFS_READ_PAGES;
  599. cmd->start = first->index;
  600. cmd->start <<= PAGE_CACHE_SHIFT;
  601. cmd->size = (num << 8 | PAGE_CACHE_SHIFT);
  602. cmd->id = pi->ino;
  603. cmd->ext = path_len;
  604. dprintk("%s: t: %p, gen: %u, path: '%s', path_len: %u, "
  605. "start: %lu, num: %u.\n",
  606. __func__, t, t->gen, (char *)data, path_len,
  607. first->index, num);
  608. netfs_convert_cmd(cmd);
  609. netfs_trans_update(cmd, t, path_len);
  610. return netfs_trans_finish(t, psb);
  611. err_out_put:
  612. netfs_trans_free(t);
  613. err_out_exit:
  614. pohmelfs_readpages_trans_complete((struct page **)first, num, pi, err);
  615. return err;
  616. }
  617. #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
  618. static int pohmelfs_readpages(struct file *file, struct address_space *mapping,
  619. struct list_head *pages, unsigned nr_pages)
  620. {
  621. unsigned int page_idx, num = 0;
  622. struct page *page = NULL, *first = NULL;
  623. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  624. page = list_to_page(pages);
  625. prefetchw(&page->flags);
  626. list_del(&page->lru);
  627. if (!add_to_page_cache_lru(page, mapping,
  628. page->index, GFP_KERNEL)) {
  629. if (!num) {
  630. num = 1;
  631. first = page;
  632. continue;
  633. }
  634. dprintk("%s: added to lru page: %p, page_index: %lu, first_index: %lu.\n",
  635. __func__, page, page->index, first->index);
  636. if (unlikely(first->index + num != page->index) || (num > 500)) {
  637. pohmelfs_send_readpages(POHMELFS_I(mapping->host),
  638. first, num);
  639. first = page;
  640. num = 0;
  641. }
  642. num++;
  643. }
  644. }
  645. pohmelfs_send_readpages(POHMELFS_I(mapping->host), first, num);
  646. /*
  647. * This will be sync read, so when last page is processed,
  648. * all previous are alerady unlocked and ready to be used.
  649. */
  650. return 0;
  651. }
  652. /*
  653. * Small address space operations for POHMELFS.
  654. */
  655. const struct address_space_operations pohmelfs_aops = {
  656. .readpage = pohmelfs_readpage,
  657. .readpages = pohmelfs_readpages,
  658. .writepages = pohmelfs_writepages,
  659. .write_begin = pohmelfs_write_begin,
  660. .write_end = pohmelfs_write_end,
  661. .set_page_dirty = __set_page_dirty_nobuffers,
  662. };
  663. static void pohmelfs_i_callback(struct rcu_head *head)
  664. {
  665. struct inode *inode = container_of(head, struct inode, i_rcu);
  666. INIT_LIST_HEAD(&inode->i_dentry);
  667. kmem_cache_free(pohmelfs_inode_cache, POHMELFS_I(inode));
  668. }
  669. /*
  670. * ->destroy_inode() callback. Deletes inode from the caches
  671. * and frees private data.
  672. */
  673. static void pohmelfs_destroy_inode(struct inode *inode)
  674. {
  675. struct super_block *sb = inode->i_sb;
  676. struct pohmelfs_sb *psb = POHMELFS_SB(sb);
  677. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  678. /* pohmelfs_data_unlock(pi, 0, inode->i_size, POHMELFS_READ_LOCK); */
  679. pohmelfs_inode_del_inode(psb, pi);
  680. dprintk("%s: pi: %p, inode: %p, ino: %llu.\n",
  681. __func__, pi, &pi->vfs_inode, pi->ino);
  682. atomic_long_dec(&psb->total_inodes);
  683. call_rcu(&inode->i_rcu, pohmelfs_i_callback);
  684. }
  685. /*
  686. * ->alloc_inode() callback. Allocates inode and initializes private data.
  687. */
  688. static struct inode *pohmelfs_alloc_inode(struct super_block *sb)
  689. {
  690. struct pohmelfs_inode *pi;
  691. pi = kmem_cache_alloc(pohmelfs_inode_cache, GFP_NOIO);
  692. if (!pi)
  693. return NULL;
  694. pi->hash_root = RB_ROOT;
  695. mutex_init(&pi->offset_lock);
  696. INIT_LIST_HEAD(&pi->sync_create_list);
  697. INIT_LIST_HEAD(&pi->inode_entry);
  698. pi->lock_type = 0;
  699. pi->state = 0;
  700. pi->total_len = 0;
  701. pi->drop_count = 0;
  702. dprintk("%s: pi: %p, inode: %p.\n", __func__, pi, &pi->vfs_inode);
  703. atomic_long_inc(&POHMELFS_SB(sb)->total_inodes);
  704. return &pi->vfs_inode;
  705. }
  706. /*
  707. * We want fsync() to work on POHMELFS.
  708. */
  709. static int pohmelfs_fsync(struct file *file, int datasync)
  710. {
  711. struct inode *inode = file->f_mapping->host;
  712. return sync_inode_metadata(inode, 1);
  713. }
  714. ssize_t pohmelfs_write(struct file *file, const char __user *buf,
  715. size_t len, loff_t *ppos)
  716. {
  717. struct address_space *mapping = file->f_mapping;
  718. struct inode *inode = mapping->host;
  719. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  720. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  721. struct kiocb kiocb;
  722. ssize_t ret;
  723. loff_t pos = *ppos;
  724. init_sync_kiocb(&kiocb, file);
  725. kiocb.ki_pos = pos;
  726. kiocb.ki_left = len;
  727. dprintk("%s: len: %zu, pos: %llu.\n", __func__, len, pos);
  728. mutex_lock(&inode->i_mutex);
  729. ret = pohmelfs_data_lock(pi, pos, len, POHMELFS_WRITE_LOCK);
  730. if (ret)
  731. goto err_out_unlock;
  732. ret = __generic_file_aio_write(&kiocb, &iov, 1, &kiocb.ki_pos);
  733. *ppos = kiocb.ki_pos;
  734. mutex_unlock(&inode->i_mutex);
  735. WARN_ON(ret < 0);
  736. if (ret > 0) {
  737. ssize_t err;
  738. err = generic_write_sync(file, pos, ret);
  739. if (err < 0)
  740. ret = err;
  741. WARN_ON(ret < 0);
  742. }
  743. return ret;
  744. err_out_unlock:
  745. mutex_unlock(&inode->i_mutex);
  746. return ret;
  747. }
  748. static const struct file_operations pohmelfs_file_ops = {
  749. .open = generic_file_open,
  750. .fsync = pohmelfs_fsync,
  751. .llseek = generic_file_llseek,
  752. .read = do_sync_read,
  753. .aio_read = generic_file_aio_read,
  754. .mmap = generic_file_mmap,
  755. .splice_read = generic_file_splice_read,
  756. .splice_write = generic_file_splice_write,
  757. .write = pohmelfs_write,
  758. .aio_write = generic_file_aio_write,
  759. };
  760. const struct inode_operations pohmelfs_symlink_inode_operations = {
  761. .readlink = generic_readlink,
  762. .follow_link = page_follow_link_light,
  763. .put_link = page_put_link,
  764. };
  765. int pohmelfs_setattr_raw(struct inode *inode, struct iattr *attr)
  766. {
  767. int err;
  768. err = inode_change_ok(inode, attr);
  769. if (err) {
  770. dprintk("%s: ino: %llu, inode changes are not allowed.\n", __func__, POHMELFS_I(inode)->ino);
  771. goto err_out_exit;
  772. }
  773. if ((attr->ia_valid & ATTR_SIZE) &&
  774. attr->ia_size != i_size_read(inode)) {
  775. err = vmtruncate(inode, attr->ia_size);
  776. if (err) {
  777. dprintk("%s: ino: %llu, failed to set the attributes.\n", __func__, POHMELFS_I(inode)->ino);
  778. goto err_out_exit;
  779. }
  780. }
  781. setattr_copy(inode, attr);
  782. mark_inode_dirty(inode);
  783. dprintk("%s: ino: %llu, mode: %o -> %o, uid: %u -> %u, gid: %u -> %u, size: %llu -> %llu.\n",
  784. __func__, POHMELFS_I(inode)->ino, inode->i_mode, attr->ia_mode,
  785. inode->i_uid, attr->ia_uid, inode->i_gid, attr->ia_gid, inode->i_size, attr->ia_size);
  786. return 0;
  787. err_out_exit:
  788. return err;
  789. }
  790. int pohmelfs_setattr(struct dentry *dentry, struct iattr *attr)
  791. {
  792. struct inode *inode = dentry->d_inode;
  793. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  794. int err;
  795. err = pohmelfs_data_lock(pi, 0, ~0, POHMELFS_WRITE_LOCK);
  796. if (err)
  797. goto err_out_exit;
  798. err = security_inode_setattr(dentry, attr);
  799. if (err)
  800. goto err_out_exit;
  801. err = pohmelfs_setattr_raw(inode, attr);
  802. if (err)
  803. goto err_out_exit;
  804. return 0;
  805. err_out_exit:
  806. return err;
  807. }
  808. static int pohmelfs_send_xattr_req(struct pohmelfs_inode *pi, u64 id, u64 start,
  809. const char *name, const void *value, size_t attrsize, int command)
  810. {
  811. struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb);
  812. int err, path_len, namelen = strlen(name) + 1; /* 0-byte */
  813. struct netfs_trans *t;
  814. struct netfs_cmd *cmd;
  815. void *data;
  816. dprintk("%s: id: %llu, start: %llu, name: '%s', attrsize: %zu, cmd: %d.\n",
  817. __func__, id, start, name, attrsize, command);
  818. path_len = pohmelfs_path_length(pi);
  819. if (path_len < 0) {
  820. err = path_len;
  821. goto err_out_exit;
  822. }
  823. t = netfs_trans_alloc(psb, namelen + path_len + attrsize, 0, 0);
  824. if (!t) {
  825. err = -ENOMEM;
  826. goto err_out_exit;
  827. }
  828. cmd = netfs_trans_current(t);
  829. data = cmd + 1;
  830. path_len = pohmelfs_construct_path_string(pi, data, path_len);
  831. if (path_len < 0) {
  832. err = path_len;
  833. goto err_out_put;
  834. }
  835. data += path_len;
  836. /*
  837. * 'name' is a NUL-terminated string already and
  838. * 'namelen' includes 0-byte.
  839. */
  840. memcpy(data, name, namelen);
  841. data += namelen;
  842. memcpy(data, value, attrsize);
  843. cmd->cmd = command;
  844. cmd->id = id;
  845. cmd->start = start;
  846. cmd->size = attrsize + namelen + path_len;
  847. cmd->ext = path_len;
  848. cmd->csize = 0;
  849. cmd->cpad = 0;
  850. netfs_convert_cmd(cmd);
  851. netfs_trans_update(cmd, t, namelen + path_len + attrsize);
  852. return netfs_trans_finish(t, psb);
  853. err_out_put:
  854. t->result = err;
  855. netfs_trans_put(t);
  856. err_out_exit:
  857. return err;
  858. }
  859. static int pohmelfs_setxattr(struct dentry *dentry, const char *name,
  860. const void *value, size_t attrsize, int flags)
  861. {
  862. struct inode *inode = dentry->d_inode;
  863. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  864. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  865. if (!(psb->state_flags & POHMELFS_FLAGS_XATTR))
  866. return -EOPNOTSUPP;
  867. return pohmelfs_send_xattr_req(pi, flags, attrsize, name,
  868. value, attrsize, NETFS_XATTR_SET);
  869. }
  870. static ssize_t pohmelfs_getxattr(struct dentry *dentry, const char *name,
  871. void *value, size_t attrsize)
  872. {
  873. struct inode *inode = dentry->d_inode;
  874. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  875. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  876. struct pohmelfs_mcache *m;
  877. int err;
  878. long timeout = psb->mcache_timeout;
  879. if (!(psb->state_flags & POHMELFS_FLAGS_XATTR))
  880. return -EOPNOTSUPP;
  881. m = pohmelfs_mcache_alloc(psb, 0, attrsize, value);
  882. if (IS_ERR(m))
  883. return PTR_ERR(m);
  884. dprintk("%s: ino: %llu, name: '%s', size: %zu.\n",
  885. __func__, pi->ino, name, attrsize);
  886. err = pohmelfs_send_xattr_req(pi, m->gen, attrsize, name, value, 0, NETFS_XATTR_GET);
  887. if (err)
  888. goto err_out_put;
  889. do {
  890. err = wait_for_completion_timeout(&m->complete, timeout);
  891. if (err) {
  892. err = m->err;
  893. break;
  894. }
  895. /*
  896. * This loop is a bit ugly, since it waits until reference counter
  897. * hits 1 and then puts the object here. Main goal is to prevent race with
  898. * the network thread, when it can start processing the given request, i.e.
  899. * increase its reference counter but yet not complete it, while
  900. * we will exit from ->getxattr() with timeout, and although request
  901. * will not be freed (its reference counter was increased by network
  902. * thread), data pointer provided by user may be released, so we will
  903. * overwrite an already freed area in the network thread.
  904. *
  905. * Now after timeout we remove request from the cache, so it can not be
  906. * found by network thread, and wait for its reference counter to hit 1,
  907. * i.e. if network thread already started to process this request, we wait
  908. * for it to finish, and then free object locally. If reference counter is
  909. * already 1, i.e. request is not used by anyone else, we can free it without
  910. * problem.
  911. */
  912. err = -ETIMEDOUT;
  913. timeout = HZ;
  914. pohmelfs_mcache_remove_locked(psb, m);
  915. } while (atomic_read(&m->refcnt) != 1);
  916. pohmelfs_mcache_put(psb, m);
  917. dprintk("%s: ino: %llu, err: %d.\n", __func__, pi->ino, err);
  918. return err;
  919. err_out_put:
  920. pohmelfs_mcache_put(psb, m);
  921. return err;
  922. }
  923. static int pohmelfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  924. {
  925. struct inode *inode = dentry->d_inode;
  926. #if 0
  927. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  928. int err;
  929. err = pohmelfs_data_lock(pi, 0, ~0, POHMELFS_READ_LOCK);
  930. if (err)
  931. return err;
  932. dprintk("%s: ino: %llu, mode: %o, uid: %u, gid: %u, size: %llu.\n",
  933. __func__, pi->ino, inode->i_mode, inode->i_uid,
  934. inode->i_gid, inode->i_size);
  935. #endif
  936. generic_fillattr(inode, stat);
  937. return 0;
  938. }
  939. const struct inode_operations pohmelfs_file_inode_operations = {
  940. .setattr = pohmelfs_setattr,
  941. .getattr = pohmelfs_getattr,
  942. .setxattr = pohmelfs_setxattr,
  943. .getxattr = pohmelfs_getxattr,
  944. };
  945. /*
  946. * Fill inode data: mode, size, operation callbacks and so on...
  947. */
  948. void pohmelfs_fill_inode(struct inode *inode, struct netfs_inode_info *info)
  949. {
  950. inode->i_mode = info->mode;
  951. inode->i_nlink = info->nlink;
  952. inode->i_uid = info->uid;
  953. inode->i_gid = info->gid;
  954. inode->i_blocks = info->blocks;
  955. inode->i_rdev = info->rdev;
  956. inode->i_size = info->size;
  957. inode->i_version = info->version;
  958. inode->i_blkbits = ffs(info->blocksize);
  959. dprintk("%s: inode: %p, num: %lu/%llu inode is regular: %d, dir: %d, link: %d, mode: %o, size: %llu.\n",
  960. __func__, inode, inode->i_ino, info->ino,
  961. S_ISREG(inode->i_mode), S_ISDIR(inode->i_mode),
  962. S_ISLNK(inode->i_mode), inode->i_mode, inode->i_size);
  963. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  964. /*
  965. * i_mapping is a pointer to i_data during inode initialization.
  966. */
  967. inode->i_data.a_ops = &pohmelfs_aops;
  968. if (S_ISREG(inode->i_mode)) {
  969. inode->i_fop = &pohmelfs_file_ops;
  970. inode->i_op = &pohmelfs_file_inode_operations;
  971. } else if (S_ISDIR(inode->i_mode)) {
  972. inode->i_fop = &pohmelfs_dir_fops;
  973. inode->i_op = &pohmelfs_dir_inode_ops;
  974. } else if (S_ISLNK(inode->i_mode)) {
  975. inode->i_op = &pohmelfs_symlink_inode_operations;
  976. inode->i_fop = &pohmelfs_file_ops;
  977. } else {
  978. inode->i_fop = &generic_ro_fops;
  979. }
  980. }
  981. static int pohmelfs_drop_inode(struct inode *inode)
  982. {
  983. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  984. struct pohmelfs_inode *pi = POHMELFS_I(inode);
  985. spin_lock(&psb->ino_lock);
  986. list_del_init(&pi->inode_entry);
  987. spin_unlock(&psb->ino_lock);
  988. return generic_drop_inode(inode);
  989. }
  990. static struct pohmelfs_inode *pohmelfs_get_inode_from_list(struct pohmelfs_sb *psb,
  991. struct list_head *head, unsigned int *count)
  992. {
  993. struct pohmelfs_inode *pi = NULL;
  994. spin_lock(&psb->ino_lock);
  995. if (!list_empty(head)) {
  996. pi = list_entry(head->next, struct pohmelfs_inode,
  997. inode_entry);
  998. list_del_init(&pi->inode_entry);
  999. *count = pi->drop_count;
  1000. pi->drop_count = 0;
  1001. }
  1002. spin_unlock(&psb->ino_lock);
  1003. return pi;
  1004. }
  1005. static void pohmelfs_flush_transactions(struct pohmelfs_sb *psb)
  1006. {
  1007. struct pohmelfs_config *c;
  1008. mutex_lock(&psb->state_lock);
  1009. list_for_each_entry(c, &psb->state_list, config_entry) {
  1010. pohmelfs_state_flush_transactions(&c->state);
  1011. }
  1012. mutex_unlock(&psb->state_lock);
  1013. }
  1014. /*
  1015. * ->put_super() callback. Invoked before superblock is destroyed,
  1016. * so it has to clean all private data.
  1017. */
  1018. static void pohmelfs_put_super(struct super_block *sb)
  1019. {
  1020. struct pohmelfs_sb *psb = POHMELFS_SB(sb);
  1021. struct pohmelfs_inode *pi;
  1022. unsigned int count = 0;
  1023. unsigned int in_drop_list = 0;
  1024. struct inode *inode, *tmp;
  1025. dprintk("%s.\n", __func__);
  1026. /*
  1027. * Kill pending transactions, which could affect inodes in-flight.
  1028. */
  1029. pohmelfs_flush_transactions(psb);
  1030. while ((pi = pohmelfs_get_inode_from_list(psb, &psb->drop_list, &count))) {
  1031. inode = &pi->vfs_inode;
  1032. dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n",
  1033. __func__, pi->ino, pi, inode, count);
  1034. if (atomic_read(&inode->i_count) != count) {
  1035. printk("%s: ino: %llu, pi: %p, inode: %p, count: %u, i_count: %d.\n",
  1036. __func__, pi->ino, pi, inode, count,
  1037. atomic_read(&inode->i_count));
  1038. count = atomic_read(&inode->i_count);
  1039. in_drop_list++;
  1040. }
  1041. while (count--)
  1042. iput(&pi->vfs_inode);
  1043. }
  1044. list_for_each_entry_safe(inode, tmp, &sb->s_inodes, i_sb_list) {
  1045. pi = POHMELFS_I(inode);
  1046. dprintk("%s: ino: %llu, pi: %p, inode: %p, i_count: %u.\n",
  1047. __func__, pi->ino, pi, inode, atomic_read(&inode->i_count));
  1048. /*
  1049. * These are special inodes, they were created during
  1050. * directory reading or lookup, and were not bound to dentry,
  1051. * so they live here with reference counter being 1 and prevent
  1052. * umount from succeed since it believes that they are busy.
  1053. */
  1054. count = atomic_read(&inode->i_count);
  1055. if (count) {
  1056. list_del_init(&inode->i_sb_list);
  1057. while (count--)
  1058. iput(&pi->vfs_inode);
  1059. }
  1060. }
  1061. psb->trans_scan_timeout = psb->drop_scan_timeout = 0;
  1062. cancel_delayed_work_sync(&psb->dwork);
  1063. cancel_delayed_work_sync(&psb->drop_dwork);
  1064. flush_scheduled_work();
  1065. dprintk("%s: stopped workqueues.\n", __func__);
  1066. pohmelfs_crypto_exit(psb);
  1067. pohmelfs_state_exit(psb);
  1068. bdi_destroy(&psb->bdi);
  1069. kfree(psb);
  1070. sb->s_fs_info = NULL;
  1071. }
  1072. static int pohmelfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1073. {
  1074. struct super_block *sb = dentry->d_sb;
  1075. struct pohmelfs_sb *psb = POHMELFS_SB(sb);
  1076. /*
  1077. * There are no filesystem size limits yet.
  1078. */
  1079. memset(buf, 0, sizeof(struct kstatfs));
  1080. buf->f_type = POHMELFS_MAGIC_NUM; /* 'POH.' */
  1081. buf->f_bsize = sb->s_blocksize;
  1082. buf->f_files = psb->ino;
  1083. buf->f_namelen = 255;
  1084. buf->f_files = atomic_long_read(&psb->total_inodes);
  1085. buf->f_bfree = buf->f_bavail = psb->avail_size >> PAGE_SHIFT;
  1086. buf->f_blocks = psb->total_size >> PAGE_SHIFT;
  1087. dprintk("%s: total: %llu, avail: %llu, inodes: %llu, bsize: %lu.\n",
  1088. __func__, psb->total_size, psb->avail_size, buf->f_files, sb->s_blocksize);
  1089. return 0;
  1090. }
  1091. static int pohmelfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  1092. {
  1093. struct pohmelfs_sb *psb = POHMELFS_SB(vfs->mnt_sb);
  1094. seq_printf(seq, ",idx=%u", psb->idx);
  1095. seq_printf(seq, ",trans_scan_timeout=%u", jiffies_to_msecs(psb->trans_scan_timeout));
  1096. seq_printf(seq, ",drop_scan_timeout=%u", jiffies_to_msecs(psb->drop_scan_timeout));
  1097. seq_printf(seq, ",wait_on_page_timeout=%u", jiffies_to_msecs(psb->wait_on_page_timeout));
  1098. seq_printf(seq, ",trans_retries=%u", psb->trans_retries);
  1099. seq_printf(seq, ",crypto_thread_num=%u", psb->crypto_thread_num);
  1100. seq_printf(seq, ",trans_max_pages=%u", psb->trans_max_pages);
  1101. seq_printf(seq, ",mcache_timeout=%u", jiffies_to_msecs(psb->mcache_timeout));
  1102. if (psb->crypto_fail_unsupported)
  1103. seq_printf(seq, ",crypto_fail_unsupported");
  1104. return 0;
  1105. }
  1106. enum {
  1107. pohmelfs_opt_idx,
  1108. pohmelfs_opt_crypto_thread_num,
  1109. pohmelfs_opt_trans_max_pages,
  1110. pohmelfs_opt_crypto_fail_unsupported,
  1111. /* Remountable options */
  1112. pohmelfs_opt_trans_scan_timeout,
  1113. pohmelfs_opt_drop_scan_timeout,
  1114. pohmelfs_opt_wait_on_page_timeout,
  1115. pohmelfs_opt_trans_retries,
  1116. pohmelfs_opt_mcache_timeout,
  1117. };
  1118. static struct match_token pohmelfs_tokens[] = {
  1119. {pohmelfs_opt_idx, "idx=%u"},
  1120. {pohmelfs_opt_crypto_thread_num, "crypto_thread_num=%u"},
  1121. {pohmelfs_opt_trans_max_pages, "trans_max_pages=%u"},
  1122. {pohmelfs_opt_crypto_fail_unsupported, "crypto_fail_unsupported"},
  1123. {pohmelfs_opt_trans_scan_timeout, "trans_scan_timeout=%u"},
  1124. {pohmelfs_opt_drop_scan_timeout, "drop_scan_timeout=%u"},
  1125. {pohmelfs_opt_wait_on_page_timeout, "wait_on_page_timeout=%u"},
  1126. {pohmelfs_opt_trans_retries, "trans_retries=%u"},
  1127. {pohmelfs_opt_mcache_timeout, "mcache_timeout=%u"},
  1128. };
  1129. static int pohmelfs_parse_options(char *options, struct pohmelfs_sb *psb, int remount)
  1130. {
  1131. char *p;
  1132. substring_t args[MAX_OPT_ARGS];
  1133. int option, err;
  1134. if (!options)
  1135. return 0;
  1136. while ((p = strsep(&options, ",")) != NULL) {
  1137. int token;
  1138. if (!*p)
  1139. continue;
  1140. token = match_token(p, pohmelfs_tokens, args);
  1141. err = match_int(&args[0], &option);
  1142. if (err)
  1143. return err;
  1144. if (remount && token <= pohmelfs_opt_crypto_fail_unsupported)
  1145. continue;
  1146. switch (token) {
  1147. case pohmelfs_opt_idx:
  1148. psb->idx = option;
  1149. break;
  1150. case pohmelfs_opt_trans_scan_timeout:
  1151. psb->trans_scan_timeout = msecs_to_jiffies(option);
  1152. break;
  1153. case pohmelfs_opt_drop_scan_timeout:
  1154. psb->drop_scan_timeout = msecs_to_jiffies(option);
  1155. break;
  1156. case pohmelfs_opt_wait_on_page_timeout:
  1157. psb->wait_on_page_timeout = msecs_to_jiffies(option);
  1158. break;
  1159. case pohmelfs_opt_mcache_timeout:
  1160. psb->mcache_timeout = msecs_to_jiffies(option);
  1161. break;
  1162. case pohmelfs_opt_trans_retries:
  1163. psb->trans_retries = option;
  1164. break;
  1165. case pohmelfs_opt_crypto_thread_num:
  1166. psb->crypto_thread_num = option;
  1167. break;
  1168. case pohmelfs_opt_trans_max_pages:
  1169. psb->trans_max_pages = option;
  1170. break;
  1171. case pohmelfs_opt_crypto_fail_unsupported:
  1172. psb->crypto_fail_unsupported = 1;
  1173. break;
  1174. default:
  1175. return -EINVAL;
  1176. }
  1177. }
  1178. return 0;
  1179. }
  1180. static int pohmelfs_remount(struct super_block *sb, int *flags, char *data)
  1181. {
  1182. int err;
  1183. struct pohmelfs_sb *psb = POHMELFS_SB(sb);
  1184. unsigned long old_sb_flags = sb->s_flags;
  1185. err = pohmelfs_parse_options(data, psb, 1);
  1186. if (err)
  1187. goto err_out_restore;
  1188. if (!(*flags & MS_RDONLY))
  1189. sb->s_flags &= ~MS_RDONLY;
  1190. return 0;
  1191. err_out_restore:
  1192. sb->s_flags = old_sb_flags;
  1193. return err;
  1194. }
  1195. static void pohmelfs_flush_inode(struct pohmelfs_inode *pi, unsigned int count)
  1196. {
  1197. struct inode *inode = &pi->vfs_inode;
  1198. dprintk("%s: %p: ino: %llu, owned: %d.\n",
  1199. __func__, inode, pi->ino, test_bit(NETFS_INODE_OWNED, &pi->state));
  1200. mutex_lock(&inode->i_mutex);
  1201. if (test_and_clear_bit(NETFS_INODE_OWNED, &pi->state)) {
  1202. filemap_fdatawrite(inode->i_mapping);
  1203. inode->i_sb->s_op->write_inode(inode, 0);
  1204. }
  1205. #ifdef POHMELFS_TRUNCATE_ON_INODE_FLUSH
  1206. truncate_inode_pages(inode->i_mapping, 0);
  1207. #endif
  1208. pohmelfs_data_unlock(pi, 0, ~0, POHMELFS_WRITE_LOCK);
  1209. mutex_unlock(&inode->i_mutex);
  1210. }
  1211. static void pohmelfs_put_inode_count(struct pohmelfs_inode *pi, unsigned int count)
  1212. {
  1213. dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n",
  1214. __func__, pi->ino, pi, &pi->vfs_inode, count);
  1215. if (test_and_clear_bit(NETFS_INODE_NEED_FLUSH, &pi->state))
  1216. pohmelfs_flush_inode(pi, count);
  1217. while (count--)
  1218. iput(&pi->vfs_inode);
  1219. }
  1220. static void pohmelfs_drop_scan(struct work_struct *work)
  1221. {
  1222. struct pohmelfs_sb *psb =
  1223. container_of(work, struct pohmelfs_sb, drop_dwork.work);
  1224. struct pohmelfs_inode *pi;
  1225. unsigned int count = 0;
  1226. while ((pi = pohmelfs_get_inode_from_list(psb, &psb->drop_list, &count)))
  1227. pohmelfs_put_inode_count(pi, count);
  1228. pohmelfs_check_states(psb);
  1229. if (psb->drop_scan_timeout)
  1230. schedule_delayed_work(&psb->drop_dwork, psb->drop_scan_timeout);
  1231. }
  1232. /*
  1233. * Run through all transactions starting from the oldest,
  1234. * drop transaction from current state and try to send it
  1235. * to all remote nodes, which are currently installed.
  1236. */
  1237. static void pohmelfs_trans_scan_state(struct netfs_state *st)
  1238. {
  1239. struct rb_node *rb_node;
  1240. struct netfs_trans_dst *dst;
  1241. struct pohmelfs_sb *psb = st->psb;
  1242. unsigned int timeout = psb->trans_scan_timeout;
  1243. struct netfs_trans *t;
  1244. int err;
  1245. mutex_lock(&st->trans_lock);
  1246. for (rb_node = rb_first(&st->trans_root); rb_node; ) {
  1247. dst = rb_entry(rb_node, struct netfs_trans_dst, state_entry);
  1248. t = dst->trans;
  1249. if (timeout && time_after(dst->send_time + timeout, jiffies)
  1250. && dst->retries == 0)
  1251. break;
  1252. dprintk("%s: t: %p, gen: %u, st: %p, retries: %u, max: %u.\n",
  1253. __func__, t, t->gen, st, dst->retries, psb->trans_retries);
  1254. netfs_trans_get(t);
  1255. rb_node = rb_next(rb_node);
  1256. err = -ETIMEDOUT;
  1257. if (timeout && (++dst->retries < psb->trans_retries))
  1258. err = netfs_trans_resend(t, psb);
  1259. if (err || (t->flags & NETFS_TRANS_SINGLE_DST)) {
  1260. if (netfs_trans_remove_nolock(dst, st))
  1261. netfs_trans_drop_dst_nostate(dst);
  1262. }
  1263. t->result = err;
  1264. netfs_trans_put(t);
  1265. }
  1266. mutex_unlock(&st->trans_lock);
  1267. }
  1268. /*
  1269. * Walk through all installed network states and resend all
  1270. * transactions, which are old enough.
  1271. */
  1272. static void pohmelfs_trans_scan(struct work_struct *work)
  1273. {
  1274. struct pohmelfs_sb *psb =
  1275. container_of(work, struct pohmelfs_sb, dwork.work);
  1276. struct netfs_state *st;
  1277. struct pohmelfs_config *c;
  1278. mutex_lock(&psb->state_lock);
  1279. list_for_each_entry(c, &psb->state_list, config_entry) {
  1280. st = &c->state;
  1281. pohmelfs_trans_scan_state(st);
  1282. }
  1283. mutex_unlock(&psb->state_lock);
  1284. /*
  1285. * If no timeout specified then system is in the middle of umount process,
  1286. * so no need to reschedule scanning process again.
  1287. */
  1288. if (psb->trans_scan_timeout)
  1289. schedule_delayed_work(&psb->dwork, psb->trans_scan_timeout);
  1290. }
  1291. int pohmelfs_meta_command_data(struct pohmelfs_inode *pi, u64 id, unsigned int cmd_op, char *addon,
  1292. unsigned int flags, netfs_trans_complete_t complete, void *priv, u64 start)
  1293. {
  1294. struct inode *inode = &pi->vfs_inode;
  1295. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  1296. int err = 0, sz;
  1297. struct netfs_trans *t;
  1298. int path_len, addon_len = 0;
  1299. void *data;
  1300. struct netfs_inode_info *info;
  1301. struct netfs_cmd *cmd;
  1302. dprintk("%s: ino: %llu, cmd: %u, addon: %p.\n", __func__, pi->ino, cmd_op, addon);
  1303. path_len = pohmelfs_path_length(pi);
  1304. if (path_len < 0) {
  1305. err = path_len;
  1306. goto err_out_exit;
  1307. }
  1308. if (addon)
  1309. addon_len = strlen(addon) + 1; /* 0-byte */
  1310. sz = addon_len;
  1311. if (cmd_op == NETFS_INODE_INFO)
  1312. sz += sizeof(struct netfs_inode_info);
  1313. t = netfs_trans_alloc(psb, sz + path_len, flags, 0);
  1314. if (!t) {
  1315. err = -ENOMEM;
  1316. goto err_out_exit;
  1317. }
  1318. t->complete = complete;
  1319. t->private = priv;
  1320. cmd = netfs_trans_current(t);
  1321. data = (void *)(cmd + 1);
  1322. if (cmd_op == NETFS_INODE_INFO) {
  1323. info = (struct netfs_inode_info *)(cmd + 1);
  1324. data = (void *)(info + 1);
  1325. /*
  1326. * We are under i_mutex, can read and change whatever we want...
  1327. */
  1328. info->mode = inode->i_mode;
  1329. info->nlink = inode->i_nlink;
  1330. info->uid = inode->i_uid;
  1331. info->gid = inode->i_gid;
  1332. info->blocks = inode->i_blocks;
  1333. info->rdev = inode->i_rdev;
  1334. info->size = inode->i_size;
  1335. info->version = inode->i_version;
  1336. netfs_convert_inode_info(info);
  1337. }
  1338. path_len = pohmelfs_construct_path_string(pi, data, path_len);
  1339. if (path_len < 0)
  1340. goto err_out_free;
  1341. dprintk("%s: path_len: %d.\n", __func__, path_len);
  1342. if (addon) {
  1343. path_len--; /* Do not place null-byte before the addon */
  1344. path_len += sprintf(data + path_len, "/%s", addon) + 1; /* 0 - byte */
  1345. }
  1346. sz += path_len;
  1347. cmd->cmd = cmd_op;
  1348. cmd->ext = path_len;
  1349. cmd->size = sz;
  1350. cmd->id = id;
  1351. cmd->start = start;
  1352. netfs_convert_cmd(cmd);
  1353. netfs_trans_update(cmd, t, sz);
  1354. /*
  1355. * Note, that it is possible to leak error here: transaction callback will not
  1356. * be invoked for allocation path failure.
  1357. */
  1358. return netfs_trans_finish(t, psb);
  1359. err_out_free:
  1360. netfs_trans_free(t);
  1361. err_out_exit:
  1362. if (complete)
  1363. complete(NULL, 0, priv, err);
  1364. return err;
  1365. }
  1366. int pohmelfs_meta_command(struct pohmelfs_inode *pi, unsigned int cmd_op, unsigned int flags,
  1367. netfs_trans_complete_t complete, void *priv, u64 start)
  1368. {
  1369. return pohmelfs_meta_command_data(pi, pi->ino, cmd_op, NULL, flags, complete, priv, start);
  1370. }
  1371. /*
  1372. * Send request and wait for POHMELFS root capabilities response,
  1373. * which will update server's informaion about size of the export,
  1374. * permissions, number of objects, available size and so on.
  1375. */
  1376. static int pohmelfs_root_handshake(struct pohmelfs_sb *psb)
  1377. {
  1378. struct netfs_trans *t;
  1379. struct netfs_cmd *cmd;
  1380. int err = -ENOMEM;
  1381. t = netfs_trans_alloc(psb, 0, 0, 0);
  1382. if (!t)
  1383. goto err_out_exit;
  1384. cmd = netfs_trans_current(t);
  1385. cmd->cmd = NETFS_CAPABILITIES;
  1386. cmd->id = POHMELFS_ROOT_CAPABILITIES;
  1387. cmd->size = 0;
  1388. cmd->start = 0;
  1389. cmd->ext = 0;
  1390. cmd->csize = 0;
  1391. netfs_convert_cmd(cmd);
  1392. netfs_trans_update(cmd, t, 0);
  1393. err = netfs_trans_finish(t, psb);
  1394. if (err)
  1395. goto err_out_exit;
  1396. psb->flags = ~0;
  1397. err = wait_event_interruptible_timeout(psb->wait,
  1398. (psb->flags != ~0),
  1399. psb->wait_on_page_timeout);
  1400. if (!err)
  1401. err = -ETIMEDOUT;
  1402. else if (err > 0)
  1403. err = -psb->flags;
  1404. if (err)
  1405. goto err_out_exit;
  1406. return 0;
  1407. err_out_exit:
  1408. return err;
  1409. }
  1410. static int pohmelfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
  1411. {
  1412. struct netfs_state *st;
  1413. struct pohmelfs_ctl *ctl;
  1414. struct pohmelfs_sb *psb = POHMELFS_SB(mnt->mnt_sb);
  1415. struct pohmelfs_config *c;
  1416. mutex_lock(&psb->state_lock);
  1417. seq_printf(m, "\nidx addr(:port) socket_type protocol active priority permissions\n");
  1418. list_for_each_entry(c, &psb->state_list, config_entry) {
  1419. st = &c->state;
  1420. ctl = &st->ctl;
  1421. seq_printf(m, "%u ", ctl->idx);
  1422. if (ctl->addr.sa_family == AF_INET) {
  1423. struct sockaddr_in *sin = (struct sockaddr_in *)&st->ctl.addr;
  1424. seq_printf(m, "%pI4:%u", &sin->sin_addr.s_addr, ntohs(sin->sin_port));
  1425. } else if (ctl->addr.sa_family == AF_INET6) {
  1426. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&st->ctl.addr;
  1427. seq_printf(m, "%pi6:%u", &sin->sin6_addr, ntohs(sin->sin6_port));
  1428. } else {
  1429. unsigned int i;
  1430. for (i = 0; i < ctl->addrlen; ++i)
  1431. seq_printf(m, "%02x.", ctl->addr.addr[i]);
  1432. }
  1433. seq_printf(m, " %u %u %d %u %x\n",
  1434. ctl->type, ctl->proto,
  1435. st->socket != NULL,
  1436. ctl->prio, ctl->perm);
  1437. }
  1438. mutex_unlock(&psb->state_lock);
  1439. return 0;
  1440. }
  1441. static const struct super_operations pohmelfs_sb_ops = {
  1442. .alloc_inode = pohmelfs_alloc_inode,
  1443. .destroy_inode = pohmelfs_destroy_inode,
  1444. .drop_inode = pohmelfs_drop_inode,
  1445. .write_inode = pohmelfs_write_inode,
  1446. .put_super = pohmelfs_put_super,
  1447. .remount_fs = pohmelfs_remount,
  1448. .statfs = pohmelfs_statfs,
  1449. .show_options = pohmelfs_show_options,
  1450. .show_stats = pohmelfs_show_stats,
  1451. };
  1452. /*
  1453. * Allocate private superblock and create root dir.
  1454. */
  1455. static int pohmelfs_fill_super(struct super_block *sb, void *data, int silent)
  1456. {
  1457. struct pohmelfs_sb *psb;
  1458. int err = -ENOMEM;
  1459. struct inode *root;
  1460. struct pohmelfs_inode *npi;
  1461. struct qstr str;
  1462. psb = kzalloc(sizeof(struct pohmelfs_sb), GFP_KERNEL);
  1463. if (!psb)
  1464. goto err_out_exit;
  1465. err = bdi_init(&psb->bdi);
  1466. if (err)
  1467. goto err_out_free_sb;
  1468. err = bdi_register(&psb->bdi, NULL, "pfs-%d", atomic_inc_return(&psb_bdi_num));
  1469. if (err) {
  1470. bdi_destroy(&psb->bdi);
  1471. goto err_out_free_sb;
  1472. }
  1473. sb->s_fs_info = psb;
  1474. sb->s_op = &pohmelfs_sb_ops;
  1475. sb->s_magic = POHMELFS_MAGIC_NUM;
  1476. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1477. sb->s_blocksize = PAGE_SIZE;
  1478. sb->s_bdi = &psb->bdi;
  1479. psb->sb = sb;
  1480. psb->ino = 2;
  1481. psb->idx = 0;
  1482. psb->active_state = NULL;
  1483. psb->trans_retries = 5;
  1484. psb->trans_data_size = PAGE_SIZE;
  1485. psb->drop_scan_timeout = msecs_to_jiffies(1000);
  1486. psb->trans_scan_timeout = msecs_to_jiffies(5000);
  1487. psb->wait_on_page_timeout = msecs_to_jiffies(5000);
  1488. init_waitqueue_head(&psb->wait);
  1489. spin_lock_init(&psb->ino_lock);
  1490. INIT_LIST_HEAD(&psb->drop_list);
  1491. mutex_init(&psb->mcache_lock);
  1492. psb->mcache_root = RB_ROOT;
  1493. psb->mcache_timeout = msecs_to_jiffies(5000);
  1494. atomic_long_set(&psb->mcache_gen, 0);
  1495. psb->trans_max_pages = 100;
  1496. psb->crypto_align_size = 16;
  1497. psb->crypto_attached_size = 0;
  1498. psb->hash_strlen = 0;
  1499. psb->cipher_strlen = 0;
  1500. psb->perform_crypto = 0;
  1501. psb->crypto_thread_num = 2;
  1502. psb->crypto_fail_unsupported = 0;
  1503. mutex_init(&psb->crypto_thread_lock);
  1504. INIT_LIST_HEAD(&psb->crypto_ready_list);
  1505. INIT_LIST_HEAD(&psb->crypto_active_list);
  1506. atomic_set(&psb->trans_gen, 1);
  1507. atomic_long_set(&psb->total_inodes, 0);
  1508. mutex_init(&psb->state_lock);
  1509. INIT_LIST_HEAD(&psb->state_list);
  1510. err = pohmelfs_parse_options((char *) data, psb, 0);
  1511. if (err)
  1512. goto err_out_free_bdi;
  1513. err = pohmelfs_copy_crypto(psb);
  1514. if (err)
  1515. goto err_out_free_bdi;
  1516. err = pohmelfs_state_init(psb);
  1517. if (err)
  1518. goto err_out_free_strings;
  1519. err = pohmelfs_crypto_init(psb);
  1520. if (err)
  1521. goto err_out_state_exit;
  1522. err = pohmelfs_root_handshake(psb);
  1523. if (err)
  1524. goto err_out_crypto_exit;
  1525. str.name = "/";
  1526. str.hash = jhash("/", 1, 0);
  1527. str.len = 1;
  1528. npi = pohmelfs_create_entry_local(psb, NULL, &str, 0, 0755|S_IFDIR);
  1529. if (IS_ERR(npi)) {
  1530. err = PTR_ERR(npi);
  1531. goto err_out_crypto_exit;
  1532. }
  1533. set_bit(NETFS_INODE_REMOTE_SYNCED, &npi->state);
  1534. clear_bit(NETFS_INODE_OWNED, &npi->state);
  1535. root = &npi->vfs_inode;
  1536. sb->s_root = d_alloc_root(root);
  1537. if (!sb->s_root)
  1538. goto err_out_put_root;
  1539. INIT_DELAYED_WORK(&psb->drop_dwork, pohmelfs_drop_scan);
  1540. schedule_delayed_work(&psb->drop_dwork, psb->drop_scan_timeout);
  1541. INIT_DELAYED_WORK(&psb->dwork, pohmelfs_trans_scan);
  1542. schedule_delayed_work(&psb->dwork, psb->trans_scan_timeout);
  1543. return 0;
  1544. err_out_put_root:
  1545. iput(root);
  1546. err_out_crypto_exit:
  1547. pohmelfs_crypto_exit(psb);
  1548. err_out_state_exit:
  1549. pohmelfs_state_exit(psb);
  1550. err_out_free_strings:
  1551. kfree(psb->cipher_string);
  1552. kfree(psb->hash_string);
  1553. err_out_free_bdi:
  1554. bdi_destroy(&psb->bdi);
  1555. err_out_free_sb:
  1556. kfree(psb);
  1557. err_out_exit:
  1558. dprintk("%s: err: %d.\n", __func__, err);
  1559. return err;
  1560. }
  1561. /*
  1562. * Some VFS magic here...
  1563. */
  1564. static struct dentry *pohmelfs_mount(struct file_system_type *fs_type,
  1565. int flags, const char *dev_name, void *data)
  1566. {
  1567. return mount_nodev(fs_type, flags, data, pohmelfs_fill_super);
  1568. }
  1569. /*
  1570. * We need this to sync all inodes earlier, since when writeback
  1571. * is invoked from the umount/mntput path dcache is already shrunk,
  1572. * see generic_shutdown_super(), and no inodes can access the path.
  1573. */
  1574. static void pohmelfs_kill_super(struct super_block *sb)
  1575. {
  1576. sync_inodes_sb(sb);
  1577. kill_anon_super(sb);
  1578. }
  1579. static struct file_system_type pohmel_fs_type = {
  1580. .owner = THIS_MODULE,
  1581. .name = "pohmel",
  1582. .mount = pohmelfs_mount,
  1583. .kill_sb = pohmelfs_kill_super,
  1584. };
  1585. /*
  1586. * Cache and module initializations and freeing routings.
  1587. */
  1588. static void pohmelfs_init_once(void *data)
  1589. {
  1590. struct pohmelfs_inode *pi = data;
  1591. inode_init_once(&pi->vfs_inode);
  1592. }
  1593. static int __init pohmelfs_init_inodecache(void)
  1594. {
  1595. pohmelfs_inode_cache = kmem_cache_create("pohmelfs_inode_cache",
  1596. sizeof(struct pohmelfs_inode),
  1597. 0, (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  1598. pohmelfs_init_once);
  1599. if (!pohmelfs_inode_cache)
  1600. return -ENOMEM;
  1601. return 0;
  1602. }
  1603. static void pohmelfs_destroy_inodecache(void)
  1604. {
  1605. kmem_cache_destroy(pohmelfs_inode_cache);
  1606. }
  1607. static int __init init_pohmel_fs(void)
  1608. {
  1609. int err;
  1610. err = pohmelfs_config_init();
  1611. if (err)
  1612. goto err_out_exit;
  1613. err = pohmelfs_init_inodecache();
  1614. if (err)
  1615. goto err_out_config_exit;
  1616. err = pohmelfs_mcache_init();
  1617. if (err)
  1618. goto err_out_destroy;
  1619. err = netfs_trans_init();
  1620. if (err)
  1621. goto err_out_mcache_exit;
  1622. err = register_filesystem(&pohmel_fs_type);
  1623. if (err)
  1624. goto err_out_trans;
  1625. return 0;
  1626. err_out_trans:
  1627. netfs_trans_exit();
  1628. err_out_mcache_exit:
  1629. pohmelfs_mcache_exit();
  1630. err_out_destroy:
  1631. pohmelfs_destroy_inodecache();
  1632. err_out_config_exit:
  1633. pohmelfs_config_exit();
  1634. err_out_exit:
  1635. return err;
  1636. }
  1637. static void __exit exit_pohmel_fs(void)
  1638. {
  1639. unregister_filesystem(&pohmel_fs_type);
  1640. pohmelfs_destroy_inodecache();
  1641. pohmelfs_mcache_exit();
  1642. pohmelfs_config_exit();
  1643. netfs_trans_exit();
  1644. }
  1645. module_init(init_pohmel_fs);
  1646. module_exit(exit_pohmel_fs);
  1647. MODULE_LICENSE("GPL");
  1648. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  1649. MODULE_DESCRIPTION("Pohmel filesystem");