oplock.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
  4. * Copyright (C) 2018 Samsung Electronics Co., Ltd.
  5. */
  6. #include <linux/moduleparam.h>
  7. #include "glob.h"
  8. #include "oplock.h"
  9. #include "smb_common.h"
  10. #include "../common/smb2status.h"
  11. #include "connection.h"
  12. #include "mgmt/user_session.h"
  13. #include "mgmt/share_config.h"
  14. #include "mgmt/tree_connect.h"
  15. static LIST_HEAD(lease_table_list);
  16. static DEFINE_RWLOCK(lease_list_lock);
  17. /**
  18. * alloc_opinfo() - allocate a new opinfo object for oplock info
  19. * @work: smb work
  20. * @id: fid of open file
  21. * @Tid: tree id of connection
  22. *
  23. * Return: allocated opinfo object on success, otherwise NULL
  24. */
  25. static struct oplock_info *alloc_opinfo(struct ksmbd_work *work,
  26. u64 id, __u16 Tid)
  27. {
  28. struct ksmbd_conn *conn = work->conn;
  29. struct ksmbd_session *sess = work->sess;
  30. struct oplock_info *opinfo;
  31. opinfo = kzalloc(sizeof(struct oplock_info), KSMBD_DEFAULT_GFP);
  32. if (!opinfo)
  33. return NULL;
  34. opinfo->sess = sess;
  35. opinfo->conn = conn;
  36. opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
  37. opinfo->op_state = OPLOCK_STATE_NONE;
  38. opinfo->pending_break = 0;
  39. opinfo->fid = id;
  40. opinfo->Tid = Tid;
  41. INIT_LIST_HEAD(&opinfo->op_entry);
  42. init_waitqueue_head(&opinfo->oplock_q);
  43. init_waitqueue_head(&opinfo->oplock_brk);
  44. atomic_set(&opinfo->refcount, 1);
  45. atomic_set(&opinfo->breaking_cnt, 0);
  46. atomic_inc(&opinfo->conn->refcnt);
  47. return opinfo;
  48. }
  49. static void lease_add_list(struct oplock_info *opinfo)
  50. {
  51. struct lease_table *lb = opinfo->o_lease->l_lb;
  52. spin_lock(&lb->lb_lock);
  53. list_add_rcu(&opinfo->lease_entry, &lb->lease_list);
  54. spin_unlock(&lb->lb_lock);
  55. }
  56. static void lease_del_list(struct oplock_info *opinfo)
  57. {
  58. struct lease_table *lb = opinfo->o_lease->l_lb;
  59. if (!lb)
  60. return;
  61. spin_lock(&lb->lb_lock);
  62. if (list_empty(&opinfo->lease_entry)) {
  63. spin_unlock(&lb->lb_lock);
  64. return;
  65. }
  66. list_del_init(&opinfo->lease_entry);
  67. opinfo->o_lease->l_lb = NULL;
  68. spin_unlock(&lb->lb_lock);
  69. }
  70. static void lb_add(struct lease_table *lb)
  71. {
  72. write_lock(&lease_list_lock);
  73. list_add(&lb->l_entry, &lease_table_list);
  74. write_unlock(&lease_list_lock);
  75. }
  76. static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx)
  77. {
  78. struct lease *lease;
  79. lease = kmalloc(sizeof(struct lease), KSMBD_DEFAULT_GFP);
  80. if (!lease)
  81. return -ENOMEM;
  82. memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
  83. lease->state = lctx->req_state;
  84. lease->new_state = 0;
  85. lease->flags = lctx->flags;
  86. lease->duration = lctx->duration;
  87. lease->is_dir = lctx->is_dir;
  88. memcpy(lease->parent_lease_key, lctx->parent_lease_key, SMB2_LEASE_KEY_SIZE);
  89. lease->version = lctx->version;
  90. lease->epoch = le16_to_cpu(lctx->epoch) + 1;
  91. INIT_LIST_HEAD(&opinfo->lease_entry);
  92. opinfo->o_lease = lease;
  93. return 0;
  94. }
  95. static void free_lease(struct oplock_info *opinfo)
  96. {
  97. struct lease *lease;
  98. lease = opinfo->o_lease;
  99. kfree(lease);
  100. }
  101. static void free_opinfo(struct oplock_info *opinfo)
  102. {
  103. if (opinfo->is_lease)
  104. free_lease(opinfo);
  105. if (opinfo->conn && atomic_dec_and_test(&opinfo->conn->refcnt))
  106. kfree(opinfo->conn);
  107. kfree(opinfo);
  108. }
  109. struct oplock_info *opinfo_get(struct ksmbd_file *fp)
  110. {
  111. struct oplock_info *opinfo;
  112. rcu_read_lock();
  113. opinfo = rcu_dereference(fp->f_opinfo);
  114. if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
  115. opinfo = NULL;
  116. rcu_read_unlock();
  117. return opinfo;
  118. }
  119. static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci)
  120. {
  121. struct oplock_info *opinfo;
  122. down_read(&ci->m_lock);
  123. opinfo = list_first_entry_or_null(&ci->m_op_list, struct oplock_info,
  124. op_entry);
  125. if (opinfo) {
  126. if (opinfo->conn == NULL ||
  127. !atomic_inc_not_zero(&opinfo->refcount))
  128. opinfo = NULL;
  129. else {
  130. if (ksmbd_conn_releasing(opinfo->conn)) {
  131. atomic_dec(&opinfo->refcount);
  132. opinfo = NULL;
  133. }
  134. }
  135. }
  136. up_read(&ci->m_lock);
  137. return opinfo;
  138. }
  139. void opinfo_put(struct oplock_info *opinfo)
  140. {
  141. if (!opinfo)
  142. return;
  143. if (!atomic_dec_and_test(&opinfo->refcount))
  144. return;
  145. free_opinfo(opinfo);
  146. }
  147. static void opinfo_add(struct oplock_info *opinfo)
  148. {
  149. struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
  150. down_write(&ci->m_lock);
  151. list_add(&opinfo->op_entry, &ci->m_op_list);
  152. up_write(&ci->m_lock);
  153. }
  154. static void opinfo_del(struct oplock_info *opinfo)
  155. {
  156. struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
  157. if (opinfo->is_lease) {
  158. write_lock(&lease_list_lock);
  159. lease_del_list(opinfo);
  160. write_unlock(&lease_list_lock);
  161. }
  162. down_write(&ci->m_lock);
  163. list_del(&opinfo->op_entry);
  164. up_write(&ci->m_lock);
  165. }
  166. static unsigned long opinfo_count(struct ksmbd_file *fp)
  167. {
  168. if (ksmbd_stream_fd(fp))
  169. return atomic_read(&fp->f_ci->sop_count);
  170. else
  171. return atomic_read(&fp->f_ci->op_count);
  172. }
  173. static void opinfo_count_inc(struct ksmbd_file *fp)
  174. {
  175. if (ksmbd_stream_fd(fp))
  176. return atomic_inc(&fp->f_ci->sop_count);
  177. else
  178. return atomic_inc(&fp->f_ci->op_count);
  179. }
  180. static void opinfo_count_dec(struct ksmbd_file *fp)
  181. {
  182. if (ksmbd_stream_fd(fp))
  183. return atomic_dec(&fp->f_ci->sop_count);
  184. else
  185. return atomic_dec(&fp->f_ci->op_count);
  186. }
  187. /**
  188. * opinfo_write_to_read() - convert a write oplock to read oplock
  189. * @opinfo: current oplock info
  190. *
  191. * Return: 0 on success, otherwise -EINVAL
  192. */
  193. int opinfo_write_to_read(struct oplock_info *opinfo)
  194. {
  195. struct lease *lease = opinfo->o_lease;
  196. if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
  197. opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
  198. pr_err("bad oplock(0x%x)\n", opinfo->level);
  199. if (opinfo->is_lease)
  200. pr_err("lease state(0x%x)\n", lease->state);
  201. return -EINVAL;
  202. }
  203. opinfo->level = SMB2_OPLOCK_LEVEL_II;
  204. if (opinfo->is_lease)
  205. lease->state = lease->new_state;
  206. return 0;
  207. }
  208. /**
  209. * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock
  210. * @opinfo: current oplock info
  211. *
  212. * Return: 0 on success, otherwise -EINVAL
  213. */
  214. int opinfo_read_handle_to_read(struct oplock_info *opinfo)
  215. {
  216. struct lease *lease = opinfo->o_lease;
  217. lease->state = lease->new_state;
  218. opinfo->level = SMB2_OPLOCK_LEVEL_II;
  219. return 0;
  220. }
  221. /**
  222. * opinfo_write_to_none() - convert a write oplock to none
  223. * @opinfo: current oplock info
  224. *
  225. * Return: 0 on success, otherwise -EINVAL
  226. */
  227. int opinfo_write_to_none(struct oplock_info *opinfo)
  228. {
  229. struct lease *lease = opinfo->o_lease;
  230. if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
  231. opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
  232. pr_err("bad oplock(0x%x)\n", opinfo->level);
  233. if (opinfo->is_lease)
  234. pr_err("lease state(0x%x)\n", lease->state);
  235. return -EINVAL;
  236. }
  237. opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
  238. if (opinfo->is_lease)
  239. lease->state = lease->new_state;
  240. return 0;
  241. }
  242. /**
  243. * opinfo_read_to_none() - convert a write read to none
  244. * @opinfo: current oplock info
  245. *
  246. * Return: 0 on success, otherwise -EINVAL
  247. */
  248. int opinfo_read_to_none(struct oplock_info *opinfo)
  249. {
  250. struct lease *lease = opinfo->o_lease;
  251. if (opinfo->level != SMB2_OPLOCK_LEVEL_II) {
  252. pr_err("bad oplock(0x%x)\n", opinfo->level);
  253. if (opinfo->is_lease)
  254. pr_err("lease state(0x%x)\n", lease->state);
  255. return -EINVAL;
  256. }
  257. opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
  258. if (opinfo->is_lease)
  259. lease->state = lease->new_state;
  260. return 0;
  261. }
  262. /**
  263. * lease_read_to_write() - upgrade lease state from read to write
  264. * @opinfo: current lease info
  265. *
  266. * Return: 0 on success, otherwise -EINVAL
  267. */
  268. int lease_read_to_write(struct oplock_info *opinfo)
  269. {
  270. struct lease *lease = opinfo->o_lease;
  271. if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) {
  272. ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
  273. return -EINVAL;
  274. }
  275. lease->new_state = SMB2_LEASE_NONE_LE;
  276. lease->state |= SMB2_LEASE_WRITE_CACHING_LE;
  277. if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
  278. opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
  279. else
  280. opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
  281. return 0;
  282. }
  283. /**
  284. * lease_none_upgrade() - upgrade lease state from none
  285. * @opinfo: current lease info
  286. * @new_state: new lease state
  287. *
  288. * Return: 0 on success, otherwise -EINVAL
  289. */
  290. static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state)
  291. {
  292. struct lease *lease = opinfo->o_lease;
  293. if (!(lease->state == SMB2_LEASE_NONE_LE)) {
  294. ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
  295. return -EINVAL;
  296. }
  297. lease->new_state = SMB2_LEASE_NONE_LE;
  298. lease->state = new_state;
  299. if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
  300. if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
  301. opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
  302. else
  303. opinfo->level = SMB2_OPLOCK_LEVEL_II;
  304. else if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
  305. opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
  306. else if (lease->state & SMB2_LEASE_READ_CACHING_LE)
  307. opinfo->level = SMB2_OPLOCK_LEVEL_II;
  308. return 0;
  309. }
  310. /**
  311. * close_id_del_oplock() - release oplock object at file close time
  312. * @fp: ksmbd file pointer
  313. */
  314. void close_id_del_oplock(struct ksmbd_file *fp)
  315. {
  316. struct oplock_info *opinfo;
  317. if (fp->reserve_lease_break)
  318. smb_lazy_parent_lease_break_close(fp);
  319. opinfo = opinfo_get(fp);
  320. if (!opinfo)
  321. return;
  322. opinfo_del(opinfo);
  323. rcu_assign_pointer(fp->f_opinfo, NULL);
  324. if (opinfo->op_state == OPLOCK_ACK_WAIT) {
  325. opinfo->op_state = OPLOCK_CLOSING;
  326. wake_up_interruptible_all(&opinfo->oplock_q);
  327. if (opinfo->is_lease) {
  328. atomic_set(&opinfo->breaking_cnt, 0);
  329. wake_up_interruptible_all(&opinfo->oplock_brk);
  330. }
  331. }
  332. opinfo_count_dec(fp);
  333. atomic_dec(&opinfo->refcount);
  334. opinfo_put(opinfo);
  335. }
  336. /**
  337. * grant_write_oplock() - grant exclusive/batch oplock or write lease
  338. * @opinfo_new: new oplock info object
  339. * @req_oplock: request oplock
  340. * @lctx: lease context information
  341. *
  342. * Return: 0
  343. */
  344. static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock,
  345. struct lease_ctx_info *lctx)
  346. {
  347. struct lease *lease = opinfo_new->o_lease;
  348. if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH)
  349. opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH;
  350. else
  351. opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
  352. if (lctx) {
  353. lease->state = lctx->req_state;
  354. memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
  355. }
  356. }
  357. /**
  358. * grant_read_oplock() - grant level2 oplock or read lease
  359. * @opinfo_new: new oplock info object
  360. * @lctx: lease context information
  361. *
  362. * Return: 0
  363. */
  364. static void grant_read_oplock(struct oplock_info *opinfo_new,
  365. struct lease_ctx_info *lctx)
  366. {
  367. struct lease *lease = opinfo_new->o_lease;
  368. opinfo_new->level = SMB2_OPLOCK_LEVEL_II;
  369. if (lctx) {
  370. lease->state = SMB2_LEASE_READ_CACHING_LE;
  371. if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE)
  372. lease->state |= SMB2_LEASE_HANDLE_CACHING_LE;
  373. memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
  374. }
  375. }
  376. /**
  377. * grant_none_oplock() - grant none oplock or none lease
  378. * @opinfo_new: new oplock info object
  379. * @lctx: lease context information
  380. *
  381. * Return: 0
  382. */
  383. static void grant_none_oplock(struct oplock_info *opinfo_new,
  384. struct lease_ctx_info *lctx)
  385. {
  386. struct lease *lease = opinfo_new->o_lease;
  387. opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE;
  388. if (lctx) {
  389. lease->state = 0;
  390. memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
  391. }
  392. }
  393. static inline int compare_guid_key(struct oplock_info *opinfo,
  394. const char *guid1, const char *key1)
  395. {
  396. const char *guid2, *key2;
  397. guid2 = opinfo->conn->ClientGUID;
  398. key2 = opinfo->o_lease->lease_key;
  399. if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) &&
  400. !memcmp(key1, key2, SMB2_LEASE_KEY_SIZE))
  401. return 1;
  402. return 0;
  403. }
  404. /**
  405. * same_client_has_lease() - check whether current lease request is
  406. * from lease owner of file
  407. * @ci: master file pointer
  408. * @client_guid: Client GUID
  409. * @lctx: lease context information
  410. *
  411. * Return: oplock(lease) object on success, otherwise NULL
  412. */
  413. static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci,
  414. char *client_guid,
  415. struct lease_ctx_info *lctx)
  416. {
  417. int ret;
  418. struct lease *lease;
  419. struct oplock_info *opinfo;
  420. struct oplock_info *m_opinfo = NULL;
  421. if (!lctx)
  422. return NULL;
  423. /*
  424. * Compare lease key and client_guid to know request from same owner
  425. * of same client
  426. */
  427. down_read(&ci->m_lock);
  428. list_for_each_entry(opinfo, &ci->m_op_list, op_entry) {
  429. if (!opinfo->is_lease || !opinfo->conn)
  430. continue;
  431. lease = opinfo->o_lease;
  432. ret = compare_guid_key(opinfo, client_guid, lctx->lease_key);
  433. if (ret) {
  434. m_opinfo = opinfo;
  435. /* skip upgrading lease about breaking lease */
  436. if (atomic_read(&opinfo->breaking_cnt))
  437. continue;
  438. /* upgrading lease */
  439. if ((atomic_read(&ci->op_count) +
  440. atomic_read(&ci->sop_count)) == 1) {
  441. if (lease->state != SMB2_LEASE_NONE_LE &&
  442. lease->state == (lctx->req_state & lease->state)) {
  443. lease->epoch++;
  444. lease->state |= lctx->req_state;
  445. if (lctx->req_state &
  446. SMB2_LEASE_WRITE_CACHING_LE)
  447. lease_read_to_write(opinfo);
  448. }
  449. } else if ((atomic_read(&ci->op_count) +
  450. atomic_read(&ci->sop_count)) > 1) {
  451. if (lctx->req_state ==
  452. (SMB2_LEASE_READ_CACHING_LE |
  453. SMB2_LEASE_HANDLE_CACHING_LE)) {
  454. lease->epoch++;
  455. lease->state = lctx->req_state;
  456. }
  457. }
  458. if (lctx->req_state && lease->state ==
  459. SMB2_LEASE_NONE_LE) {
  460. lease->epoch++;
  461. lease_none_upgrade(opinfo, lctx->req_state);
  462. }
  463. }
  464. }
  465. up_read(&ci->m_lock);
  466. return m_opinfo;
  467. }
  468. static void wait_for_break_ack(struct oplock_info *opinfo)
  469. {
  470. int rc = 0;
  471. rc = wait_event_interruptible_timeout(opinfo->oplock_q,
  472. opinfo->op_state == OPLOCK_STATE_NONE ||
  473. opinfo->op_state == OPLOCK_CLOSING,
  474. OPLOCK_WAIT_TIME);
  475. /* is this a timeout ? */
  476. if (!rc) {
  477. if (opinfo->is_lease)
  478. opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
  479. opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
  480. opinfo->op_state = OPLOCK_STATE_NONE;
  481. }
  482. }
  483. static void wake_up_oplock_break(struct oplock_info *opinfo)
  484. {
  485. clear_bit_unlock(0, &opinfo->pending_break);
  486. /* memory barrier is needed for wake_up_bit() */
  487. smp_mb__after_atomic();
  488. wake_up_bit(&opinfo->pending_break, 0);
  489. }
  490. static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level)
  491. {
  492. while (test_and_set_bit(0, &opinfo->pending_break)) {
  493. wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE);
  494. /* Not immediately break to none. */
  495. opinfo->open_trunc = 0;
  496. if (opinfo->op_state == OPLOCK_CLOSING)
  497. return -ENOENT;
  498. else if (opinfo->level <= req_op_level) {
  499. if (opinfo->is_lease == false)
  500. return 1;
  501. if (opinfo->o_lease->state !=
  502. (SMB2_LEASE_HANDLE_CACHING_LE |
  503. SMB2_LEASE_READ_CACHING_LE))
  504. return 1;
  505. }
  506. }
  507. if (opinfo->level <= req_op_level) {
  508. if (opinfo->is_lease == false) {
  509. wake_up_oplock_break(opinfo);
  510. return 1;
  511. }
  512. if (opinfo->o_lease->state !=
  513. (SMB2_LEASE_HANDLE_CACHING_LE |
  514. SMB2_LEASE_READ_CACHING_LE)) {
  515. wake_up_oplock_break(opinfo);
  516. return 1;
  517. }
  518. }
  519. return 0;
  520. }
  521. /**
  522. * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn
  523. * to client
  524. * @wk: smb work object
  525. *
  526. * There are two ways this function can be called. 1- while file open we break
  527. * from exclusive/batch lock to levelII oplock and 2- while file write/truncate
  528. * we break from levelII oplock no oplock.
  529. * work->request_buf contains oplock_info.
  530. */
  531. static void __smb2_oplock_break_noti(struct work_struct *wk)
  532. {
  533. struct smb2_oplock_break *rsp = NULL;
  534. struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
  535. struct ksmbd_conn *conn = work->conn;
  536. struct oplock_break_info *br_info = work->request_buf;
  537. struct smb2_hdr *rsp_hdr;
  538. struct ksmbd_file *fp;
  539. fp = ksmbd_lookup_global_fd(br_info->fid);
  540. if (!fp)
  541. goto out;
  542. if (allocate_interim_rsp_buf(work)) {
  543. pr_err("smb2_allocate_rsp_buf failed! ");
  544. ksmbd_fd_put(work, fp);
  545. goto out;
  546. }
  547. rsp_hdr = smb2_get_msg(work->response_buf);
  548. memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
  549. rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
  550. rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
  551. rsp_hdr->CreditRequest = cpu_to_le16(0);
  552. rsp_hdr->Command = SMB2_OPLOCK_BREAK;
  553. rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
  554. rsp_hdr->NextCommand = 0;
  555. rsp_hdr->MessageId = cpu_to_le64(-1);
  556. rsp_hdr->Id.SyncId.ProcessId = 0;
  557. rsp_hdr->Id.SyncId.TreeId = 0;
  558. rsp_hdr->SessionId = 0;
  559. memset(rsp_hdr->Signature, 0, 16);
  560. rsp = smb2_get_msg(work->response_buf);
  561. rsp->StructureSize = cpu_to_le16(24);
  562. if (!br_info->open_trunc &&
  563. (br_info->level == SMB2_OPLOCK_LEVEL_BATCH ||
  564. br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
  565. rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II;
  566. else
  567. rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
  568. rsp->Reserved = 0;
  569. rsp->Reserved2 = 0;
  570. rsp->PersistentFid = fp->persistent_id;
  571. rsp->VolatileFid = fp->volatile_id;
  572. ksmbd_fd_put(work, fp);
  573. if (ksmbd_iov_pin_rsp(work, (void *)rsp,
  574. sizeof(struct smb2_oplock_break)))
  575. goto out;
  576. ksmbd_debug(OPLOCK,
  577. "sending oplock break v_id %llu p_id = %llu lock level = %d\n",
  578. rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel);
  579. ksmbd_conn_write(work);
  580. out:
  581. ksmbd_free_work_struct(work);
  582. ksmbd_conn_r_count_dec(conn);
  583. }
  584. /**
  585. * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock
  586. * break command from server to client
  587. * @opinfo: oplock info object
  588. *
  589. * Return: 0 on success, otherwise error
  590. */
  591. static int smb2_oplock_break_noti(struct oplock_info *opinfo)
  592. {
  593. struct ksmbd_conn *conn = opinfo->conn;
  594. struct oplock_break_info *br_info;
  595. int ret = 0;
  596. struct ksmbd_work *work = ksmbd_alloc_work_struct();
  597. if (!work)
  598. return -ENOMEM;
  599. br_info = kmalloc(sizeof(struct oplock_break_info), KSMBD_DEFAULT_GFP);
  600. if (!br_info) {
  601. ksmbd_free_work_struct(work);
  602. return -ENOMEM;
  603. }
  604. br_info->level = opinfo->level;
  605. br_info->fid = opinfo->fid;
  606. br_info->open_trunc = opinfo->open_trunc;
  607. work->request_buf = (char *)br_info;
  608. work->conn = conn;
  609. work->sess = opinfo->sess;
  610. ksmbd_conn_r_count_inc(conn);
  611. if (opinfo->op_state == OPLOCK_ACK_WAIT) {
  612. INIT_WORK(&work->work, __smb2_oplock_break_noti);
  613. ksmbd_queue_work(work);
  614. wait_for_break_ack(opinfo);
  615. } else {
  616. __smb2_oplock_break_noti(&work->work);
  617. if (opinfo->level == SMB2_OPLOCK_LEVEL_II)
  618. opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
  619. }
  620. return ret;
  621. }
  622. /**
  623. * __smb2_lease_break_noti() - send lease break command from server
  624. * to client
  625. * @wk: smb work object
  626. */
  627. static void __smb2_lease_break_noti(struct work_struct *wk)
  628. {
  629. struct smb2_lease_break *rsp = NULL;
  630. struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
  631. struct ksmbd_conn *conn = work->conn;
  632. struct lease_break_info *br_info = work->request_buf;
  633. struct smb2_hdr *rsp_hdr;
  634. if (allocate_interim_rsp_buf(work)) {
  635. ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! ");
  636. goto out;
  637. }
  638. rsp_hdr = smb2_get_msg(work->response_buf);
  639. memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
  640. rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
  641. rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
  642. rsp_hdr->CreditRequest = cpu_to_le16(0);
  643. rsp_hdr->Command = SMB2_OPLOCK_BREAK;
  644. rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
  645. rsp_hdr->NextCommand = 0;
  646. rsp_hdr->MessageId = cpu_to_le64(-1);
  647. rsp_hdr->Id.SyncId.ProcessId = 0;
  648. rsp_hdr->Id.SyncId.TreeId = 0;
  649. rsp_hdr->SessionId = 0;
  650. memset(rsp_hdr->Signature, 0, 16);
  651. rsp = smb2_get_msg(work->response_buf);
  652. rsp->StructureSize = cpu_to_le16(44);
  653. rsp->Epoch = br_info->epoch;
  654. rsp->Flags = 0;
  655. if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE |
  656. SMB2_LEASE_HANDLE_CACHING_LE))
  657. rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
  658. memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE);
  659. rsp->CurrentLeaseState = br_info->curr_state;
  660. rsp->NewLeaseState = br_info->new_state;
  661. rsp->BreakReason = 0;
  662. rsp->AccessMaskHint = 0;
  663. rsp->ShareMaskHint = 0;
  664. if (ksmbd_iov_pin_rsp(work, (void *)rsp,
  665. sizeof(struct smb2_lease_break)))
  666. goto out;
  667. ksmbd_conn_write(work);
  668. out:
  669. ksmbd_free_work_struct(work);
  670. ksmbd_conn_r_count_dec(conn);
  671. }
  672. /**
  673. * smb2_lease_break_noti() - break lease when a new client request
  674. * write lease
  675. * @opinfo: contains lease state information
  676. *
  677. * Return: 0 on success, otherwise error
  678. */
  679. static int smb2_lease_break_noti(struct oplock_info *opinfo)
  680. {
  681. struct ksmbd_conn *conn = opinfo->conn;
  682. struct ksmbd_work *work;
  683. struct lease_break_info *br_info;
  684. struct lease *lease = opinfo->o_lease;
  685. work = ksmbd_alloc_work_struct();
  686. if (!work)
  687. return -ENOMEM;
  688. br_info = kmalloc(sizeof(struct lease_break_info), KSMBD_DEFAULT_GFP);
  689. if (!br_info) {
  690. ksmbd_free_work_struct(work);
  691. return -ENOMEM;
  692. }
  693. br_info->curr_state = lease->state;
  694. br_info->new_state = lease->new_state;
  695. if (lease->version == 2)
  696. br_info->epoch = cpu_to_le16(++lease->epoch);
  697. else
  698. br_info->epoch = 0;
  699. memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE);
  700. work->request_buf = (char *)br_info;
  701. work->conn = conn;
  702. work->sess = opinfo->sess;
  703. ksmbd_conn_r_count_inc(conn);
  704. if (opinfo->op_state == OPLOCK_ACK_WAIT) {
  705. INIT_WORK(&work->work, __smb2_lease_break_noti);
  706. ksmbd_queue_work(work);
  707. wait_for_break_ack(opinfo);
  708. } else {
  709. __smb2_lease_break_noti(&work->work);
  710. if (opinfo->o_lease->new_state == SMB2_LEASE_NONE_LE) {
  711. opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
  712. opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
  713. }
  714. }
  715. return 0;
  716. }
  717. static void wait_lease_breaking(struct oplock_info *opinfo)
  718. {
  719. if (!opinfo->is_lease)
  720. return;
  721. wake_up_interruptible_all(&opinfo->oplock_brk);
  722. if (atomic_read(&opinfo->breaking_cnt)) {
  723. int ret = 0;
  724. ret = wait_event_interruptible_timeout(opinfo->oplock_brk,
  725. atomic_read(&opinfo->breaking_cnt) == 0,
  726. HZ);
  727. if (!ret)
  728. atomic_set(&opinfo->breaking_cnt, 0);
  729. }
  730. }
  731. static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level,
  732. struct ksmbd_work *in_work)
  733. {
  734. int err = 0;
  735. /* Need to break exclusive/batch oplock, write lease or overwrite_if */
  736. ksmbd_debug(OPLOCK,
  737. "request to send oplock(level : 0x%x) break notification\n",
  738. brk_opinfo->level);
  739. if (brk_opinfo->is_lease) {
  740. struct lease *lease = brk_opinfo->o_lease;
  741. atomic_inc(&brk_opinfo->breaking_cnt);
  742. err = oplock_break_pending(brk_opinfo, req_op_level);
  743. if (err)
  744. return err < 0 ? err : 0;
  745. if (brk_opinfo->open_trunc) {
  746. /*
  747. * Create overwrite break trigger the lease break to
  748. * none.
  749. */
  750. lease->new_state = SMB2_LEASE_NONE_LE;
  751. } else {
  752. if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) {
  753. if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
  754. lease->new_state =
  755. SMB2_LEASE_READ_CACHING_LE |
  756. SMB2_LEASE_HANDLE_CACHING_LE;
  757. else
  758. lease->new_state =
  759. SMB2_LEASE_READ_CACHING_LE;
  760. } else {
  761. if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE &&
  762. !lease->is_dir)
  763. lease->new_state =
  764. SMB2_LEASE_READ_CACHING_LE;
  765. else
  766. lease->new_state = SMB2_LEASE_NONE_LE;
  767. }
  768. }
  769. if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE |
  770. SMB2_LEASE_HANDLE_CACHING_LE)) {
  771. if (in_work) {
  772. setup_async_work(in_work, NULL, NULL);
  773. smb2_send_interim_resp(in_work, STATUS_PENDING);
  774. release_async_work(in_work);
  775. }
  776. brk_opinfo->op_state = OPLOCK_ACK_WAIT;
  777. } else
  778. atomic_dec(&brk_opinfo->breaking_cnt);
  779. } else {
  780. err = oplock_break_pending(brk_opinfo, req_op_level);
  781. if (err)
  782. return err < 0 ? err : 0;
  783. if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
  784. brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  785. brk_opinfo->op_state = OPLOCK_ACK_WAIT;
  786. }
  787. if (brk_opinfo->is_lease)
  788. err = smb2_lease_break_noti(brk_opinfo);
  789. else
  790. err = smb2_oplock_break_noti(brk_opinfo);
  791. ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level);
  792. if (brk_opinfo->op_state == OPLOCK_CLOSING)
  793. err = -ENOENT;
  794. wake_up_oplock_break(brk_opinfo);
  795. wait_lease_breaking(brk_opinfo);
  796. return err;
  797. }
  798. void destroy_lease_table(struct ksmbd_conn *conn)
  799. {
  800. struct lease_table *lb, *lbtmp;
  801. struct oplock_info *opinfo;
  802. write_lock(&lease_list_lock);
  803. if (list_empty(&lease_table_list)) {
  804. write_unlock(&lease_list_lock);
  805. return;
  806. }
  807. list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) {
  808. if (conn && memcmp(lb->client_guid, conn->ClientGUID,
  809. SMB2_CLIENT_GUID_SIZE))
  810. continue;
  811. again:
  812. rcu_read_lock();
  813. list_for_each_entry_rcu(opinfo, &lb->lease_list,
  814. lease_entry) {
  815. rcu_read_unlock();
  816. lease_del_list(opinfo);
  817. goto again;
  818. }
  819. rcu_read_unlock();
  820. list_del(&lb->l_entry);
  821. kfree(lb);
  822. }
  823. write_unlock(&lease_list_lock);
  824. }
  825. int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,
  826. struct lease_ctx_info *lctx)
  827. {
  828. struct oplock_info *opinfo;
  829. int err = 0;
  830. struct lease_table *lb;
  831. if (!lctx)
  832. return err;
  833. read_lock(&lease_list_lock);
  834. if (list_empty(&lease_table_list)) {
  835. read_unlock(&lease_list_lock);
  836. return 0;
  837. }
  838. list_for_each_entry(lb, &lease_table_list, l_entry) {
  839. if (!memcmp(lb->client_guid, sess->ClientGUID,
  840. SMB2_CLIENT_GUID_SIZE))
  841. goto found;
  842. }
  843. read_unlock(&lease_list_lock);
  844. return 0;
  845. found:
  846. rcu_read_lock();
  847. list_for_each_entry_rcu(opinfo, &lb->lease_list, lease_entry) {
  848. if (!atomic_inc_not_zero(&opinfo->refcount))
  849. continue;
  850. rcu_read_unlock();
  851. if (opinfo->o_fp->f_ci == ci)
  852. goto op_next;
  853. err = compare_guid_key(opinfo, sess->ClientGUID,
  854. lctx->lease_key);
  855. if (err) {
  856. err = -EINVAL;
  857. ksmbd_debug(OPLOCK,
  858. "found same lease key is already used in other files\n");
  859. opinfo_put(opinfo);
  860. goto out;
  861. }
  862. op_next:
  863. opinfo_put(opinfo);
  864. rcu_read_lock();
  865. }
  866. rcu_read_unlock();
  867. out:
  868. read_unlock(&lease_list_lock);
  869. return err;
  870. }
  871. static void copy_lease(struct oplock_info *op1, struct oplock_info *op2)
  872. {
  873. struct lease *lease1 = op1->o_lease;
  874. struct lease *lease2 = op2->o_lease;
  875. op2->level = op1->level;
  876. lease2->state = lease1->state;
  877. memcpy(lease2->lease_key, lease1->lease_key,
  878. SMB2_LEASE_KEY_SIZE);
  879. lease2->duration = lease1->duration;
  880. lease2->flags = lease1->flags;
  881. lease2->epoch = lease1->epoch;
  882. lease2->version = lease1->version;
  883. }
  884. static int add_lease_global_list(struct oplock_info *opinfo)
  885. {
  886. struct lease_table *lb;
  887. read_lock(&lease_list_lock);
  888. list_for_each_entry(lb, &lease_table_list, l_entry) {
  889. if (!memcmp(lb->client_guid, opinfo->conn->ClientGUID,
  890. SMB2_CLIENT_GUID_SIZE)) {
  891. opinfo->o_lease->l_lb = lb;
  892. lease_add_list(opinfo);
  893. read_unlock(&lease_list_lock);
  894. return 0;
  895. }
  896. }
  897. read_unlock(&lease_list_lock);
  898. lb = kmalloc(sizeof(struct lease_table), KSMBD_DEFAULT_GFP);
  899. if (!lb)
  900. return -ENOMEM;
  901. memcpy(lb->client_guid, opinfo->conn->ClientGUID,
  902. SMB2_CLIENT_GUID_SIZE);
  903. INIT_LIST_HEAD(&lb->lease_list);
  904. spin_lock_init(&lb->lb_lock);
  905. opinfo->o_lease->l_lb = lb;
  906. lease_add_list(opinfo);
  907. lb_add(lb);
  908. return 0;
  909. }
  910. static void set_oplock_level(struct oplock_info *opinfo, int level,
  911. struct lease_ctx_info *lctx)
  912. {
  913. switch (level) {
  914. case SMB2_OPLOCK_LEVEL_BATCH:
  915. case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
  916. grant_write_oplock(opinfo, level, lctx);
  917. break;
  918. case SMB2_OPLOCK_LEVEL_II:
  919. grant_read_oplock(opinfo, lctx);
  920. break;
  921. default:
  922. grant_none_oplock(opinfo, lctx);
  923. break;
  924. }
  925. }
  926. void smb_send_parent_lease_break_noti(struct ksmbd_file *fp,
  927. struct lease_ctx_info *lctx)
  928. {
  929. struct oplock_info *opinfo;
  930. struct ksmbd_inode *p_ci = NULL;
  931. if (lctx->version != 2)
  932. return;
  933. p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent);
  934. if (!p_ci)
  935. return;
  936. down_read(&p_ci->m_lock);
  937. list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) {
  938. if (opinfo->conn == NULL || !opinfo->is_lease)
  939. continue;
  940. if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE &&
  941. (!(lctx->flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) ||
  942. !compare_guid_key(opinfo, fp->conn->ClientGUID,
  943. lctx->parent_lease_key))) {
  944. if (!atomic_inc_not_zero(&opinfo->refcount))
  945. continue;
  946. if (ksmbd_conn_releasing(opinfo->conn)) {
  947. opinfo_put(opinfo);
  948. continue;
  949. }
  950. oplock_break(opinfo, SMB2_OPLOCK_LEVEL_NONE, NULL);
  951. opinfo_put(opinfo);
  952. }
  953. }
  954. up_read(&p_ci->m_lock);
  955. ksmbd_inode_put(p_ci);
  956. }
  957. void smb_lazy_parent_lease_break_close(struct ksmbd_file *fp)
  958. {
  959. struct oplock_info *opinfo;
  960. struct ksmbd_inode *p_ci = NULL;
  961. rcu_read_lock();
  962. opinfo = rcu_dereference(fp->f_opinfo);
  963. rcu_read_unlock();
  964. if (!opinfo || !opinfo->is_lease || opinfo->o_lease->version != 2)
  965. return;
  966. p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent);
  967. if (!p_ci)
  968. return;
  969. down_read(&p_ci->m_lock);
  970. list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) {
  971. if (opinfo->conn == NULL || !opinfo->is_lease)
  972. continue;
  973. if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE) {
  974. if (!atomic_inc_not_zero(&opinfo->refcount))
  975. continue;
  976. if (ksmbd_conn_releasing(opinfo->conn)) {
  977. opinfo_put(opinfo);
  978. continue;
  979. }
  980. oplock_break(opinfo, SMB2_OPLOCK_LEVEL_NONE, NULL);
  981. opinfo_put(opinfo);
  982. }
  983. }
  984. up_read(&p_ci->m_lock);
  985. ksmbd_inode_put(p_ci);
  986. }
  987. /**
  988. * smb_grant_oplock() - handle oplock/lease request on file open
  989. * @work: smb work
  990. * @req_op_level: oplock level
  991. * @pid: id of open file
  992. * @fp: ksmbd file pointer
  993. * @tid: Tree id of connection
  994. * @lctx: lease context information on file open
  995. * @share_ret: share mode
  996. *
  997. * Return: 0 on success, otherwise error
  998. */
  999. int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,
  1000. struct ksmbd_file *fp, __u16 tid,
  1001. struct lease_ctx_info *lctx, int share_ret)
  1002. {
  1003. struct ksmbd_session *sess = work->sess;
  1004. int err = 0;
  1005. struct oplock_info *opinfo = NULL, *prev_opinfo = NULL;
  1006. struct ksmbd_inode *ci = fp->f_ci;
  1007. bool prev_op_has_lease;
  1008. __le32 prev_op_state = 0;
  1009. /* Only v2 leases handle the directory */
  1010. if (S_ISDIR(file_inode(fp->filp)->i_mode)) {
  1011. if (!lctx || lctx->version != 2 ||
  1012. (lctx->flags != SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE &&
  1013. !lctx->epoch))
  1014. return 0;
  1015. }
  1016. opinfo = alloc_opinfo(work, pid, tid);
  1017. if (!opinfo)
  1018. return -ENOMEM;
  1019. if (lctx) {
  1020. err = alloc_lease(opinfo, lctx);
  1021. if (err)
  1022. goto err_out;
  1023. opinfo->is_lease = 1;
  1024. }
  1025. /* ci does not have any oplock */
  1026. if (!opinfo_count(fp))
  1027. goto set_lev;
  1028. /* grant none-oplock if second open is trunc */
  1029. if (fp->attrib_only && fp->cdoption != FILE_OVERWRITE_IF_LE &&
  1030. fp->cdoption != FILE_OVERWRITE_LE &&
  1031. fp->cdoption != FILE_SUPERSEDE_LE) {
  1032. req_op_level = SMB2_OPLOCK_LEVEL_NONE;
  1033. goto set_lev;
  1034. }
  1035. if (lctx) {
  1036. struct oplock_info *m_opinfo;
  1037. /* is lease already granted ? */
  1038. m_opinfo = same_client_has_lease(ci, sess->ClientGUID,
  1039. lctx);
  1040. if (m_opinfo) {
  1041. copy_lease(m_opinfo, opinfo);
  1042. if (atomic_read(&m_opinfo->breaking_cnt))
  1043. opinfo->o_lease->flags =
  1044. SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE;
  1045. goto out;
  1046. }
  1047. }
  1048. prev_opinfo = opinfo_get_list(ci);
  1049. if (!prev_opinfo ||
  1050. (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx)) {
  1051. opinfo_put(prev_opinfo);
  1052. goto set_lev;
  1053. }
  1054. prev_op_has_lease = prev_opinfo->is_lease;
  1055. if (prev_op_has_lease)
  1056. prev_op_state = prev_opinfo->o_lease->state;
  1057. if (share_ret < 0 &&
  1058. prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  1059. err = share_ret;
  1060. opinfo_put(prev_opinfo);
  1061. goto err_out;
  1062. }
  1063. if (prev_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
  1064. prev_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  1065. opinfo_put(prev_opinfo);
  1066. goto op_break_not_needed;
  1067. }
  1068. err = oplock_break(prev_opinfo, SMB2_OPLOCK_LEVEL_II, work);
  1069. opinfo_put(prev_opinfo);
  1070. if (err == -ENOENT)
  1071. goto set_lev;
  1072. /* Check all oplock was freed by close */
  1073. else if (err < 0)
  1074. goto err_out;
  1075. op_break_not_needed:
  1076. if (share_ret < 0) {
  1077. err = share_ret;
  1078. goto err_out;
  1079. }
  1080. if (req_op_level != SMB2_OPLOCK_LEVEL_NONE)
  1081. req_op_level = SMB2_OPLOCK_LEVEL_II;
  1082. /* grant fixed oplock on stacked locking between lease and oplock */
  1083. if (prev_op_has_lease && !lctx)
  1084. if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE)
  1085. req_op_level = SMB2_OPLOCK_LEVEL_NONE;
  1086. if (!prev_op_has_lease && lctx) {
  1087. req_op_level = SMB2_OPLOCK_LEVEL_II;
  1088. lctx->req_state = SMB2_LEASE_READ_CACHING_LE;
  1089. }
  1090. set_lev:
  1091. set_oplock_level(opinfo, req_op_level, lctx);
  1092. out:
  1093. rcu_assign_pointer(fp->f_opinfo, opinfo);
  1094. opinfo->o_fp = fp;
  1095. opinfo_count_inc(fp);
  1096. opinfo_add(opinfo);
  1097. if (opinfo->is_lease) {
  1098. err = add_lease_global_list(opinfo);
  1099. if (err)
  1100. goto err_out;
  1101. }
  1102. return 0;
  1103. err_out:
  1104. free_opinfo(opinfo);
  1105. return err;
  1106. }
  1107. /**
  1108. * smb_break_all_write_oplock() - break batch/exclusive oplock to level2
  1109. * @work: smb work
  1110. * @fp: ksmbd file pointer
  1111. * @is_trunc: truncate on open
  1112. */
  1113. static void smb_break_all_write_oplock(struct ksmbd_work *work,
  1114. struct ksmbd_file *fp, int is_trunc)
  1115. {
  1116. struct oplock_info *brk_opinfo;
  1117. brk_opinfo = opinfo_get_list(fp->f_ci);
  1118. if (!brk_opinfo)
  1119. return;
  1120. if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
  1121. brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  1122. opinfo_put(brk_opinfo);
  1123. return;
  1124. }
  1125. brk_opinfo->open_trunc = is_trunc;
  1126. oplock_break(brk_opinfo, SMB2_OPLOCK_LEVEL_II, work);
  1127. opinfo_put(brk_opinfo);
  1128. }
  1129. /**
  1130. * smb_break_all_levII_oplock() - send level2 oplock or read lease break command
  1131. * from server to client
  1132. * @work: smb work
  1133. * @fp: ksmbd file pointer
  1134. * @is_trunc: truncate on open
  1135. */
  1136. void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp,
  1137. int is_trunc)
  1138. {
  1139. struct oplock_info *op, *brk_op;
  1140. struct ksmbd_inode *ci;
  1141. struct ksmbd_conn *conn = work->conn;
  1142. if (!test_share_config_flag(work->tcon->share_conf,
  1143. KSMBD_SHARE_FLAG_OPLOCKS))
  1144. return;
  1145. ci = fp->f_ci;
  1146. op = opinfo_get(fp);
  1147. down_read(&ci->m_lock);
  1148. list_for_each_entry(brk_op, &ci->m_op_list, op_entry) {
  1149. if (brk_op->conn == NULL)
  1150. continue;
  1151. if (!atomic_inc_not_zero(&brk_op->refcount))
  1152. continue;
  1153. if (ksmbd_conn_releasing(brk_op->conn)) {
  1154. opinfo_put(brk_op);
  1155. continue;
  1156. }
  1157. if (brk_op->is_lease && (brk_op->o_lease->state &
  1158. (~(SMB2_LEASE_READ_CACHING_LE |
  1159. SMB2_LEASE_HANDLE_CACHING_LE)))) {
  1160. ksmbd_debug(OPLOCK, "unexpected lease state(0x%x)\n",
  1161. brk_op->o_lease->state);
  1162. goto next;
  1163. } else if (brk_op->level !=
  1164. SMB2_OPLOCK_LEVEL_II) {
  1165. ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n",
  1166. brk_op->level);
  1167. goto next;
  1168. }
  1169. /* Skip oplock being break to none */
  1170. if (brk_op->is_lease &&
  1171. brk_op->o_lease->new_state == SMB2_LEASE_NONE_LE &&
  1172. atomic_read(&brk_op->breaking_cnt))
  1173. goto next;
  1174. if (op && op->is_lease && brk_op->is_lease &&
  1175. !memcmp(conn->ClientGUID, brk_op->conn->ClientGUID,
  1176. SMB2_CLIENT_GUID_SIZE) &&
  1177. !memcmp(op->o_lease->lease_key, brk_op->o_lease->lease_key,
  1178. SMB2_LEASE_KEY_SIZE))
  1179. goto next;
  1180. brk_op->open_trunc = is_trunc;
  1181. oplock_break(brk_op, SMB2_OPLOCK_LEVEL_NONE, NULL);
  1182. next:
  1183. opinfo_put(brk_op);
  1184. }
  1185. up_read(&ci->m_lock);
  1186. if (op)
  1187. opinfo_put(op);
  1188. }
  1189. /**
  1190. * smb_break_all_oplock() - break both batch/exclusive and level2 oplock
  1191. * @work: smb work
  1192. * @fp: ksmbd file pointer
  1193. */
  1194. void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp)
  1195. {
  1196. if (!test_share_config_flag(work->tcon->share_conf,
  1197. KSMBD_SHARE_FLAG_OPLOCKS))
  1198. return;
  1199. smb_break_all_write_oplock(work, fp, 1);
  1200. smb_break_all_levII_oplock(work, fp, 1);
  1201. }
  1202. /**
  1203. * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type
  1204. * @lease_state: lease type
  1205. *
  1206. * Return: 0 if no mapping, otherwise corresponding oplock type
  1207. */
  1208. __u8 smb2_map_lease_to_oplock(__le32 lease_state)
  1209. {
  1210. if (lease_state == (SMB2_LEASE_HANDLE_CACHING_LE |
  1211. SMB2_LEASE_READ_CACHING_LE |
  1212. SMB2_LEASE_WRITE_CACHING_LE)) {
  1213. return SMB2_OPLOCK_LEVEL_BATCH;
  1214. } else if (lease_state != SMB2_LEASE_WRITE_CACHING_LE &&
  1215. lease_state & SMB2_LEASE_WRITE_CACHING_LE) {
  1216. if (!(lease_state & SMB2_LEASE_HANDLE_CACHING_LE))
  1217. return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
  1218. } else if (lease_state & SMB2_LEASE_READ_CACHING_LE) {
  1219. return SMB2_OPLOCK_LEVEL_II;
  1220. }
  1221. return 0;
  1222. }
  1223. /**
  1224. * create_lease_buf() - create lease context for open cmd response
  1225. * @rbuf: buffer to create lease context response
  1226. * @lease: buffer to stored parsed lease state information
  1227. */
  1228. void create_lease_buf(u8 *rbuf, struct lease *lease)
  1229. {
  1230. if (lease->version == 2) {
  1231. struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf;
  1232. memset(buf, 0, sizeof(struct create_lease_v2));
  1233. memcpy(buf->lcontext.LeaseKey, lease->lease_key,
  1234. SMB2_LEASE_KEY_SIZE);
  1235. buf->lcontext.LeaseFlags = lease->flags;
  1236. buf->lcontext.Epoch = cpu_to_le16(lease->epoch);
  1237. buf->lcontext.LeaseState = lease->state;
  1238. if (lease->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE)
  1239. memcpy(buf->lcontext.ParentLeaseKey, lease->parent_lease_key,
  1240. SMB2_LEASE_KEY_SIZE);
  1241. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1242. (struct create_lease_v2, lcontext));
  1243. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
  1244. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1245. (struct create_lease_v2, Name));
  1246. buf->ccontext.NameLength = cpu_to_le16(4);
  1247. buf->Name[0] = 'R';
  1248. buf->Name[1] = 'q';
  1249. buf->Name[2] = 'L';
  1250. buf->Name[3] = 's';
  1251. } else {
  1252. struct create_lease *buf = (struct create_lease *)rbuf;
  1253. memset(buf, 0, sizeof(struct create_lease));
  1254. memcpy(buf->lcontext.LeaseKey, lease->lease_key, SMB2_LEASE_KEY_SIZE);
  1255. buf->lcontext.LeaseFlags = lease->flags;
  1256. buf->lcontext.LeaseState = lease->state;
  1257. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1258. (struct create_lease, lcontext));
  1259. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  1260. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1261. (struct create_lease, Name));
  1262. buf->ccontext.NameLength = cpu_to_le16(4);
  1263. buf->Name[0] = 'R';
  1264. buf->Name[1] = 'q';
  1265. buf->Name[2] = 'L';
  1266. buf->Name[3] = 's';
  1267. }
  1268. }
  1269. /**
  1270. * parse_lease_state() - parse lease context contained in file open request
  1271. * @open_req: buffer containing smb2 file open(create) request
  1272. *
  1273. * Return: allocated lease context object on success, otherwise NULL
  1274. */
  1275. struct lease_ctx_info *parse_lease_state(void *open_req)
  1276. {
  1277. struct create_context *cc;
  1278. struct smb2_create_req *req = (struct smb2_create_req *)open_req;
  1279. struct lease_ctx_info *lreq;
  1280. cc = smb2_find_context_vals(req, SMB2_CREATE_REQUEST_LEASE, 4);
  1281. if (IS_ERR_OR_NULL(cc))
  1282. return NULL;
  1283. lreq = kzalloc(sizeof(struct lease_ctx_info), KSMBD_DEFAULT_GFP);
  1284. if (!lreq)
  1285. return NULL;
  1286. if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) {
  1287. struct create_lease_v2 *lc = (struct create_lease_v2 *)cc;
  1288. if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
  1289. sizeof(struct create_lease_v2) - 4)
  1290. goto err_out;
  1291. memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
  1292. lreq->req_state = lc->lcontext.LeaseState;
  1293. lreq->flags = lc->lcontext.LeaseFlags;
  1294. lreq->epoch = lc->lcontext.Epoch;
  1295. lreq->duration = lc->lcontext.LeaseDuration;
  1296. if (lreq->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE)
  1297. memcpy(lreq->parent_lease_key, lc->lcontext.ParentLeaseKey,
  1298. SMB2_LEASE_KEY_SIZE);
  1299. lreq->version = 2;
  1300. } else {
  1301. struct create_lease *lc = (struct create_lease *)cc;
  1302. if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
  1303. sizeof(struct create_lease))
  1304. goto err_out;
  1305. memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
  1306. lreq->req_state = lc->lcontext.LeaseState;
  1307. lreq->flags = lc->lcontext.LeaseFlags;
  1308. lreq->duration = lc->lcontext.LeaseDuration;
  1309. lreq->version = 1;
  1310. }
  1311. return lreq;
  1312. err_out:
  1313. kfree(lreq);
  1314. return NULL;
  1315. }
  1316. /**
  1317. * smb2_find_context_vals() - find a particular context info in open request
  1318. * @open_req: buffer containing smb2 file open(create) request
  1319. * @tag: context name to search for
  1320. * @tag_len: the length of tag
  1321. *
  1322. * Return: pointer to requested context, NULL if @str context not found
  1323. * or error pointer if name length is invalid.
  1324. */
  1325. struct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len)
  1326. {
  1327. struct create_context *cc;
  1328. unsigned int next = 0;
  1329. char *name;
  1330. struct smb2_create_req *req = (struct smb2_create_req *)open_req;
  1331. unsigned int remain_len, name_off, name_len, value_off, value_len,
  1332. cc_len;
  1333. /*
  1334. * CreateContextsOffset and CreateContextsLength are guaranteed to
  1335. * be valid because of ksmbd_smb2_check_message().
  1336. */
  1337. cc = (struct create_context *)((char *)req +
  1338. le32_to_cpu(req->CreateContextsOffset));
  1339. remain_len = le32_to_cpu(req->CreateContextsLength);
  1340. do {
  1341. cc = (struct create_context *)((char *)cc + next);
  1342. if (remain_len < offsetof(struct create_context, Buffer))
  1343. return ERR_PTR(-EINVAL);
  1344. next = le32_to_cpu(cc->Next);
  1345. name_off = le16_to_cpu(cc->NameOffset);
  1346. name_len = le16_to_cpu(cc->NameLength);
  1347. value_off = le16_to_cpu(cc->DataOffset);
  1348. value_len = le32_to_cpu(cc->DataLength);
  1349. cc_len = next ? next : remain_len;
  1350. if ((next & 0x7) != 0 ||
  1351. next > remain_len ||
  1352. name_off != offsetof(struct create_context, Buffer) ||
  1353. name_len < 4 ||
  1354. name_off + name_len > cc_len ||
  1355. (value_off & 0x7) != 0 ||
  1356. (value_len && value_off < name_off + (name_len < 8 ? 8 : name_len)) ||
  1357. ((u64)value_off + value_len > cc_len))
  1358. return ERR_PTR(-EINVAL);
  1359. name = (char *)cc + name_off;
  1360. if (name_len == tag_len && !memcmp(name, tag, name_len))
  1361. return cc;
  1362. remain_len -= next;
  1363. } while (next != 0);
  1364. return NULL;
  1365. }
  1366. /**
  1367. * create_durable_rsp_buf() - create durable handle context
  1368. * @cc: buffer to create durable context response
  1369. */
  1370. void create_durable_rsp_buf(char *cc)
  1371. {
  1372. struct create_durable_rsp *buf;
  1373. buf = (struct create_durable_rsp *)cc;
  1374. memset(buf, 0, sizeof(struct create_durable_rsp));
  1375. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1376. (struct create_durable_rsp, Data));
  1377. buf->ccontext.DataLength = cpu_to_le32(8);
  1378. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1379. (struct create_durable_rsp, Name));
  1380. buf->ccontext.NameLength = cpu_to_le16(4);
  1381. /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */
  1382. buf->Name[0] = 'D';
  1383. buf->Name[1] = 'H';
  1384. buf->Name[2] = 'n';
  1385. buf->Name[3] = 'Q';
  1386. }
  1387. /**
  1388. * create_durable_v2_rsp_buf() - create durable handle v2 context
  1389. * @cc: buffer to create durable context response
  1390. * @fp: ksmbd file pointer
  1391. */
  1392. void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp)
  1393. {
  1394. struct create_durable_v2_rsp *buf;
  1395. buf = (struct create_durable_v2_rsp *)cc;
  1396. memset(buf, 0, sizeof(struct create_durable_rsp));
  1397. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1398. (struct create_durable_rsp, Data));
  1399. buf->ccontext.DataLength = cpu_to_le32(8);
  1400. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1401. (struct create_durable_rsp, Name));
  1402. buf->ccontext.NameLength = cpu_to_le16(4);
  1403. /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */
  1404. buf->Name[0] = 'D';
  1405. buf->Name[1] = 'H';
  1406. buf->Name[2] = '2';
  1407. buf->Name[3] = 'Q';
  1408. buf->Timeout = cpu_to_le32(fp->durable_timeout);
  1409. if (fp->is_persistent)
  1410. buf->Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
  1411. }
  1412. /**
  1413. * create_mxac_rsp_buf() - create query maximal access context
  1414. * @cc: buffer to create maximal access context response
  1415. * @maximal_access: maximal access
  1416. */
  1417. void create_mxac_rsp_buf(char *cc, int maximal_access)
  1418. {
  1419. struct create_mxac_rsp *buf;
  1420. buf = (struct create_mxac_rsp *)cc;
  1421. memset(buf, 0, sizeof(struct create_mxac_rsp));
  1422. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1423. (struct create_mxac_rsp, QueryStatus));
  1424. buf->ccontext.DataLength = cpu_to_le32(8);
  1425. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1426. (struct create_mxac_rsp, Name));
  1427. buf->ccontext.NameLength = cpu_to_le16(4);
  1428. /* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */
  1429. buf->Name[0] = 'M';
  1430. buf->Name[1] = 'x';
  1431. buf->Name[2] = 'A';
  1432. buf->Name[3] = 'c';
  1433. buf->QueryStatus = STATUS_SUCCESS;
  1434. buf->MaximalAccess = cpu_to_le32(maximal_access);
  1435. }
  1436. void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id)
  1437. {
  1438. struct create_disk_id_rsp *buf;
  1439. buf = (struct create_disk_id_rsp *)cc;
  1440. memset(buf, 0, sizeof(struct create_disk_id_rsp));
  1441. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1442. (struct create_disk_id_rsp, DiskFileId));
  1443. buf->ccontext.DataLength = cpu_to_le32(32);
  1444. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1445. (struct create_mxac_rsp, Name));
  1446. buf->ccontext.NameLength = cpu_to_le16(4);
  1447. /* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */
  1448. buf->Name[0] = 'Q';
  1449. buf->Name[1] = 'F';
  1450. buf->Name[2] = 'i';
  1451. buf->Name[3] = 'd';
  1452. buf->DiskFileId = cpu_to_le64(file_id);
  1453. buf->VolumeId = cpu_to_le64(vol_id);
  1454. }
  1455. /**
  1456. * create_posix_rsp_buf() - create posix extension context
  1457. * @cc: buffer to create posix on posix response
  1458. * @fp: ksmbd file pointer
  1459. */
  1460. void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp)
  1461. {
  1462. struct create_posix_rsp *buf;
  1463. struct inode *inode = file_inode(fp->filp);
  1464. struct mnt_idmap *idmap = file_mnt_idmap(fp->filp);
  1465. vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
  1466. vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
  1467. buf = (struct create_posix_rsp *)cc;
  1468. memset(buf, 0, sizeof(struct create_posix_rsp));
  1469. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1470. (struct create_posix_rsp, nlink));
  1471. /*
  1472. * DataLength = nlink(4) + reparse_tag(4) + mode(4) +
  1473. * domain sid(28) + unix group sid(16).
  1474. */
  1475. buf->ccontext.DataLength = cpu_to_le32(56);
  1476. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1477. (struct create_posix_rsp, Name));
  1478. buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
  1479. /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
  1480. buf->Name[0] = 0x93;
  1481. buf->Name[1] = 0xAD;
  1482. buf->Name[2] = 0x25;
  1483. buf->Name[3] = 0x50;
  1484. buf->Name[4] = 0x9C;
  1485. buf->Name[5] = 0xB4;
  1486. buf->Name[6] = 0x11;
  1487. buf->Name[7] = 0xE7;
  1488. buf->Name[8] = 0xB4;
  1489. buf->Name[9] = 0x23;
  1490. buf->Name[10] = 0x83;
  1491. buf->Name[11] = 0xDE;
  1492. buf->Name[12] = 0x96;
  1493. buf->Name[13] = 0x8B;
  1494. buf->Name[14] = 0xCD;
  1495. buf->Name[15] = 0x7C;
  1496. buf->nlink = cpu_to_le32(inode->i_nlink);
  1497. buf->reparse_tag = cpu_to_le32(fp->volatile_id);
  1498. buf->mode = cpu_to_le32(inode->i_mode & 0777);
  1499. /*
  1500. * SidBuffer(44) contain two sids(Domain sid(28), UNIX group sid(16)).
  1501. * Domain sid(28) = revision(1) + num_subauth(1) + authority(6) +
  1502. * sub_auth(4 * 4(num_subauth)) + RID(4).
  1503. * UNIX group id(16) = revision(1) + num_subauth(1) + authority(6) +
  1504. * sub_auth(4 * 1(num_subauth)) + RID(4).
  1505. */
  1506. id_to_sid(from_kuid_munged(&init_user_ns, vfsuid_into_kuid(vfsuid)),
  1507. SIDOWNER, (struct smb_sid *)&buf->SidBuffer[0]);
  1508. id_to_sid(from_kgid_munged(&init_user_ns, vfsgid_into_kgid(vfsgid)),
  1509. SIDUNIX_GROUP, (struct smb_sid *)&buf->SidBuffer[28]);
  1510. }
  1511. /*
  1512. * Find lease object(opinfo) for given lease key/fid from lease
  1513. * break/file close path.
  1514. */
  1515. /**
  1516. * lookup_lease_in_table() - find a matching lease info object
  1517. * @conn: connection instance
  1518. * @lease_key: lease key to be searched for
  1519. *
  1520. * Return: opinfo if found matching opinfo, otherwise NULL
  1521. */
  1522. struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
  1523. char *lease_key)
  1524. {
  1525. struct oplock_info *opinfo = NULL, *ret_op = NULL;
  1526. struct lease_table *lt;
  1527. int ret;
  1528. read_lock(&lease_list_lock);
  1529. list_for_each_entry(lt, &lease_table_list, l_entry) {
  1530. if (!memcmp(lt->client_guid, conn->ClientGUID,
  1531. SMB2_CLIENT_GUID_SIZE))
  1532. goto found;
  1533. }
  1534. read_unlock(&lease_list_lock);
  1535. return NULL;
  1536. found:
  1537. rcu_read_lock();
  1538. list_for_each_entry_rcu(opinfo, &lt->lease_list, lease_entry) {
  1539. if (!atomic_inc_not_zero(&opinfo->refcount))
  1540. continue;
  1541. rcu_read_unlock();
  1542. if (!opinfo->op_state || opinfo->op_state == OPLOCK_CLOSING)
  1543. goto op_next;
  1544. if (!(opinfo->o_lease->state &
  1545. (SMB2_LEASE_HANDLE_CACHING_LE |
  1546. SMB2_LEASE_WRITE_CACHING_LE)))
  1547. goto op_next;
  1548. ret = compare_guid_key(opinfo, conn->ClientGUID,
  1549. lease_key);
  1550. if (ret) {
  1551. ksmbd_debug(OPLOCK, "found opinfo\n");
  1552. ret_op = opinfo;
  1553. goto out;
  1554. }
  1555. op_next:
  1556. opinfo_put(opinfo);
  1557. rcu_read_lock();
  1558. }
  1559. rcu_read_unlock();
  1560. out:
  1561. read_unlock(&lease_list_lock);
  1562. return ret_op;
  1563. }
  1564. int smb2_check_durable_oplock(struct ksmbd_conn *conn,
  1565. struct ksmbd_share_config *share,
  1566. struct ksmbd_file *fp,
  1567. struct lease_ctx_info *lctx,
  1568. char *name)
  1569. {
  1570. struct oplock_info *opinfo = opinfo_get(fp);
  1571. int ret = 0;
  1572. if (!opinfo)
  1573. return 0;
  1574. if (opinfo->is_lease == false) {
  1575. if (lctx) {
  1576. pr_err("create context include lease\n");
  1577. ret = -EBADF;
  1578. goto out;
  1579. }
  1580. if (opinfo->level != SMB2_OPLOCK_LEVEL_BATCH) {
  1581. pr_err("oplock level is not equal to SMB2_OPLOCK_LEVEL_BATCH\n");
  1582. ret = -EBADF;
  1583. }
  1584. goto out;
  1585. }
  1586. if (memcmp(conn->ClientGUID, fp->client_guid,
  1587. SMB2_CLIENT_GUID_SIZE)) {
  1588. ksmbd_debug(SMB, "Client guid of fp is not equal to the one of connection\n");
  1589. ret = -EBADF;
  1590. goto out;
  1591. }
  1592. if (!lctx) {
  1593. ksmbd_debug(SMB, "create context does not include lease\n");
  1594. ret = -EBADF;
  1595. goto out;
  1596. }
  1597. if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key,
  1598. SMB2_LEASE_KEY_SIZE)) {
  1599. ksmbd_debug(SMB,
  1600. "lease key of fp does not match lease key in create context\n");
  1601. ret = -EBADF;
  1602. goto out;
  1603. }
  1604. if (!(opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)) {
  1605. ksmbd_debug(SMB, "lease state does not contain SMB2_LEASE_HANDLE_CACHING\n");
  1606. ret = -EBADF;
  1607. goto out;
  1608. }
  1609. if (opinfo->o_lease->version != lctx->version) {
  1610. ksmbd_debug(SMB,
  1611. "lease version of fp does not match the one in create context\n");
  1612. ret = -EBADF;
  1613. goto out;
  1614. }
  1615. if (!ksmbd_inode_pending_delete(fp))
  1616. ret = ksmbd_validate_name_reconnect(share, fp, name);
  1617. out:
  1618. opinfo_put(opinfo);
  1619. return ret;
  1620. }