vfs_cache.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
  4. * Copyright (C) 2019 Samsung Electronics Co., Ltd.
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/filelock.h>
  8. #include <linux/slab.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/kthread.h>
  11. #include <linux/freezer.h>
  12. #include "glob.h"
  13. #include "vfs_cache.h"
  14. #include "oplock.h"
  15. #include "vfs.h"
  16. #include "connection.h"
  17. #include "mgmt/tree_connect.h"
  18. #include "mgmt/user_session.h"
  19. #include "smb_common.h"
  20. #include "server.h"
  21. #define S_DEL_PENDING 1
  22. #define S_DEL_ON_CLS 2
  23. #define S_DEL_ON_CLS_STREAM 8
  24. static unsigned int inode_hash_mask __read_mostly;
  25. static unsigned int inode_hash_shift __read_mostly;
  26. static struct hlist_head *inode_hashtable __read_mostly;
  27. static DEFINE_RWLOCK(inode_hash_lock);
  28. static struct ksmbd_file_table global_ft;
  29. static atomic_long_t fd_limit;
  30. static struct kmem_cache *filp_cache;
  31. static bool durable_scavenger_running;
  32. static DEFINE_MUTEX(durable_scavenger_lock);
  33. static wait_queue_head_t dh_wq;
  34. void ksmbd_set_fd_limit(unsigned long limit)
  35. {
  36. limit = min(limit, get_max_files());
  37. atomic_long_set(&fd_limit, limit);
  38. }
  39. static bool fd_limit_depleted(void)
  40. {
  41. long v = atomic_long_dec_return(&fd_limit);
  42. if (v >= 0)
  43. return false;
  44. atomic_long_inc(&fd_limit);
  45. return true;
  46. }
  47. static void fd_limit_close(void)
  48. {
  49. atomic_long_inc(&fd_limit);
  50. }
  51. /*
  52. * INODE hash
  53. */
  54. static unsigned long inode_hash(struct super_block *sb, unsigned long hashval)
  55. {
  56. unsigned long tmp;
  57. tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
  58. L1_CACHE_BYTES;
  59. tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift);
  60. return tmp & inode_hash_mask;
  61. }
  62. static struct ksmbd_inode *__ksmbd_inode_lookup(struct dentry *de)
  63. {
  64. struct hlist_head *head = inode_hashtable +
  65. inode_hash(d_inode(de)->i_sb, (unsigned long)de);
  66. struct ksmbd_inode *ci = NULL, *ret_ci = NULL;
  67. hlist_for_each_entry(ci, head, m_hash) {
  68. if (ci->m_de == de) {
  69. if (atomic_inc_not_zero(&ci->m_count))
  70. ret_ci = ci;
  71. break;
  72. }
  73. }
  74. return ret_ci;
  75. }
  76. static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp)
  77. {
  78. return __ksmbd_inode_lookup(fp->filp->f_path.dentry);
  79. }
  80. struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d)
  81. {
  82. struct ksmbd_inode *ci;
  83. read_lock(&inode_hash_lock);
  84. ci = __ksmbd_inode_lookup(d);
  85. read_unlock(&inode_hash_lock);
  86. return ci;
  87. }
  88. int ksmbd_query_inode_status(struct dentry *dentry)
  89. {
  90. struct ksmbd_inode *ci;
  91. int ret = KSMBD_INODE_STATUS_UNKNOWN;
  92. read_lock(&inode_hash_lock);
  93. ci = __ksmbd_inode_lookup(dentry);
  94. if (ci) {
  95. ret = KSMBD_INODE_STATUS_OK;
  96. if (ci->m_flags & (S_DEL_PENDING | S_DEL_ON_CLS))
  97. ret = KSMBD_INODE_STATUS_PENDING_DELETE;
  98. atomic_dec(&ci->m_count);
  99. }
  100. read_unlock(&inode_hash_lock);
  101. return ret;
  102. }
  103. bool ksmbd_inode_pending_delete(struct ksmbd_file *fp)
  104. {
  105. return (fp->f_ci->m_flags & (S_DEL_PENDING | S_DEL_ON_CLS));
  106. }
  107. void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp)
  108. {
  109. fp->f_ci->m_flags |= S_DEL_PENDING;
  110. }
  111. void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp)
  112. {
  113. fp->f_ci->m_flags &= ~S_DEL_PENDING;
  114. }
  115. void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
  116. int file_info)
  117. {
  118. if (ksmbd_stream_fd(fp)) {
  119. fp->f_ci->m_flags |= S_DEL_ON_CLS_STREAM;
  120. return;
  121. }
  122. fp->f_ci->m_flags |= S_DEL_ON_CLS;
  123. }
  124. static void ksmbd_inode_hash(struct ksmbd_inode *ci)
  125. {
  126. struct hlist_head *b = inode_hashtable +
  127. inode_hash(d_inode(ci->m_de)->i_sb, (unsigned long)ci->m_de);
  128. hlist_add_head(&ci->m_hash, b);
  129. }
  130. static void ksmbd_inode_unhash(struct ksmbd_inode *ci)
  131. {
  132. write_lock(&inode_hash_lock);
  133. hlist_del_init(&ci->m_hash);
  134. write_unlock(&inode_hash_lock);
  135. }
  136. static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp)
  137. {
  138. atomic_set(&ci->m_count, 1);
  139. atomic_set(&ci->op_count, 0);
  140. atomic_set(&ci->sop_count, 0);
  141. ci->m_flags = 0;
  142. ci->m_fattr = 0;
  143. INIT_LIST_HEAD(&ci->m_fp_list);
  144. INIT_LIST_HEAD(&ci->m_op_list);
  145. init_rwsem(&ci->m_lock);
  146. ci->m_de = fp->filp->f_path.dentry;
  147. return 0;
  148. }
  149. static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp)
  150. {
  151. struct ksmbd_inode *ci, *tmpci;
  152. int rc;
  153. read_lock(&inode_hash_lock);
  154. ci = ksmbd_inode_lookup(fp);
  155. read_unlock(&inode_hash_lock);
  156. if (ci)
  157. return ci;
  158. ci = kmalloc(sizeof(struct ksmbd_inode), KSMBD_DEFAULT_GFP);
  159. if (!ci)
  160. return NULL;
  161. rc = ksmbd_inode_init(ci, fp);
  162. if (rc) {
  163. pr_err("inode initialized failed\n");
  164. kfree(ci);
  165. return NULL;
  166. }
  167. write_lock(&inode_hash_lock);
  168. tmpci = ksmbd_inode_lookup(fp);
  169. if (!tmpci) {
  170. ksmbd_inode_hash(ci);
  171. } else {
  172. kfree(ci);
  173. ci = tmpci;
  174. }
  175. write_unlock(&inode_hash_lock);
  176. return ci;
  177. }
  178. static void ksmbd_inode_free(struct ksmbd_inode *ci)
  179. {
  180. ksmbd_inode_unhash(ci);
  181. kfree(ci);
  182. }
  183. void ksmbd_inode_put(struct ksmbd_inode *ci)
  184. {
  185. if (atomic_dec_and_test(&ci->m_count))
  186. ksmbd_inode_free(ci);
  187. }
  188. int __init ksmbd_inode_hash_init(void)
  189. {
  190. unsigned int loop;
  191. unsigned long numentries = 16384;
  192. unsigned long bucketsize = sizeof(struct hlist_head);
  193. unsigned long size;
  194. inode_hash_shift = ilog2(numentries);
  195. inode_hash_mask = (1 << inode_hash_shift) - 1;
  196. size = bucketsize << inode_hash_shift;
  197. /* init master fp hash table */
  198. inode_hashtable = vmalloc(size);
  199. if (!inode_hashtable)
  200. return -ENOMEM;
  201. for (loop = 0; loop < (1U << inode_hash_shift); loop++)
  202. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  203. return 0;
  204. }
  205. void ksmbd_release_inode_hash(void)
  206. {
  207. vfree(inode_hashtable);
  208. }
  209. static void __ksmbd_inode_close(struct ksmbd_file *fp)
  210. {
  211. struct ksmbd_inode *ci = fp->f_ci;
  212. int err;
  213. struct file *filp;
  214. filp = fp->filp;
  215. if (ksmbd_stream_fd(fp) && (ci->m_flags & S_DEL_ON_CLS_STREAM)) {
  216. ci->m_flags &= ~S_DEL_ON_CLS_STREAM;
  217. err = ksmbd_vfs_remove_xattr(file_mnt_idmap(filp),
  218. &filp->f_path,
  219. fp->stream.name,
  220. true);
  221. if (err)
  222. pr_err("remove xattr failed : %s\n",
  223. fp->stream.name);
  224. }
  225. if (atomic_dec_and_test(&ci->m_count)) {
  226. down_write(&ci->m_lock);
  227. if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) {
  228. ci->m_flags &= ~(S_DEL_ON_CLS | S_DEL_PENDING);
  229. up_write(&ci->m_lock);
  230. ksmbd_vfs_unlink(filp);
  231. down_write(&ci->m_lock);
  232. }
  233. up_write(&ci->m_lock);
  234. ksmbd_inode_free(ci);
  235. }
  236. }
  237. static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp)
  238. {
  239. if (!has_file_id(fp->persistent_id))
  240. return;
  241. idr_remove(global_ft.idr, fp->persistent_id);
  242. }
  243. static void ksmbd_remove_durable_fd(struct ksmbd_file *fp)
  244. {
  245. write_lock(&global_ft.lock);
  246. __ksmbd_remove_durable_fd(fp);
  247. write_unlock(&global_ft.lock);
  248. if (waitqueue_active(&dh_wq))
  249. wake_up(&dh_wq);
  250. }
  251. static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
  252. {
  253. if (!has_file_id(fp->volatile_id))
  254. return;
  255. down_write(&fp->f_ci->m_lock);
  256. list_del_init(&fp->node);
  257. up_write(&fp->f_ci->m_lock);
  258. write_lock(&ft->lock);
  259. idr_remove(ft->idr, fp->volatile_id);
  260. write_unlock(&ft->lock);
  261. }
  262. static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
  263. {
  264. struct file *filp;
  265. struct ksmbd_lock *smb_lock, *tmp_lock;
  266. fd_limit_close();
  267. ksmbd_remove_durable_fd(fp);
  268. if (ft)
  269. __ksmbd_remove_fd(ft, fp);
  270. close_id_del_oplock(fp);
  271. filp = fp->filp;
  272. __ksmbd_inode_close(fp);
  273. if (!IS_ERR_OR_NULL(filp))
  274. fput(filp);
  275. /* because the reference count of fp is 0, it is guaranteed that
  276. * there are not accesses to fp->lock_list.
  277. */
  278. list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
  279. spin_lock(&fp->conn->llist_lock);
  280. list_del(&smb_lock->clist);
  281. spin_unlock(&fp->conn->llist_lock);
  282. list_del(&smb_lock->flist);
  283. locks_free_lock(smb_lock->fl);
  284. kfree(smb_lock);
  285. }
  286. if (ksmbd_stream_fd(fp))
  287. kfree(fp->stream.name);
  288. kmem_cache_free(filp_cache, fp);
  289. }
  290. static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp)
  291. {
  292. if (fp->f_state != FP_INITED)
  293. return NULL;
  294. if (!atomic_inc_not_zero(&fp->refcount))
  295. return NULL;
  296. return fp;
  297. }
  298. static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft,
  299. u64 id)
  300. {
  301. struct ksmbd_file *fp;
  302. if (!has_file_id(id))
  303. return NULL;
  304. read_lock(&ft->lock);
  305. fp = idr_find(ft->idr, id);
  306. if (fp)
  307. fp = ksmbd_fp_get(fp);
  308. read_unlock(&ft->lock);
  309. return fp;
  310. }
  311. static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp)
  312. {
  313. __ksmbd_close_fd(&work->sess->file_table, fp);
  314. atomic_dec(&work->conn->stats.open_files_count);
  315. }
  316. static void set_close_state_blocked_works(struct ksmbd_file *fp)
  317. {
  318. struct ksmbd_work *cancel_work;
  319. spin_lock(&fp->f_lock);
  320. list_for_each_entry(cancel_work, &fp->blocked_works,
  321. fp_entry) {
  322. cancel_work->state = KSMBD_WORK_CLOSED;
  323. cancel_work->cancel_fn(cancel_work->cancel_argv);
  324. }
  325. spin_unlock(&fp->f_lock);
  326. }
  327. int ksmbd_close_fd(struct ksmbd_work *work, u64 id)
  328. {
  329. struct ksmbd_file *fp;
  330. struct ksmbd_file_table *ft;
  331. if (!has_file_id(id))
  332. return 0;
  333. ft = &work->sess->file_table;
  334. write_lock(&ft->lock);
  335. fp = idr_find(ft->idr, id);
  336. if (fp) {
  337. set_close_state_blocked_works(fp);
  338. if (fp->f_state != FP_INITED)
  339. fp = NULL;
  340. else {
  341. fp->f_state = FP_CLOSED;
  342. if (!atomic_dec_and_test(&fp->refcount))
  343. fp = NULL;
  344. }
  345. }
  346. write_unlock(&ft->lock);
  347. if (!fp)
  348. return -EINVAL;
  349. __put_fd_final(work, fp);
  350. return 0;
  351. }
  352. void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp)
  353. {
  354. if (!fp)
  355. return;
  356. if (!atomic_dec_and_test(&fp->refcount))
  357. return;
  358. __put_fd_final(work, fp);
  359. }
  360. static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp)
  361. {
  362. if (!fp)
  363. return false;
  364. if (fp->tcon != tcon)
  365. return false;
  366. return true;
  367. }
  368. struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id)
  369. {
  370. return __ksmbd_lookup_fd(&work->sess->file_table, id);
  371. }
  372. struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id)
  373. {
  374. struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
  375. if (__sanity_check(work->tcon, fp))
  376. return fp;
  377. ksmbd_fd_put(work, fp);
  378. return NULL;
  379. }
  380. struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id,
  381. u64 pid)
  382. {
  383. struct ksmbd_file *fp;
  384. if (!has_file_id(id)) {
  385. id = work->compound_fid;
  386. pid = work->compound_pfid;
  387. }
  388. fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
  389. if (!__sanity_check(work->tcon, fp)) {
  390. ksmbd_fd_put(work, fp);
  391. return NULL;
  392. }
  393. if (fp->persistent_id != pid) {
  394. ksmbd_fd_put(work, fp);
  395. return NULL;
  396. }
  397. return fp;
  398. }
  399. struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id)
  400. {
  401. return __ksmbd_lookup_fd(&global_ft, id);
  402. }
  403. struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id)
  404. {
  405. struct ksmbd_file *fp;
  406. fp = __ksmbd_lookup_fd(&global_ft, id);
  407. if (fp && (fp->conn ||
  408. (fp->durable_scavenger_timeout &&
  409. (fp->durable_scavenger_timeout <
  410. jiffies_to_msecs(jiffies))))) {
  411. ksmbd_put_durable_fd(fp);
  412. fp = NULL;
  413. }
  414. return fp;
  415. }
  416. void ksmbd_put_durable_fd(struct ksmbd_file *fp)
  417. {
  418. if (!atomic_dec_and_test(&fp->refcount))
  419. return;
  420. __ksmbd_close_fd(NULL, fp);
  421. }
  422. struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid)
  423. {
  424. struct ksmbd_file *fp = NULL;
  425. unsigned int id;
  426. read_lock(&global_ft.lock);
  427. idr_for_each_entry(global_ft.idr, fp, id) {
  428. if (!memcmp(fp->create_guid,
  429. cguid,
  430. SMB2_CREATE_GUID_SIZE)) {
  431. fp = ksmbd_fp_get(fp);
  432. break;
  433. }
  434. }
  435. read_unlock(&global_ft.lock);
  436. return fp;
  437. }
  438. struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry)
  439. {
  440. struct ksmbd_file *lfp;
  441. struct ksmbd_inode *ci;
  442. struct inode *inode = d_inode(dentry);
  443. read_lock(&inode_hash_lock);
  444. ci = __ksmbd_inode_lookup(dentry);
  445. read_unlock(&inode_hash_lock);
  446. if (!ci)
  447. return NULL;
  448. down_read(&ci->m_lock);
  449. list_for_each_entry(lfp, &ci->m_fp_list, node) {
  450. if (inode == file_inode(lfp->filp)) {
  451. atomic_dec(&ci->m_count);
  452. lfp = ksmbd_fp_get(lfp);
  453. up_read(&ci->m_lock);
  454. return lfp;
  455. }
  456. }
  457. atomic_dec(&ci->m_count);
  458. up_read(&ci->m_lock);
  459. return NULL;
  460. }
  461. #define OPEN_ID_TYPE_VOLATILE_ID (0)
  462. #define OPEN_ID_TYPE_PERSISTENT_ID (1)
  463. static void __open_id_set(struct ksmbd_file *fp, u64 id, int type)
  464. {
  465. if (type == OPEN_ID_TYPE_VOLATILE_ID)
  466. fp->volatile_id = id;
  467. if (type == OPEN_ID_TYPE_PERSISTENT_ID)
  468. fp->persistent_id = id;
  469. }
  470. static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
  471. int type)
  472. {
  473. u64 id = 0;
  474. int ret;
  475. if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) {
  476. __open_id_set(fp, KSMBD_NO_FID, type);
  477. return -EMFILE;
  478. }
  479. idr_preload(KSMBD_DEFAULT_GFP);
  480. write_lock(&ft->lock);
  481. ret = idr_alloc_cyclic(ft->idr, fp, 0, INT_MAX - 1, GFP_NOWAIT);
  482. if (ret >= 0) {
  483. id = ret;
  484. ret = 0;
  485. } else {
  486. id = KSMBD_NO_FID;
  487. fd_limit_close();
  488. }
  489. __open_id_set(fp, id, type);
  490. write_unlock(&ft->lock);
  491. idr_preload_end();
  492. return ret;
  493. }
  494. unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp)
  495. {
  496. __open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID);
  497. return fp->persistent_id;
  498. }
  499. struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
  500. {
  501. struct ksmbd_file *fp;
  502. int ret;
  503. fp = kmem_cache_zalloc(filp_cache, KSMBD_DEFAULT_GFP);
  504. if (!fp) {
  505. pr_err("Failed to allocate memory\n");
  506. return ERR_PTR(-ENOMEM);
  507. }
  508. INIT_LIST_HEAD(&fp->blocked_works);
  509. INIT_LIST_HEAD(&fp->node);
  510. INIT_LIST_HEAD(&fp->lock_list);
  511. spin_lock_init(&fp->f_lock);
  512. atomic_set(&fp->refcount, 1);
  513. fp->filp = filp;
  514. fp->conn = work->conn;
  515. fp->tcon = work->tcon;
  516. fp->volatile_id = KSMBD_NO_FID;
  517. fp->persistent_id = KSMBD_NO_FID;
  518. fp->f_state = FP_NEW;
  519. fp->f_ci = ksmbd_inode_get(fp);
  520. if (!fp->f_ci) {
  521. ret = -ENOMEM;
  522. goto err_out;
  523. }
  524. ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
  525. if (ret) {
  526. ksmbd_inode_put(fp->f_ci);
  527. goto err_out;
  528. }
  529. atomic_inc(&work->conn->stats.open_files_count);
  530. return fp;
  531. err_out:
  532. kmem_cache_free(filp_cache, fp);
  533. return ERR_PTR(ret);
  534. }
  535. void ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
  536. unsigned int state)
  537. {
  538. if (!fp)
  539. return;
  540. write_lock(&ft->lock);
  541. fp->f_state = state;
  542. write_unlock(&ft->lock);
  543. }
  544. static int
  545. __close_file_table_ids(struct ksmbd_file_table *ft,
  546. struct ksmbd_tree_connect *tcon,
  547. bool (*skip)(struct ksmbd_tree_connect *tcon,
  548. struct ksmbd_file *fp))
  549. {
  550. struct ksmbd_file *fp;
  551. unsigned int id = 0;
  552. int num = 0;
  553. while (1) {
  554. write_lock(&ft->lock);
  555. fp = idr_get_next(ft->idr, &id);
  556. if (!fp) {
  557. write_unlock(&ft->lock);
  558. break;
  559. }
  560. if (skip(tcon, fp) ||
  561. !atomic_dec_and_test(&fp->refcount)) {
  562. id++;
  563. write_unlock(&ft->lock);
  564. continue;
  565. }
  566. set_close_state_blocked_works(fp);
  567. idr_remove(ft->idr, fp->volatile_id);
  568. fp->volatile_id = KSMBD_NO_FID;
  569. write_unlock(&ft->lock);
  570. down_write(&fp->f_ci->m_lock);
  571. list_del_init(&fp->node);
  572. up_write(&fp->f_ci->m_lock);
  573. __ksmbd_close_fd(ft, fp);
  574. num++;
  575. id++;
  576. }
  577. return num;
  578. }
  579. static inline bool is_reconnectable(struct ksmbd_file *fp)
  580. {
  581. struct oplock_info *opinfo = opinfo_get(fp);
  582. bool reconn = false;
  583. if (!opinfo)
  584. return false;
  585. if (opinfo->op_state != OPLOCK_STATE_NONE) {
  586. opinfo_put(opinfo);
  587. return false;
  588. }
  589. if (fp->is_resilient || fp->is_persistent)
  590. reconn = true;
  591. else if (fp->is_durable && opinfo->is_lease &&
  592. opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
  593. reconn = true;
  594. else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH)
  595. reconn = true;
  596. opinfo_put(opinfo);
  597. return reconn;
  598. }
  599. static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon,
  600. struct ksmbd_file *fp)
  601. {
  602. return fp->tcon != tcon;
  603. }
  604. static bool ksmbd_durable_scavenger_alive(void)
  605. {
  606. if (!durable_scavenger_running)
  607. return false;
  608. if (kthread_should_stop())
  609. return false;
  610. if (idr_is_empty(global_ft.idr))
  611. return false;
  612. return true;
  613. }
  614. static void ksmbd_scavenger_dispose_dh(struct list_head *head)
  615. {
  616. while (!list_empty(head)) {
  617. struct ksmbd_file *fp;
  618. fp = list_first_entry(head, struct ksmbd_file, node);
  619. list_del_init(&fp->node);
  620. __ksmbd_close_fd(NULL, fp);
  621. }
  622. }
  623. static int ksmbd_durable_scavenger(void *dummy)
  624. {
  625. struct ksmbd_file *fp = NULL;
  626. unsigned int id;
  627. unsigned int min_timeout = 1;
  628. bool found_fp_timeout;
  629. LIST_HEAD(scavenger_list);
  630. unsigned long remaining_jiffies;
  631. __module_get(THIS_MODULE);
  632. set_freezable();
  633. while (ksmbd_durable_scavenger_alive()) {
  634. if (try_to_freeze())
  635. continue;
  636. found_fp_timeout = false;
  637. remaining_jiffies = wait_event_timeout(dh_wq,
  638. ksmbd_durable_scavenger_alive() == false,
  639. __msecs_to_jiffies(min_timeout));
  640. if (remaining_jiffies)
  641. min_timeout = jiffies_to_msecs(remaining_jiffies);
  642. else
  643. min_timeout = DURABLE_HANDLE_MAX_TIMEOUT;
  644. write_lock(&global_ft.lock);
  645. idr_for_each_entry(global_ft.idr, fp, id) {
  646. if (!fp->durable_timeout)
  647. continue;
  648. if (atomic_read(&fp->refcount) > 1 ||
  649. fp->conn)
  650. continue;
  651. found_fp_timeout = true;
  652. if (fp->durable_scavenger_timeout <=
  653. jiffies_to_msecs(jiffies)) {
  654. __ksmbd_remove_durable_fd(fp);
  655. list_add(&fp->node, &scavenger_list);
  656. } else {
  657. unsigned long durable_timeout;
  658. durable_timeout =
  659. fp->durable_scavenger_timeout -
  660. jiffies_to_msecs(jiffies);
  661. if (min_timeout > durable_timeout)
  662. min_timeout = durable_timeout;
  663. }
  664. }
  665. write_unlock(&global_ft.lock);
  666. ksmbd_scavenger_dispose_dh(&scavenger_list);
  667. if (found_fp_timeout == false)
  668. break;
  669. }
  670. durable_scavenger_running = false;
  671. module_put(THIS_MODULE);
  672. return 0;
  673. }
  674. void ksmbd_launch_ksmbd_durable_scavenger(void)
  675. {
  676. if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE))
  677. return;
  678. mutex_lock(&durable_scavenger_lock);
  679. if (durable_scavenger_running == true) {
  680. mutex_unlock(&durable_scavenger_lock);
  681. return;
  682. }
  683. durable_scavenger_running = true;
  684. server_conf.dh_task = kthread_run(ksmbd_durable_scavenger,
  685. (void *)NULL, "ksmbd-durable-scavenger");
  686. if (IS_ERR(server_conf.dh_task))
  687. pr_err("cannot start conn thread, err : %ld\n",
  688. PTR_ERR(server_conf.dh_task));
  689. mutex_unlock(&durable_scavenger_lock);
  690. }
  691. void ksmbd_stop_durable_scavenger(void)
  692. {
  693. if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE))
  694. return;
  695. mutex_lock(&durable_scavenger_lock);
  696. if (!durable_scavenger_running) {
  697. mutex_unlock(&durable_scavenger_lock);
  698. return;
  699. }
  700. durable_scavenger_running = false;
  701. if (waitqueue_active(&dh_wq))
  702. wake_up(&dh_wq);
  703. mutex_unlock(&durable_scavenger_lock);
  704. kthread_stop(server_conf.dh_task);
  705. }
  706. static bool session_fd_check(struct ksmbd_tree_connect *tcon,
  707. struct ksmbd_file *fp)
  708. {
  709. struct ksmbd_inode *ci;
  710. struct oplock_info *op;
  711. struct ksmbd_conn *conn;
  712. if (!is_reconnectable(fp))
  713. return false;
  714. conn = fp->conn;
  715. ci = fp->f_ci;
  716. down_write(&ci->m_lock);
  717. list_for_each_entry_rcu(op, &ci->m_op_list, op_entry) {
  718. if (op->conn != conn)
  719. continue;
  720. if (op->conn && atomic_dec_and_test(&op->conn->refcnt))
  721. kfree(op->conn);
  722. op->conn = NULL;
  723. }
  724. up_write(&ci->m_lock);
  725. fp->conn = NULL;
  726. fp->tcon = NULL;
  727. fp->volatile_id = KSMBD_NO_FID;
  728. if (fp->durable_timeout)
  729. fp->durable_scavenger_timeout =
  730. jiffies_to_msecs(jiffies) + fp->durable_timeout;
  731. return true;
  732. }
  733. void ksmbd_close_tree_conn_fds(struct ksmbd_work *work)
  734. {
  735. int num = __close_file_table_ids(&work->sess->file_table,
  736. work->tcon,
  737. tree_conn_fd_check);
  738. atomic_sub(num, &work->conn->stats.open_files_count);
  739. }
  740. void ksmbd_close_session_fds(struct ksmbd_work *work)
  741. {
  742. int num = __close_file_table_ids(&work->sess->file_table,
  743. work->tcon,
  744. session_fd_check);
  745. atomic_sub(num, &work->conn->stats.open_files_count);
  746. }
  747. int ksmbd_init_global_file_table(void)
  748. {
  749. return ksmbd_init_file_table(&global_ft);
  750. }
  751. void ksmbd_free_global_file_table(void)
  752. {
  753. struct ksmbd_file *fp = NULL;
  754. unsigned int id;
  755. idr_for_each_entry(global_ft.idr, fp, id) {
  756. ksmbd_remove_durable_fd(fp);
  757. __ksmbd_close_fd(NULL, fp);
  758. }
  759. idr_destroy(global_ft.idr);
  760. kfree(global_ft.idr);
  761. }
  762. int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
  763. struct ksmbd_file *fp, char *name)
  764. {
  765. char *pathname, *ab_pathname;
  766. int ret = 0;
  767. pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
  768. if (!pathname)
  769. return -EACCES;
  770. ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
  771. if (IS_ERR(ab_pathname)) {
  772. kfree(pathname);
  773. return -EACCES;
  774. }
  775. if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) {
  776. ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
  777. ret = -EINVAL;
  778. }
  779. kfree(pathname);
  780. return ret;
  781. }
  782. int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp)
  783. {
  784. struct ksmbd_inode *ci;
  785. struct oplock_info *op;
  786. if (!fp->is_durable || fp->conn || fp->tcon) {
  787. pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon);
  788. return -EBADF;
  789. }
  790. if (has_file_id(fp->volatile_id)) {
  791. pr_err("Still in use durable fd: %llu\n", fp->volatile_id);
  792. return -EBADF;
  793. }
  794. fp->conn = work->conn;
  795. fp->tcon = work->tcon;
  796. ci = fp->f_ci;
  797. down_write(&ci->m_lock);
  798. list_for_each_entry_rcu(op, &ci->m_op_list, op_entry) {
  799. if (op->conn)
  800. continue;
  801. op->conn = fp->conn;
  802. atomic_inc(&op->conn->refcnt);
  803. }
  804. up_write(&ci->m_lock);
  805. fp->f_state = FP_NEW;
  806. __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
  807. if (!has_file_id(fp->volatile_id)) {
  808. fp->conn = NULL;
  809. fp->tcon = NULL;
  810. return -EBADF;
  811. }
  812. return 0;
  813. }
  814. int ksmbd_init_file_table(struct ksmbd_file_table *ft)
  815. {
  816. ft->idr = kzalloc(sizeof(struct idr), KSMBD_DEFAULT_GFP);
  817. if (!ft->idr)
  818. return -ENOMEM;
  819. idr_init(ft->idr);
  820. rwlock_init(&ft->lock);
  821. return 0;
  822. }
  823. void ksmbd_destroy_file_table(struct ksmbd_file_table *ft)
  824. {
  825. if (!ft->idr)
  826. return;
  827. __close_file_table_ids(ft, NULL, session_fd_check);
  828. idr_destroy(ft->idr);
  829. kfree(ft->idr);
  830. ft->idr = NULL;
  831. }
  832. int ksmbd_init_file_cache(void)
  833. {
  834. filp_cache = kmem_cache_create("ksmbd_file_cache",
  835. sizeof(struct ksmbd_file), 0,
  836. SLAB_HWCACHE_ALIGN, NULL);
  837. if (!filp_cache)
  838. goto out;
  839. init_waitqueue_head(&dh_wq);
  840. return 0;
  841. out:
  842. pr_err("failed to allocate file cache\n");
  843. return -ENOMEM;
  844. }
  845. void ksmbd_exit_file_cache(void)
  846. {
  847. kmem_cache_destroy(filp_cache);
  848. }