cifsfs.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * Common Internet FileSystem (CIFS) client
  8. *
  9. */
  10. /* Note that BB means BUGBUG (ie something to fix eventually) */
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/filelock.h>
  14. #include <linux/mount.h>
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/vfs.h>
  20. #include <linux/mempool.h>
  21. #include <linux/delay.h>
  22. #include <linux/kthread.h>
  23. #include <linux/freezer.h>
  24. #include <linux/namei.h>
  25. #include <linux/random.h>
  26. #include <linux/splice.h>
  27. #include <linux/uuid.h>
  28. #include <linux/xattr.h>
  29. #include <uapi/linux/magic.h>
  30. #include <net/ipv6.h>
  31. #include "cifsfs.h"
  32. #include "cifspdu.h"
  33. #define DECLARE_GLOBALS_HERE
  34. #include "cifsglob.h"
  35. #include "cifsproto.h"
  36. #include "cifs_debug.h"
  37. #include "cifs_fs_sb.h"
  38. #include <linux/mm.h>
  39. #include <linux/key-type.h>
  40. #include "cifs_spnego.h"
  41. #include "fscache.h"
  42. #ifdef CONFIG_CIFS_DFS_UPCALL
  43. #include "dfs_cache.h"
  44. #endif
  45. #ifdef CONFIG_CIFS_SWN_UPCALL
  46. #include "netlink.h"
  47. #endif
  48. #include "fs_context.h"
  49. #include "cached_dir.h"
  50. /*
  51. * DOS dates from 1980/1/1 through 2107/12/31
  52. * Protocol specifications indicate the range should be to 119, which
  53. * limits maximum year to 2099. But this range has not been checked.
  54. */
  55. #define SMB_DATE_MAX (127<<9 | 12<<5 | 31)
  56. #define SMB_DATE_MIN (0<<9 | 1<<5 | 1)
  57. #define SMB_TIME_MAX (23<<11 | 59<<5 | 29)
  58. int cifsFYI = 0;
  59. bool traceSMB;
  60. bool enable_oplocks = true;
  61. bool linuxExtEnabled = true;
  62. bool lookupCacheEnabled = true;
  63. bool disable_legacy_dialects; /* false by default */
  64. bool enable_gcm_256 = true;
  65. bool require_gcm_256; /* false by default */
  66. bool enable_negotiate_signing; /* false by default */
  67. unsigned int global_secflags = CIFSSEC_DEF;
  68. /* unsigned int ntlmv2_support = 0; */
  69. unsigned int sign_CIFS_PDUs = 1;
  70. /*
  71. * Global transaction id (XID) information
  72. */
  73. unsigned int GlobalCurrentXid; /* protected by GlobalMid_Lock */
  74. unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Lock */
  75. unsigned int GlobalMaxActiveXid; /* prot by GlobalMid_Lock */
  76. spinlock_t GlobalMid_Lock; /* protects above & list operations on midQ entries */
  77. /*
  78. * Global counters, updated atomically
  79. */
  80. atomic_t sesInfoAllocCount;
  81. atomic_t tconInfoAllocCount;
  82. atomic_t tcpSesNextId;
  83. atomic_t tcpSesAllocCount;
  84. atomic_t tcpSesReconnectCount;
  85. atomic_t tconInfoReconnectCount;
  86. atomic_t mid_count;
  87. atomic_t buf_alloc_count;
  88. atomic_t small_buf_alloc_count;
  89. #ifdef CONFIG_CIFS_STATS2
  90. atomic_t total_buf_alloc_count;
  91. atomic_t total_small_buf_alloc_count;
  92. #endif/* STATS2 */
  93. struct list_head cifs_tcp_ses_list;
  94. spinlock_t cifs_tcp_ses_lock;
  95. static const struct super_operations cifs_super_ops;
  96. unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
  97. module_param(CIFSMaxBufSize, uint, 0444);
  98. MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
  99. "for CIFS requests. "
  100. "Default: 16384 Range: 8192 to 130048");
  101. unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
  102. module_param(cifs_min_rcv, uint, 0444);
  103. MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
  104. "1 to 64");
  105. unsigned int cifs_min_small = 30;
  106. module_param(cifs_min_small, uint, 0444);
  107. MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
  108. "Range: 2 to 256");
  109. unsigned int cifs_max_pending = CIFS_MAX_REQ;
  110. module_param(cifs_max_pending, uint, 0444);
  111. MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
  112. "CIFS/SMB1 dialect (N/A for SMB3) "
  113. "Default: 32767 Range: 2 to 32767.");
  114. unsigned int dir_cache_timeout = 30;
  115. module_param(dir_cache_timeout, uint, 0644);
  116. MODULE_PARM_DESC(dir_cache_timeout, "Number of seconds to cache directory contents for which we have a lease. Default: 30 "
  117. "Range: 1 to 65000 seconds, 0 to disable caching dir contents");
  118. #ifdef CONFIG_CIFS_STATS2
  119. unsigned int slow_rsp_threshold = 1;
  120. module_param(slow_rsp_threshold, uint, 0644);
  121. MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
  122. "before logging that a response is delayed. "
  123. "Default: 1 (if set to 0 disables msg).");
  124. #endif /* STATS2 */
  125. module_param(enable_oplocks, bool, 0644);
  126. MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
  127. module_param(enable_gcm_256, bool, 0644);
  128. MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: y/Y/0");
  129. module_param(require_gcm_256, bool, 0644);
  130. MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
  131. module_param(enable_negotiate_signing, bool, 0644);
  132. MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0");
  133. module_param(disable_legacy_dialects, bool, 0644);
  134. MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
  135. "helpful to restrict the ability to "
  136. "override the default dialects (SMB2.1, "
  137. "SMB3 and SMB3.02) on mount with old "
  138. "dialects (CIFS/SMB1 and SMB2) since "
  139. "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
  140. " and less secure. Default: n/N/0");
  141. struct workqueue_struct *cifsiod_wq;
  142. struct workqueue_struct *decrypt_wq;
  143. struct workqueue_struct *fileinfo_put_wq;
  144. struct workqueue_struct *cifsoplockd_wq;
  145. struct workqueue_struct *deferredclose_wq;
  146. struct workqueue_struct *serverclose_wq;
  147. struct workqueue_struct *cfid_put_wq;
  148. __u32 cifs_lock_secret;
  149. /*
  150. * Bumps refcount for cifs super block.
  151. * Note that it should be only called if a reference to VFS super block is
  152. * already held, e.g. in open-type syscalls context. Otherwise it can race with
  153. * atomic_dec_and_test in deactivate_locked_super.
  154. */
  155. void
  156. cifs_sb_active(struct super_block *sb)
  157. {
  158. struct cifs_sb_info *server = CIFS_SB(sb);
  159. if (atomic_inc_return(&server->active) == 1)
  160. atomic_inc(&sb->s_active);
  161. }
  162. void
  163. cifs_sb_deactive(struct super_block *sb)
  164. {
  165. struct cifs_sb_info *server = CIFS_SB(sb);
  166. if (atomic_dec_and_test(&server->active))
  167. deactivate_super(sb);
  168. }
  169. static int
  170. cifs_read_super(struct super_block *sb)
  171. {
  172. struct inode *inode;
  173. struct cifs_sb_info *cifs_sb;
  174. struct cifs_tcon *tcon;
  175. struct timespec64 ts;
  176. int rc = 0;
  177. cifs_sb = CIFS_SB(sb);
  178. tcon = cifs_sb_master_tcon(cifs_sb);
  179. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
  180. sb->s_flags |= SB_POSIXACL;
  181. if (tcon->snapshot_time)
  182. sb->s_flags |= SB_RDONLY;
  183. if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
  184. sb->s_maxbytes = MAX_LFS_FILESIZE;
  185. else
  186. sb->s_maxbytes = MAX_NON_LFS;
  187. /*
  188. * Some very old servers like DOS and OS/2 used 2 second granularity
  189. * (while all current servers use 100ns granularity - see MS-DTYP)
  190. * but 1 second is the maximum allowed granularity for the VFS
  191. * so for old servers set time granularity to 1 second while for
  192. * everything else (current servers) set it to 100ns.
  193. */
  194. if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) &&
  195. ((tcon->ses->capabilities &
  196. tcon->ses->server->vals->cap_nt_find) == 0) &&
  197. !tcon->unix_ext) {
  198. sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */
  199. ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0);
  200. sb->s_time_min = ts.tv_sec;
  201. ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
  202. cpu_to_le16(SMB_TIME_MAX), 0);
  203. sb->s_time_max = ts.tv_sec;
  204. } else {
  205. /*
  206. * Almost every server, including all SMB2+, uses DCE TIME
  207. * ie 100 nanosecond units, since 1601. See MS-DTYP and MS-FSCC
  208. */
  209. sb->s_time_gran = 100;
  210. ts = cifs_NTtimeToUnix(0);
  211. sb->s_time_min = ts.tv_sec;
  212. ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
  213. sb->s_time_max = ts.tv_sec;
  214. }
  215. sb->s_magic = CIFS_SUPER_MAGIC;
  216. sb->s_op = &cifs_super_ops;
  217. sb->s_xattr = cifs_xattr_handlers;
  218. rc = super_setup_bdi(sb);
  219. if (rc)
  220. goto out_no_root;
  221. /* tune readahead according to rsize if readahead size not set on mount */
  222. if (cifs_sb->ctx->rsize == 0)
  223. cifs_sb->ctx->rsize =
  224. tcon->ses->server->ops->negotiate_rsize(tcon, cifs_sb->ctx);
  225. if (cifs_sb->ctx->rasize)
  226. sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
  227. else
  228. sb->s_bdi->ra_pages = 2 * (cifs_sb->ctx->rsize / PAGE_SIZE);
  229. sb->s_blocksize = CIFS_MAX_MSGSIZE;
  230. sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
  231. inode = cifs_root_iget(sb);
  232. if (IS_ERR(inode)) {
  233. rc = PTR_ERR(inode);
  234. goto out_no_root;
  235. }
  236. if (tcon->nocase)
  237. sb->s_d_op = &cifs_ci_dentry_ops;
  238. else
  239. sb->s_d_op = &cifs_dentry_ops;
  240. sb->s_root = d_make_root(inode);
  241. if (!sb->s_root) {
  242. rc = -ENOMEM;
  243. goto out_no_root;
  244. }
  245. #ifdef CONFIG_CIFS_NFSD_EXPORT
  246. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  247. cifs_dbg(FYI, "export ops supported\n");
  248. sb->s_export_op = &cifs_export_ops;
  249. }
  250. #endif /* CONFIG_CIFS_NFSD_EXPORT */
  251. return 0;
  252. out_no_root:
  253. cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
  254. return rc;
  255. }
  256. static void cifs_kill_sb(struct super_block *sb)
  257. {
  258. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  259. /*
  260. * We need to release all dentries for the cached directories
  261. * before we kill the sb.
  262. */
  263. if (cifs_sb->root) {
  264. close_all_cached_dirs(cifs_sb);
  265. /* finally release root dentry */
  266. dput(cifs_sb->root);
  267. cifs_sb->root = NULL;
  268. }
  269. kill_anon_super(sb);
  270. cifs_umount(cifs_sb);
  271. }
  272. static int
  273. cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
  274. {
  275. struct super_block *sb = dentry->d_sb;
  276. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  277. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  278. struct TCP_Server_Info *server = tcon->ses->server;
  279. unsigned int xid;
  280. int rc = 0;
  281. const char *full_path;
  282. void *page;
  283. xid = get_xid();
  284. page = alloc_dentry_path();
  285. full_path = build_path_from_dentry(dentry, page);
  286. if (IS_ERR(full_path)) {
  287. rc = PTR_ERR(full_path);
  288. goto statfs_out;
  289. }
  290. if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
  291. buf->f_namelen =
  292. le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
  293. else
  294. buf->f_namelen = PATH_MAX;
  295. buf->f_fsid.val[0] = tcon->vol_serial_number;
  296. /* are using part of create time for more randomness, see man statfs */
  297. buf->f_fsid.val[1] = (int)le64_to_cpu(tcon->vol_create_time);
  298. buf->f_files = 0; /* undefined */
  299. buf->f_ffree = 0; /* unlimited */
  300. if (server->ops->queryfs)
  301. rc = server->ops->queryfs(xid, tcon, full_path, cifs_sb, buf);
  302. statfs_out:
  303. free_dentry_path(page);
  304. free_xid(xid);
  305. return rc;
  306. }
  307. static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
  308. {
  309. struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
  310. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  311. struct TCP_Server_Info *server = tcon->ses->server;
  312. if (server->ops->fallocate)
  313. return server->ops->fallocate(file, tcon, mode, off, len);
  314. return -EOPNOTSUPP;
  315. }
  316. static int cifs_permission(struct mnt_idmap *idmap,
  317. struct inode *inode, int mask)
  318. {
  319. struct cifs_sb_info *cifs_sb;
  320. cifs_sb = CIFS_SB(inode->i_sb);
  321. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
  322. if ((mask & MAY_EXEC) && !execute_ok(inode))
  323. return -EACCES;
  324. else
  325. return 0;
  326. } else /* file mode might have been restricted at mount time
  327. on the client (above and beyond ACL on servers) for
  328. servers which do not support setting and viewing mode bits,
  329. so allowing client to check permissions is useful */
  330. return generic_permission(&nop_mnt_idmap, inode, mask);
  331. }
  332. static struct kmem_cache *cifs_inode_cachep;
  333. static struct kmem_cache *cifs_req_cachep;
  334. static struct kmem_cache *cifs_mid_cachep;
  335. static struct kmem_cache *cifs_sm_req_cachep;
  336. static struct kmem_cache *cifs_io_request_cachep;
  337. static struct kmem_cache *cifs_io_subrequest_cachep;
  338. mempool_t *cifs_sm_req_poolp;
  339. mempool_t *cifs_req_poolp;
  340. mempool_t *cifs_mid_poolp;
  341. mempool_t cifs_io_request_pool;
  342. mempool_t cifs_io_subrequest_pool;
  343. static struct inode *
  344. cifs_alloc_inode(struct super_block *sb)
  345. {
  346. struct cifsInodeInfo *cifs_inode;
  347. cifs_inode = alloc_inode_sb(sb, cifs_inode_cachep, GFP_KERNEL);
  348. if (!cifs_inode)
  349. return NULL;
  350. cifs_inode->cifsAttrs = 0x20; /* default */
  351. cifs_inode->time = 0;
  352. /*
  353. * Until the file is open and we have gotten oplock info back from the
  354. * server, can not assume caching of file data or metadata.
  355. */
  356. cifs_set_oplock_level(cifs_inode, 0);
  357. cifs_inode->lease_granted = false;
  358. cifs_inode->flags = 0;
  359. spin_lock_init(&cifs_inode->writers_lock);
  360. cifs_inode->writers = 0;
  361. cifs_inode->netfs.inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
  362. cifs_inode->netfs.remote_i_size = 0;
  363. cifs_inode->uniqueid = 0;
  364. cifs_inode->createtime = 0;
  365. cifs_inode->epoch = 0;
  366. spin_lock_init(&cifs_inode->open_file_lock);
  367. generate_random_uuid(cifs_inode->lease_key);
  368. cifs_inode->symlink_target = NULL;
  369. /*
  370. * Can not set i_flags here - they get immediately overwritten to zero
  371. * by the VFS.
  372. */
  373. /* cifs_inode->netfs.inode.i_flags = S_NOATIME | S_NOCMTIME; */
  374. INIT_LIST_HEAD(&cifs_inode->openFileList);
  375. INIT_LIST_HEAD(&cifs_inode->llist);
  376. INIT_LIST_HEAD(&cifs_inode->deferred_closes);
  377. spin_lock_init(&cifs_inode->deferred_lock);
  378. return &cifs_inode->netfs.inode;
  379. }
  380. static void
  381. cifs_free_inode(struct inode *inode)
  382. {
  383. struct cifsInodeInfo *cinode = CIFS_I(inode);
  384. if (S_ISLNK(inode->i_mode))
  385. kfree(cinode->symlink_target);
  386. kmem_cache_free(cifs_inode_cachep, cinode);
  387. }
  388. static void
  389. cifs_evict_inode(struct inode *inode)
  390. {
  391. netfs_wait_for_outstanding_io(inode);
  392. truncate_inode_pages_final(&inode->i_data);
  393. if (inode->i_state & I_PINNING_NETFS_WB)
  394. cifs_fscache_unuse_inode_cookie(inode, true);
  395. cifs_fscache_release_inode_cookie(inode);
  396. clear_inode(inode);
  397. }
  398. static void
  399. cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
  400. {
  401. struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
  402. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
  403. seq_puts(s, ",addr=");
  404. switch (server->dstaddr.ss_family) {
  405. case AF_INET:
  406. seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
  407. break;
  408. case AF_INET6:
  409. seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
  410. if (sa6->sin6_scope_id)
  411. seq_printf(s, "%%%u", sa6->sin6_scope_id);
  412. break;
  413. default:
  414. seq_puts(s, "(unknown)");
  415. }
  416. if (server->rdma)
  417. seq_puts(s, ",rdma");
  418. }
  419. static void
  420. cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
  421. {
  422. if (ses->sectype == Unspecified) {
  423. if (ses->user_name == NULL)
  424. seq_puts(s, ",sec=none");
  425. return;
  426. }
  427. seq_puts(s, ",sec=");
  428. switch (ses->sectype) {
  429. case NTLMv2:
  430. seq_puts(s, "ntlmv2");
  431. break;
  432. case Kerberos:
  433. seq_puts(s, "krb5");
  434. break;
  435. case RawNTLMSSP:
  436. seq_puts(s, "ntlmssp");
  437. break;
  438. default:
  439. /* shouldn't ever happen */
  440. seq_puts(s, "unknown");
  441. break;
  442. }
  443. if (ses->sign)
  444. seq_puts(s, "i");
  445. if (ses->sectype == Kerberos)
  446. seq_printf(s, ",cruid=%u",
  447. from_kuid_munged(&init_user_ns, ses->cred_uid));
  448. }
  449. static void
  450. cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
  451. {
  452. seq_puts(s, ",cache=");
  453. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
  454. seq_puts(s, "strict");
  455. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
  456. seq_puts(s, "none");
  457. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
  458. seq_puts(s, "singleclient"); /* assume only one client access */
  459. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE)
  460. seq_puts(s, "ro"); /* read only caching assumed */
  461. else
  462. seq_puts(s, "loose");
  463. }
  464. /*
  465. * cifs_show_devname() is used so we show the mount device name with correct
  466. * format (e.g. forward slashes vs. back slashes) in /proc/mounts
  467. */
  468. static int cifs_show_devname(struct seq_file *m, struct dentry *root)
  469. {
  470. struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
  471. char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL);
  472. if (devname == NULL)
  473. seq_puts(m, "none");
  474. else {
  475. convert_delimiter(devname, '/');
  476. /* escape all spaces in share names */
  477. seq_escape(m, devname, " \t");
  478. kfree(devname);
  479. }
  480. return 0;
  481. }
  482. static void
  483. cifs_show_upcall_target(struct seq_file *s, struct cifs_sb_info *cifs_sb)
  484. {
  485. if (cifs_sb->ctx->upcall_target == UPTARGET_UNSPECIFIED) {
  486. seq_puts(s, ",upcall_target=app");
  487. return;
  488. }
  489. seq_puts(s, ",upcall_target=");
  490. switch (cifs_sb->ctx->upcall_target) {
  491. case UPTARGET_APP:
  492. seq_puts(s, "app");
  493. break;
  494. case UPTARGET_MOUNT:
  495. seq_puts(s, "mount");
  496. break;
  497. default:
  498. /* shouldn't ever happen */
  499. seq_puts(s, "unknown");
  500. break;
  501. }
  502. }
  503. /*
  504. * cifs_show_options() is for displaying mount options in /proc/mounts.
  505. * Not all settable options are displayed but most of the important
  506. * ones are.
  507. */
  508. static int
  509. cifs_show_options(struct seq_file *s, struct dentry *root)
  510. {
  511. struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
  512. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  513. struct sockaddr *srcaddr;
  514. srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
  515. seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
  516. cifs_show_security(s, tcon->ses);
  517. cifs_show_cache_flavor(s, cifs_sb);
  518. cifs_show_upcall_target(s, cifs_sb);
  519. if (tcon->no_lease)
  520. seq_puts(s, ",nolease");
  521. if (cifs_sb->ctx->multiuser)
  522. seq_puts(s, ",multiuser");
  523. else if (tcon->ses->user_name)
  524. seq_show_option(s, "username", tcon->ses->user_name);
  525. if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
  526. seq_show_option(s, "domain", tcon->ses->domainName);
  527. if (srcaddr->sa_family != AF_UNSPEC) {
  528. struct sockaddr_in *saddr4;
  529. struct sockaddr_in6 *saddr6;
  530. saddr4 = (struct sockaddr_in *)srcaddr;
  531. saddr6 = (struct sockaddr_in6 *)srcaddr;
  532. if (srcaddr->sa_family == AF_INET6)
  533. seq_printf(s, ",srcaddr=%pI6c",
  534. &saddr6->sin6_addr);
  535. else if (srcaddr->sa_family == AF_INET)
  536. seq_printf(s, ",srcaddr=%pI4",
  537. &saddr4->sin_addr.s_addr);
  538. else
  539. seq_printf(s, ",srcaddr=BAD-AF:%i",
  540. (int)(srcaddr->sa_family));
  541. }
  542. seq_printf(s, ",uid=%u",
  543. from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid));
  544. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  545. seq_puts(s, ",forceuid");
  546. else
  547. seq_puts(s, ",noforceuid");
  548. seq_printf(s, ",gid=%u",
  549. from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid));
  550. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  551. seq_puts(s, ",forcegid");
  552. else
  553. seq_puts(s, ",noforcegid");
  554. cifs_show_address(s, tcon->ses->server);
  555. if (!tcon->unix_ext)
  556. seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
  557. cifs_sb->ctx->file_mode,
  558. cifs_sb->ctx->dir_mode);
  559. if (cifs_sb->ctx->iocharset)
  560. seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
  561. if (tcon->seal)
  562. seq_puts(s, ",seal");
  563. else if (tcon->ses->server->ignore_signature)
  564. seq_puts(s, ",signloosely");
  565. if (tcon->nocase)
  566. seq_puts(s, ",nocase");
  567. if (tcon->nodelete)
  568. seq_puts(s, ",nodelete");
  569. if (cifs_sb->ctx->no_sparse)
  570. seq_puts(s, ",nosparse");
  571. if (tcon->local_lease)
  572. seq_puts(s, ",locallease");
  573. if (tcon->retry)
  574. seq_puts(s, ",hard");
  575. else
  576. seq_puts(s, ",soft");
  577. if (tcon->use_persistent)
  578. seq_puts(s, ",persistenthandles");
  579. else if (tcon->use_resilient)
  580. seq_puts(s, ",resilienthandles");
  581. if (tcon->posix_extensions)
  582. seq_puts(s, ",posix");
  583. else if (tcon->unix_ext)
  584. seq_puts(s, ",unix");
  585. else
  586. seq_puts(s, ",nounix");
  587. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
  588. seq_puts(s, ",nodfs");
  589. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
  590. seq_puts(s, ",posixpaths");
  591. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
  592. seq_puts(s, ",setuids");
  593. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
  594. seq_puts(s, ",idsfromsid");
  595. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  596. seq_puts(s, ",serverino");
  597. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  598. seq_puts(s, ",rwpidforward");
  599. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
  600. seq_puts(s, ",forcemand");
  601. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  602. seq_puts(s, ",nouser_xattr");
  603. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  604. seq_puts(s, ",mapchars");
  605. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
  606. seq_puts(s, ",mapposix");
  607. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  608. seq_puts(s, ",sfu");
  609. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  610. seq_puts(s, ",nobrl");
  611. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
  612. seq_puts(s, ",nohandlecache");
  613. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
  614. seq_puts(s, ",modefromsid");
  615. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
  616. seq_puts(s, ",cifsacl");
  617. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  618. seq_puts(s, ",dynperm");
  619. if (root->d_sb->s_flags & SB_POSIXACL)
  620. seq_puts(s, ",acl");
  621. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  622. seq_puts(s, ",mfsymlinks");
  623. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
  624. seq_puts(s, ",fsc");
  625. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
  626. seq_puts(s, ",nostrictsync");
  627. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  628. seq_puts(s, ",noperm");
  629. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
  630. seq_printf(s, ",backupuid=%u",
  631. from_kuid_munged(&init_user_ns,
  632. cifs_sb->ctx->backupuid));
  633. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
  634. seq_printf(s, ",backupgid=%u",
  635. from_kgid_munged(&init_user_ns,
  636. cifs_sb->ctx->backupgid));
  637. seq_show_option(s, "reparse",
  638. cifs_reparse_type_str(cifs_sb->ctx->reparse_type));
  639. seq_printf(s, ",rsize=%u", cifs_sb->ctx->rsize);
  640. seq_printf(s, ",wsize=%u", cifs_sb->ctx->wsize);
  641. seq_printf(s, ",bsize=%u", cifs_sb->ctx->bsize);
  642. if (cifs_sb->ctx->rasize)
  643. seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
  644. if (tcon->ses->server->min_offload)
  645. seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
  646. if (tcon->ses->server->retrans)
  647. seq_printf(s, ",retrans=%u", tcon->ses->server->retrans);
  648. seq_printf(s, ",echo_interval=%lu",
  649. tcon->ses->server->echo_interval / HZ);
  650. /* Only display the following if overridden on mount */
  651. if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE)
  652. seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits);
  653. if (tcon->ses->server->tcp_nodelay)
  654. seq_puts(s, ",tcpnodelay");
  655. if (tcon->ses->server->noautotune)
  656. seq_puts(s, ",noautotune");
  657. if (tcon->ses->server->noblocksnd)
  658. seq_puts(s, ",noblocksend");
  659. if (tcon->ses->server->nosharesock)
  660. seq_puts(s, ",nosharesock");
  661. if (tcon->snapshot_time)
  662. seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
  663. if (tcon->handle_timeout)
  664. seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
  665. if (tcon->max_cached_dirs != MAX_CACHED_FIDS)
  666. seq_printf(s, ",max_cached_dirs=%u", tcon->max_cached_dirs);
  667. /*
  668. * Display file and directory attribute timeout in seconds.
  669. * If file and directory attribute timeout the same then actimeo
  670. * was likely specified on mount
  671. */
  672. if (cifs_sb->ctx->acdirmax == cifs_sb->ctx->acregmax)
  673. seq_printf(s, ",actimeo=%lu", cifs_sb->ctx->acregmax / HZ);
  674. else {
  675. seq_printf(s, ",acdirmax=%lu", cifs_sb->ctx->acdirmax / HZ);
  676. seq_printf(s, ",acregmax=%lu", cifs_sb->ctx->acregmax / HZ);
  677. }
  678. seq_printf(s, ",closetimeo=%lu", cifs_sb->ctx->closetimeo / HZ);
  679. if (tcon->ses->chan_max > 1)
  680. seq_printf(s, ",multichannel,max_channels=%zu",
  681. tcon->ses->chan_max);
  682. if (tcon->use_witness)
  683. seq_puts(s, ",witness");
  684. return 0;
  685. }
  686. static void cifs_umount_begin(struct super_block *sb)
  687. {
  688. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  689. struct cifs_tcon *tcon;
  690. if (cifs_sb == NULL)
  691. return;
  692. tcon = cifs_sb_master_tcon(cifs_sb);
  693. spin_lock(&cifs_tcp_ses_lock);
  694. spin_lock(&tcon->tc_lock);
  695. trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
  696. netfs_trace_tcon_ref_see_umount);
  697. if ((tcon->tc_count > 1) || (tcon->status == TID_EXITING)) {
  698. /* we have other mounts to same share or we have
  699. already tried to umount this and woken up
  700. all waiting network requests, nothing to do */
  701. spin_unlock(&tcon->tc_lock);
  702. spin_unlock(&cifs_tcp_ses_lock);
  703. return;
  704. }
  705. /*
  706. * can not set tcon->status to TID_EXITING yet since we don't know if umount -f will
  707. * fail later (e.g. due to open files). TID_EXITING will be set just before tdis req sent
  708. */
  709. spin_unlock(&tcon->tc_lock);
  710. spin_unlock(&cifs_tcp_ses_lock);
  711. cifs_close_all_deferred_files(tcon);
  712. /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
  713. /* cancel_notify_requests(tcon); */
  714. if (tcon->ses && tcon->ses->server) {
  715. cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
  716. wake_up_all(&tcon->ses->server->request_q);
  717. wake_up_all(&tcon->ses->server->response_q);
  718. msleep(1); /* yield */
  719. /* we have to kick the requests once more */
  720. wake_up_all(&tcon->ses->server->response_q);
  721. msleep(1);
  722. }
  723. return;
  724. }
  725. static int cifs_freeze(struct super_block *sb)
  726. {
  727. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  728. struct cifs_tcon *tcon;
  729. if (cifs_sb == NULL)
  730. return 0;
  731. tcon = cifs_sb_master_tcon(cifs_sb);
  732. cifs_close_all_deferred_files(tcon);
  733. return 0;
  734. }
  735. #ifdef CONFIG_CIFS_STATS2
  736. static int cifs_show_stats(struct seq_file *s, struct dentry *root)
  737. {
  738. /* BB FIXME */
  739. return 0;
  740. }
  741. #endif
  742. static int cifs_write_inode(struct inode *inode, struct writeback_control *wbc)
  743. {
  744. return netfs_unpin_writeback(inode, wbc);
  745. }
  746. static int cifs_drop_inode(struct inode *inode)
  747. {
  748. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  749. /* no serverino => unconditional eviction */
  750. return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
  751. generic_drop_inode(inode);
  752. }
  753. static const struct super_operations cifs_super_ops = {
  754. .statfs = cifs_statfs,
  755. .alloc_inode = cifs_alloc_inode,
  756. .write_inode = cifs_write_inode,
  757. .free_inode = cifs_free_inode,
  758. .drop_inode = cifs_drop_inode,
  759. .evict_inode = cifs_evict_inode,
  760. /* .show_path = cifs_show_path, */ /* Would we ever need show path? */
  761. .show_devname = cifs_show_devname,
  762. /* .delete_inode = cifs_delete_inode, */ /* Do not need above
  763. function unless later we add lazy close of inodes or unless the
  764. kernel forgets to call us with the same number of releases (closes)
  765. as opens */
  766. .show_options = cifs_show_options,
  767. .umount_begin = cifs_umount_begin,
  768. .freeze_fs = cifs_freeze,
  769. #ifdef CONFIG_CIFS_STATS2
  770. .show_stats = cifs_show_stats,
  771. #endif
  772. };
  773. /*
  774. * Get root dentry from superblock according to prefix path mount option.
  775. * Return dentry with refcount + 1 on success and NULL otherwise.
  776. */
  777. static struct dentry *
  778. cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb)
  779. {
  780. struct dentry *dentry;
  781. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  782. char *full_path = NULL;
  783. char *s, *p;
  784. char sep;
  785. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
  786. return dget(sb->s_root);
  787. full_path = cifs_build_path_to_root(ctx, cifs_sb,
  788. cifs_sb_master_tcon(cifs_sb), 0);
  789. if (full_path == NULL)
  790. return ERR_PTR(-ENOMEM);
  791. cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
  792. sep = CIFS_DIR_SEP(cifs_sb);
  793. dentry = dget(sb->s_root);
  794. s = full_path;
  795. do {
  796. struct inode *dir = d_inode(dentry);
  797. struct dentry *child;
  798. if (!S_ISDIR(dir->i_mode)) {
  799. dput(dentry);
  800. dentry = ERR_PTR(-ENOTDIR);
  801. break;
  802. }
  803. /* skip separators */
  804. while (*s == sep)
  805. s++;
  806. if (!*s)
  807. break;
  808. p = s++;
  809. /* next separator */
  810. while (*s && *s != sep)
  811. s++;
  812. child = lookup_positive_unlocked(p, dentry, s - p);
  813. dput(dentry);
  814. dentry = child;
  815. } while (!IS_ERR(dentry));
  816. kfree(full_path);
  817. return dentry;
  818. }
  819. static int cifs_set_super(struct super_block *sb, void *data)
  820. {
  821. struct cifs_mnt_data *mnt_data = data;
  822. sb->s_fs_info = mnt_data->cifs_sb;
  823. return set_anon_super(sb, NULL);
  824. }
  825. struct dentry *
  826. cifs_smb3_do_mount(struct file_system_type *fs_type,
  827. int flags, struct smb3_fs_context *old_ctx)
  828. {
  829. struct cifs_mnt_data mnt_data;
  830. struct cifs_sb_info *cifs_sb;
  831. struct super_block *sb;
  832. struct dentry *root;
  833. int rc;
  834. if (cifsFYI) {
  835. cifs_dbg(FYI, "%s: devname=%s flags=0x%x\n", __func__,
  836. old_ctx->source, flags);
  837. } else {
  838. cifs_info("Attempting to mount %s\n", old_ctx->source);
  839. }
  840. cifs_sb = kzalloc(sizeof(*cifs_sb), GFP_KERNEL);
  841. if (!cifs_sb)
  842. return ERR_PTR(-ENOMEM);
  843. cifs_sb->ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
  844. if (!cifs_sb->ctx) {
  845. root = ERR_PTR(-ENOMEM);
  846. goto out;
  847. }
  848. rc = smb3_fs_context_dup(cifs_sb->ctx, old_ctx);
  849. if (rc) {
  850. root = ERR_PTR(rc);
  851. goto out;
  852. }
  853. rc = cifs_setup_cifs_sb(cifs_sb);
  854. if (rc) {
  855. root = ERR_PTR(rc);
  856. goto out;
  857. }
  858. rc = cifs_mount(cifs_sb, cifs_sb->ctx);
  859. if (rc) {
  860. if (!(flags & SB_SILENT))
  861. cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
  862. rc);
  863. root = ERR_PTR(rc);
  864. goto out;
  865. }
  866. mnt_data.ctx = cifs_sb->ctx;
  867. mnt_data.cifs_sb = cifs_sb;
  868. mnt_data.flags = flags;
  869. /* BB should we make this contingent on mount parm? */
  870. flags |= SB_NODIRATIME | SB_NOATIME;
  871. sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
  872. if (IS_ERR(sb)) {
  873. cifs_umount(cifs_sb);
  874. return ERR_CAST(sb);
  875. }
  876. if (sb->s_root) {
  877. cifs_dbg(FYI, "Use existing superblock\n");
  878. cifs_umount(cifs_sb);
  879. cifs_sb = NULL;
  880. } else {
  881. rc = cifs_read_super(sb);
  882. if (rc) {
  883. root = ERR_PTR(rc);
  884. goto out_super;
  885. }
  886. sb->s_flags |= SB_ACTIVE;
  887. }
  888. root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb);
  889. if (IS_ERR(root))
  890. goto out_super;
  891. if (cifs_sb)
  892. cifs_sb->root = dget(root);
  893. cifs_dbg(FYI, "dentry root is: %p\n", root);
  894. return root;
  895. out_super:
  896. deactivate_locked_super(sb);
  897. return root;
  898. out:
  899. kfree(cifs_sb->prepath);
  900. smb3_cleanup_fs_context(cifs_sb->ctx);
  901. kfree(cifs_sb);
  902. return root;
  903. }
  904. static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
  905. {
  906. struct cifsFileInfo *cfile = file->private_data;
  907. struct cifs_tcon *tcon;
  908. /*
  909. * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
  910. * the cached file length
  911. */
  912. if (whence != SEEK_SET && whence != SEEK_CUR) {
  913. int rc;
  914. struct inode *inode = file_inode(file);
  915. /*
  916. * We need to be sure that all dirty pages are written and the
  917. * server has the newest file length.
  918. */
  919. if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
  920. inode->i_mapping->nrpages != 0) {
  921. rc = filemap_fdatawait(inode->i_mapping);
  922. if (rc) {
  923. mapping_set_error(inode->i_mapping, rc);
  924. return rc;
  925. }
  926. }
  927. /*
  928. * Some applications poll for the file length in this strange
  929. * way so we must seek to end on non-oplocked files by
  930. * setting the revalidate time to zero.
  931. */
  932. CIFS_I(inode)->time = 0;
  933. rc = cifs_revalidate_file_attr(file);
  934. if (rc < 0)
  935. return (loff_t)rc;
  936. }
  937. if (cfile && cfile->tlink) {
  938. tcon = tlink_tcon(cfile->tlink);
  939. if (tcon->ses->server->ops->llseek)
  940. return tcon->ses->server->ops->llseek(file, tcon,
  941. offset, whence);
  942. }
  943. return generic_file_llseek(file, offset, whence);
  944. }
  945. static int
  946. cifs_setlease(struct file *file, int arg, struct file_lease **lease, void **priv)
  947. {
  948. /*
  949. * Note that this is called by vfs setlease with i_lock held to
  950. * protect *lease from going away.
  951. */
  952. struct inode *inode = file_inode(file);
  953. struct cifsFileInfo *cfile = file->private_data;
  954. /* Check if file is oplocked if this is request for new lease */
  955. if (arg == F_UNLCK ||
  956. ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
  957. ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
  958. return generic_setlease(file, arg, lease, priv);
  959. else if (tlink_tcon(cfile->tlink)->local_lease &&
  960. !CIFS_CACHE_READ(CIFS_I(inode)))
  961. /*
  962. * If the server claims to support oplock on this file, then we
  963. * still need to check oplock even if the local_lease mount
  964. * option is set, but there are servers which do not support
  965. * oplock for which this mount option may be useful if the user
  966. * knows that the file won't be changed on the server by anyone
  967. * else.
  968. */
  969. return generic_setlease(file, arg, lease, priv);
  970. else
  971. return -EAGAIN;
  972. }
  973. struct file_system_type cifs_fs_type = {
  974. .owner = THIS_MODULE,
  975. .name = "cifs",
  976. .init_fs_context = smb3_init_fs_context,
  977. .parameters = smb3_fs_parameters,
  978. .kill_sb = cifs_kill_sb,
  979. .fs_flags = FS_RENAME_DOES_D_MOVE,
  980. };
  981. MODULE_ALIAS_FS("cifs");
  982. struct file_system_type smb3_fs_type = {
  983. .owner = THIS_MODULE,
  984. .name = "smb3",
  985. .init_fs_context = smb3_init_fs_context,
  986. .parameters = smb3_fs_parameters,
  987. .kill_sb = cifs_kill_sb,
  988. .fs_flags = FS_RENAME_DOES_D_MOVE,
  989. };
  990. MODULE_ALIAS_FS("smb3");
  991. MODULE_ALIAS("smb3");
  992. const struct inode_operations cifs_dir_inode_ops = {
  993. .create = cifs_create,
  994. .atomic_open = cifs_atomic_open,
  995. .lookup = cifs_lookup,
  996. .getattr = cifs_getattr,
  997. .unlink = cifs_unlink,
  998. .link = cifs_hardlink,
  999. .mkdir = cifs_mkdir,
  1000. .rmdir = cifs_rmdir,
  1001. .rename = cifs_rename2,
  1002. .permission = cifs_permission,
  1003. .setattr = cifs_setattr,
  1004. .symlink = cifs_symlink,
  1005. .mknod = cifs_mknod,
  1006. .listxattr = cifs_listxattr,
  1007. .get_acl = cifs_get_acl,
  1008. .set_acl = cifs_set_acl,
  1009. };
  1010. const struct inode_operations cifs_file_inode_ops = {
  1011. .setattr = cifs_setattr,
  1012. .getattr = cifs_getattr,
  1013. .permission = cifs_permission,
  1014. .listxattr = cifs_listxattr,
  1015. .fiemap = cifs_fiemap,
  1016. .get_acl = cifs_get_acl,
  1017. .set_acl = cifs_set_acl,
  1018. };
  1019. const char *cifs_get_link(struct dentry *dentry, struct inode *inode,
  1020. struct delayed_call *done)
  1021. {
  1022. char *target_path;
  1023. if (!dentry)
  1024. return ERR_PTR(-ECHILD);
  1025. target_path = kmalloc(PATH_MAX, GFP_KERNEL);
  1026. if (!target_path)
  1027. return ERR_PTR(-ENOMEM);
  1028. spin_lock(&inode->i_lock);
  1029. if (likely(CIFS_I(inode)->symlink_target)) {
  1030. strscpy(target_path, CIFS_I(inode)->symlink_target, PATH_MAX);
  1031. } else {
  1032. kfree(target_path);
  1033. target_path = ERR_PTR(-EOPNOTSUPP);
  1034. }
  1035. spin_unlock(&inode->i_lock);
  1036. if (!IS_ERR(target_path))
  1037. set_delayed_call(done, kfree_link, target_path);
  1038. return target_path;
  1039. }
  1040. const struct inode_operations cifs_symlink_inode_ops = {
  1041. .get_link = cifs_get_link,
  1042. .setattr = cifs_setattr,
  1043. .permission = cifs_permission,
  1044. .listxattr = cifs_listxattr,
  1045. };
  1046. /*
  1047. * Advance the EOF marker to after the source range.
  1048. */
  1049. static int cifs_precopy_set_eof(struct inode *src_inode, struct cifsInodeInfo *src_cifsi,
  1050. struct cifs_tcon *src_tcon,
  1051. unsigned int xid, loff_t src_end)
  1052. {
  1053. struct cifsFileInfo *writeable_srcfile;
  1054. int rc = -EINVAL;
  1055. writeable_srcfile = find_writable_file(src_cifsi, FIND_WR_FSUID_ONLY);
  1056. if (writeable_srcfile) {
  1057. if (src_tcon->ses->server->ops->set_file_size)
  1058. rc = src_tcon->ses->server->ops->set_file_size(
  1059. xid, src_tcon, writeable_srcfile,
  1060. src_inode->i_size, true /* no need to set sparse */);
  1061. else
  1062. rc = -ENOSYS;
  1063. cifsFileInfo_put(writeable_srcfile);
  1064. cifs_dbg(FYI, "SetFSize for copychunk rc = %d\n", rc);
  1065. }
  1066. if (rc < 0)
  1067. goto set_failed;
  1068. netfs_resize_file(&src_cifsi->netfs, src_end, true);
  1069. fscache_resize_cookie(cifs_inode_cookie(src_inode), src_end);
  1070. return 0;
  1071. set_failed:
  1072. return filemap_write_and_wait(src_inode->i_mapping);
  1073. }
  1074. /*
  1075. * Flush out either the folio that overlaps the beginning of a range in which
  1076. * pos resides or the folio that overlaps the end of a range unless that folio
  1077. * is entirely within the range we're going to invalidate. We extend the flush
  1078. * bounds to encompass the folio.
  1079. */
  1080. static int cifs_flush_folio(struct inode *inode, loff_t pos, loff_t *_fstart, loff_t *_fend,
  1081. bool first)
  1082. {
  1083. struct folio *folio;
  1084. unsigned long long fpos, fend;
  1085. pgoff_t index = pos / PAGE_SIZE;
  1086. size_t size;
  1087. int rc = 0;
  1088. folio = filemap_get_folio(inode->i_mapping, index);
  1089. if (IS_ERR(folio))
  1090. return 0;
  1091. size = folio_size(folio);
  1092. fpos = folio_pos(folio);
  1093. fend = fpos + size - 1;
  1094. *_fstart = min_t(unsigned long long, *_fstart, fpos);
  1095. *_fend = max_t(unsigned long long, *_fend, fend);
  1096. if ((first && pos == fpos) || (!first && pos == fend))
  1097. goto out;
  1098. rc = filemap_write_and_wait_range(inode->i_mapping, fpos, fend);
  1099. out:
  1100. folio_put(folio);
  1101. return rc;
  1102. }
  1103. static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
  1104. struct file *dst_file, loff_t destoff, loff_t len,
  1105. unsigned int remap_flags)
  1106. {
  1107. struct inode *src_inode = file_inode(src_file);
  1108. struct inode *target_inode = file_inode(dst_file);
  1109. struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode);
  1110. struct cifsInodeInfo *target_cifsi = CIFS_I(target_inode);
  1111. struct cifsFileInfo *smb_file_src = src_file->private_data;
  1112. struct cifsFileInfo *smb_file_target = dst_file->private_data;
  1113. struct cifs_tcon *target_tcon, *src_tcon;
  1114. unsigned long long destend, fstart, fend, old_size, new_size;
  1115. unsigned int xid;
  1116. int rc;
  1117. if (remap_flags & REMAP_FILE_DEDUP)
  1118. return -EOPNOTSUPP;
  1119. if (remap_flags & ~REMAP_FILE_ADVISORY)
  1120. return -EINVAL;
  1121. cifs_dbg(FYI, "clone range\n");
  1122. xid = get_xid();
  1123. if (!smb_file_src || !smb_file_target) {
  1124. rc = -EBADF;
  1125. cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
  1126. goto out;
  1127. }
  1128. src_tcon = tlink_tcon(smb_file_src->tlink);
  1129. target_tcon = tlink_tcon(smb_file_target->tlink);
  1130. /*
  1131. * Note: cifs case is easier than btrfs since server responsible for
  1132. * checks for proper open modes and file type and if it wants
  1133. * server could even support copy of range where source = target
  1134. */
  1135. lock_two_nondirectories(target_inode, src_inode);
  1136. if (len == 0)
  1137. len = src_inode->i_size - off;
  1138. cifs_dbg(FYI, "clone range\n");
  1139. /* Flush the source buffer */
  1140. rc = filemap_write_and_wait_range(src_inode->i_mapping, off,
  1141. off + len - 1);
  1142. if (rc)
  1143. goto unlock;
  1144. /* The server-side copy will fail if the source crosses the EOF marker.
  1145. * Advance the EOF marker after the flush above to the end of the range
  1146. * if it's short of that.
  1147. */
  1148. if (src_cifsi->netfs.remote_i_size < off + len) {
  1149. rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len);
  1150. if (rc < 0)
  1151. goto unlock;
  1152. }
  1153. new_size = destoff + len;
  1154. destend = destoff + len - 1;
  1155. /* Flush the folios at either end of the destination range to prevent
  1156. * accidental loss of dirty data outside of the range.
  1157. */
  1158. fstart = destoff;
  1159. fend = destend;
  1160. rc = cifs_flush_folio(target_inode, destoff, &fstart, &fend, true);
  1161. if (rc)
  1162. goto unlock;
  1163. rc = cifs_flush_folio(target_inode, destend, &fstart, &fend, false);
  1164. if (rc)
  1165. goto unlock;
  1166. if (fend > target_cifsi->netfs.zero_point)
  1167. target_cifsi->netfs.zero_point = fend + 1;
  1168. old_size = target_cifsi->netfs.remote_i_size;
  1169. /* Discard all the folios that overlap the destination region. */
  1170. cifs_dbg(FYI, "about to discard pages %llx-%llx\n", fstart, fend);
  1171. truncate_inode_pages_range(&target_inode->i_data, fstart, fend);
  1172. fscache_invalidate(cifs_inode_cookie(target_inode), NULL,
  1173. i_size_read(target_inode), 0);
  1174. rc = -EOPNOTSUPP;
  1175. if (target_tcon->ses->server->ops->duplicate_extents) {
  1176. rc = target_tcon->ses->server->ops->duplicate_extents(xid,
  1177. smb_file_src, smb_file_target, off, len, destoff);
  1178. if (rc == 0 && new_size > old_size) {
  1179. truncate_setsize(target_inode, new_size);
  1180. fscache_resize_cookie(cifs_inode_cookie(target_inode),
  1181. new_size);
  1182. } else if (rc == -EOPNOTSUPP) {
  1183. /*
  1184. * copy_file_range syscall man page indicates EINVAL
  1185. * is returned e.g when "fd_in and fd_out refer to the
  1186. * same file and the source and target ranges overlap."
  1187. * Test generic/157 was what showed these cases where
  1188. * we need to remap EOPNOTSUPP to EINVAL
  1189. */
  1190. if (off >= src_inode->i_size) {
  1191. rc = -EINVAL;
  1192. } else if (src_inode == target_inode) {
  1193. if (off + len > destoff)
  1194. rc = -EINVAL;
  1195. }
  1196. }
  1197. if (rc == 0 && new_size > target_cifsi->netfs.zero_point)
  1198. target_cifsi->netfs.zero_point = new_size;
  1199. }
  1200. /* force revalidate of size and timestamps of target file now
  1201. that target is updated on the server */
  1202. CIFS_I(target_inode)->time = 0;
  1203. unlock:
  1204. /* although unlocking in the reverse order from locking is not
  1205. strictly necessary here it is a little cleaner to be consistent */
  1206. unlock_two_nondirectories(src_inode, target_inode);
  1207. out:
  1208. free_xid(xid);
  1209. return rc < 0 ? rc : len;
  1210. }
  1211. ssize_t cifs_file_copychunk_range(unsigned int xid,
  1212. struct file *src_file, loff_t off,
  1213. struct file *dst_file, loff_t destoff,
  1214. size_t len, unsigned int flags)
  1215. {
  1216. struct inode *src_inode = file_inode(src_file);
  1217. struct inode *target_inode = file_inode(dst_file);
  1218. struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode);
  1219. struct cifsInodeInfo *target_cifsi = CIFS_I(target_inode);
  1220. struct cifsFileInfo *smb_file_src;
  1221. struct cifsFileInfo *smb_file_target;
  1222. struct cifs_tcon *src_tcon;
  1223. struct cifs_tcon *target_tcon;
  1224. ssize_t rc;
  1225. cifs_dbg(FYI, "copychunk range\n");
  1226. if (!src_file->private_data || !dst_file->private_data) {
  1227. rc = -EBADF;
  1228. cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
  1229. goto out;
  1230. }
  1231. rc = -EXDEV;
  1232. smb_file_target = dst_file->private_data;
  1233. smb_file_src = src_file->private_data;
  1234. src_tcon = tlink_tcon(smb_file_src->tlink);
  1235. target_tcon = tlink_tcon(smb_file_target->tlink);
  1236. if (src_tcon->ses != target_tcon->ses) {
  1237. cifs_dbg(FYI, "source and target of copy not on same server\n");
  1238. goto out;
  1239. }
  1240. rc = -EOPNOTSUPP;
  1241. if (!target_tcon->ses->server->ops->copychunk_range)
  1242. goto out;
  1243. /*
  1244. * Note: cifs case is easier than btrfs since server responsible for
  1245. * checks for proper open modes and file type and if it wants
  1246. * server could even support copy of range where source = target
  1247. */
  1248. lock_two_nondirectories(target_inode, src_inode);
  1249. cifs_dbg(FYI, "about to flush pages\n");
  1250. rc = filemap_write_and_wait_range(src_inode->i_mapping, off,
  1251. off + len - 1);
  1252. if (rc)
  1253. goto unlock;
  1254. /* The server-side copy will fail if the source crosses the EOF marker.
  1255. * Advance the EOF marker after the flush above to the end of the range
  1256. * if it's short of that.
  1257. */
  1258. if (src_cifsi->netfs.remote_i_size < off + len) {
  1259. rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len);
  1260. if (rc < 0)
  1261. goto unlock;
  1262. }
  1263. /* Flush and invalidate all the folios in the destination region. If
  1264. * the copy was successful, then some of the flush is extra overhead,
  1265. * but we need to allow for the copy failing in some way (eg. ENOSPC).
  1266. */
  1267. rc = filemap_invalidate_inode(target_inode, true, destoff, destoff + len - 1);
  1268. if (rc)
  1269. goto unlock;
  1270. fscache_invalidate(cifs_inode_cookie(target_inode), NULL,
  1271. i_size_read(target_inode), 0);
  1272. rc = file_modified(dst_file);
  1273. if (!rc) {
  1274. rc = target_tcon->ses->server->ops->copychunk_range(xid,
  1275. smb_file_src, smb_file_target, off, len, destoff);
  1276. if (rc > 0 && destoff + rc > i_size_read(target_inode)) {
  1277. truncate_setsize(target_inode, destoff + rc);
  1278. netfs_resize_file(&target_cifsi->netfs,
  1279. i_size_read(target_inode), true);
  1280. fscache_resize_cookie(cifs_inode_cookie(target_inode),
  1281. i_size_read(target_inode));
  1282. }
  1283. if (rc > 0 && destoff + rc > target_cifsi->netfs.zero_point)
  1284. target_cifsi->netfs.zero_point = destoff + rc;
  1285. }
  1286. file_accessed(src_file);
  1287. /* force revalidate of size and timestamps of target file now
  1288. * that target is updated on the server
  1289. */
  1290. CIFS_I(target_inode)->time = 0;
  1291. unlock:
  1292. /* although unlocking in the reverse order from locking is not
  1293. * strictly necessary here it is a little cleaner to be consistent
  1294. */
  1295. unlock_two_nondirectories(src_inode, target_inode);
  1296. out:
  1297. return rc;
  1298. }
  1299. /*
  1300. * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
  1301. * is a dummy operation.
  1302. */
  1303. static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  1304. {
  1305. cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
  1306. file, datasync);
  1307. return 0;
  1308. }
  1309. static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
  1310. struct file *dst_file, loff_t destoff,
  1311. size_t len, unsigned int flags)
  1312. {
  1313. unsigned int xid = get_xid();
  1314. ssize_t rc;
  1315. struct cifsFileInfo *cfile = dst_file->private_data;
  1316. if (cfile->swapfile) {
  1317. rc = -EOPNOTSUPP;
  1318. free_xid(xid);
  1319. return rc;
  1320. }
  1321. rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
  1322. len, flags);
  1323. free_xid(xid);
  1324. if (rc == -EOPNOTSUPP || rc == -EXDEV)
  1325. rc = splice_copy_file_range(src_file, off, dst_file,
  1326. destoff, len);
  1327. return rc;
  1328. }
  1329. const struct file_operations cifs_file_ops = {
  1330. .read_iter = cifs_loose_read_iter,
  1331. .write_iter = cifs_file_write_iter,
  1332. .open = cifs_open,
  1333. .release = cifs_close,
  1334. .lock = cifs_lock,
  1335. .flock = cifs_flock,
  1336. .fsync = cifs_fsync,
  1337. .flush = cifs_flush,
  1338. .mmap = cifs_file_mmap,
  1339. .splice_read = filemap_splice_read,
  1340. .splice_write = iter_file_splice_write,
  1341. .llseek = cifs_llseek,
  1342. .unlocked_ioctl = cifs_ioctl,
  1343. .copy_file_range = cifs_copy_file_range,
  1344. .remap_file_range = cifs_remap_file_range,
  1345. .setlease = cifs_setlease,
  1346. .fallocate = cifs_fallocate,
  1347. };
  1348. const struct file_operations cifs_file_strict_ops = {
  1349. .read_iter = cifs_strict_readv,
  1350. .write_iter = cifs_strict_writev,
  1351. .open = cifs_open,
  1352. .release = cifs_close,
  1353. .lock = cifs_lock,
  1354. .flock = cifs_flock,
  1355. .fsync = cifs_strict_fsync,
  1356. .flush = cifs_flush,
  1357. .mmap = cifs_file_strict_mmap,
  1358. .splice_read = filemap_splice_read,
  1359. .splice_write = iter_file_splice_write,
  1360. .llseek = cifs_llseek,
  1361. .unlocked_ioctl = cifs_ioctl,
  1362. .copy_file_range = cifs_copy_file_range,
  1363. .remap_file_range = cifs_remap_file_range,
  1364. .setlease = cifs_setlease,
  1365. .fallocate = cifs_fallocate,
  1366. };
  1367. const struct file_operations cifs_file_direct_ops = {
  1368. .read_iter = netfs_unbuffered_read_iter,
  1369. .write_iter = netfs_file_write_iter,
  1370. .open = cifs_open,
  1371. .release = cifs_close,
  1372. .lock = cifs_lock,
  1373. .flock = cifs_flock,
  1374. .fsync = cifs_fsync,
  1375. .flush = cifs_flush,
  1376. .mmap = cifs_file_mmap,
  1377. .splice_read = copy_splice_read,
  1378. .splice_write = iter_file_splice_write,
  1379. .unlocked_ioctl = cifs_ioctl,
  1380. .copy_file_range = cifs_copy_file_range,
  1381. .remap_file_range = cifs_remap_file_range,
  1382. .llseek = cifs_llseek,
  1383. .setlease = cifs_setlease,
  1384. .fallocate = cifs_fallocate,
  1385. };
  1386. const struct file_operations cifs_file_nobrl_ops = {
  1387. .read_iter = cifs_loose_read_iter,
  1388. .write_iter = cifs_file_write_iter,
  1389. .open = cifs_open,
  1390. .release = cifs_close,
  1391. .fsync = cifs_fsync,
  1392. .flush = cifs_flush,
  1393. .mmap = cifs_file_mmap,
  1394. .splice_read = filemap_splice_read,
  1395. .splice_write = iter_file_splice_write,
  1396. .llseek = cifs_llseek,
  1397. .unlocked_ioctl = cifs_ioctl,
  1398. .copy_file_range = cifs_copy_file_range,
  1399. .remap_file_range = cifs_remap_file_range,
  1400. .setlease = cifs_setlease,
  1401. .fallocate = cifs_fallocate,
  1402. };
  1403. const struct file_operations cifs_file_strict_nobrl_ops = {
  1404. .read_iter = cifs_strict_readv,
  1405. .write_iter = cifs_strict_writev,
  1406. .open = cifs_open,
  1407. .release = cifs_close,
  1408. .fsync = cifs_strict_fsync,
  1409. .flush = cifs_flush,
  1410. .mmap = cifs_file_strict_mmap,
  1411. .splice_read = filemap_splice_read,
  1412. .splice_write = iter_file_splice_write,
  1413. .llseek = cifs_llseek,
  1414. .unlocked_ioctl = cifs_ioctl,
  1415. .copy_file_range = cifs_copy_file_range,
  1416. .remap_file_range = cifs_remap_file_range,
  1417. .setlease = cifs_setlease,
  1418. .fallocate = cifs_fallocate,
  1419. };
  1420. const struct file_operations cifs_file_direct_nobrl_ops = {
  1421. .read_iter = netfs_unbuffered_read_iter,
  1422. .write_iter = netfs_file_write_iter,
  1423. .open = cifs_open,
  1424. .release = cifs_close,
  1425. .fsync = cifs_fsync,
  1426. .flush = cifs_flush,
  1427. .mmap = cifs_file_mmap,
  1428. .splice_read = copy_splice_read,
  1429. .splice_write = iter_file_splice_write,
  1430. .unlocked_ioctl = cifs_ioctl,
  1431. .copy_file_range = cifs_copy_file_range,
  1432. .remap_file_range = cifs_remap_file_range,
  1433. .llseek = cifs_llseek,
  1434. .setlease = cifs_setlease,
  1435. .fallocate = cifs_fallocate,
  1436. };
  1437. const struct file_operations cifs_dir_ops = {
  1438. .iterate_shared = cifs_readdir,
  1439. .release = cifs_closedir,
  1440. .read = generic_read_dir,
  1441. .unlocked_ioctl = cifs_ioctl,
  1442. .copy_file_range = cifs_copy_file_range,
  1443. .remap_file_range = cifs_remap_file_range,
  1444. .llseek = generic_file_llseek,
  1445. .fsync = cifs_dir_fsync,
  1446. };
  1447. static void
  1448. cifs_init_once(void *inode)
  1449. {
  1450. struct cifsInodeInfo *cifsi = inode;
  1451. inode_init_once(&cifsi->netfs.inode);
  1452. init_rwsem(&cifsi->lock_sem);
  1453. }
  1454. static int __init
  1455. cifs_init_inodecache(void)
  1456. {
  1457. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  1458. sizeof(struct cifsInodeInfo),
  1459. 0, (SLAB_RECLAIM_ACCOUNT|
  1460. SLAB_ACCOUNT),
  1461. cifs_init_once);
  1462. if (cifs_inode_cachep == NULL)
  1463. return -ENOMEM;
  1464. return 0;
  1465. }
  1466. static void
  1467. cifs_destroy_inodecache(void)
  1468. {
  1469. /*
  1470. * Make sure all delayed rcu free inodes are flushed before we
  1471. * destroy cache.
  1472. */
  1473. rcu_barrier();
  1474. kmem_cache_destroy(cifs_inode_cachep);
  1475. }
  1476. static int
  1477. cifs_init_request_bufs(void)
  1478. {
  1479. /*
  1480. * SMB2 maximum header size is bigger than CIFS one - no problems to
  1481. * allocate some more bytes for CIFS.
  1482. */
  1483. size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
  1484. if (CIFSMaxBufSize < 8192) {
  1485. /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
  1486. Unicode path name has to fit in any SMB/CIFS path based frames */
  1487. CIFSMaxBufSize = 8192;
  1488. } else if (CIFSMaxBufSize > 1024*127) {
  1489. CIFSMaxBufSize = 1024 * 127;
  1490. } else {
  1491. CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
  1492. }
  1493. /*
  1494. cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
  1495. CIFSMaxBufSize, CIFSMaxBufSize);
  1496. */
  1497. cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
  1498. CIFSMaxBufSize + max_hdr_size, 0,
  1499. SLAB_HWCACHE_ALIGN, 0,
  1500. CIFSMaxBufSize + max_hdr_size,
  1501. NULL);
  1502. if (cifs_req_cachep == NULL)
  1503. return -ENOMEM;
  1504. if (cifs_min_rcv < 1)
  1505. cifs_min_rcv = 1;
  1506. else if (cifs_min_rcv > 64) {
  1507. cifs_min_rcv = 64;
  1508. cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
  1509. }
  1510. cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
  1511. cifs_req_cachep);
  1512. if (cifs_req_poolp == NULL) {
  1513. kmem_cache_destroy(cifs_req_cachep);
  1514. return -ENOMEM;
  1515. }
  1516. /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
  1517. almost all handle based requests (but not write response, nor is it
  1518. sufficient for path based requests). A smaller size would have
  1519. been more efficient (compacting multiple slab items on one 4k page)
  1520. for the case in which debug was on, but this larger size allows
  1521. more SMBs to use small buffer alloc and is still much more
  1522. efficient to alloc 1 per page off the slab compared to 17K (5page)
  1523. alloc of large cifs buffers even when page debugging is on */
  1524. cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
  1525. MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
  1526. 0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
  1527. if (cifs_sm_req_cachep == NULL) {
  1528. mempool_destroy(cifs_req_poolp);
  1529. kmem_cache_destroy(cifs_req_cachep);
  1530. return -ENOMEM;
  1531. }
  1532. if (cifs_min_small < 2)
  1533. cifs_min_small = 2;
  1534. else if (cifs_min_small > 256) {
  1535. cifs_min_small = 256;
  1536. cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
  1537. }
  1538. cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
  1539. cifs_sm_req_cachep);
  1540. if (cifs_sm_req_poolp == NULL) {
  1541. mempool_destroy(cifs_req_poolp);
  1542. kmem_cache_destroy(cifs_req_cachep);
  1543. kmem_cache_destroy(cifs_sm_req_cachep);
  1544. return -ENOMEM;
  1545. }
  1546. return 0;
  1547. }
  1548. static void
  1549. cifs_destroy_request_bufs(void)
  1550. {
  1551. mempool_destroy(cifs_req_poolp);
  1552. kmem_cache_destroy(cifs_req_cachep);
  1553. mempool_destroy(cifs_sm_req_poolp);
  1554. kmem_cache_destroy(cifs_sm_req_cachep);
  1555. }
  1556. static int init_mids(void)
  1557. {
  1558. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  1559. sizeof(struct mid_q_entry), 0,
  1560. SLAB_HWCACHE_ALIGN, NULL);
  1561. if (cifs_mid_cachep == NULL)
  1562. return -ENOMEM;
  1563. /* 3 is a reasonable minimum number of simultaneous operations */
  1564. cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
  1565. if (cifs_mid_poolp == NULL) {
  1566. kmem_cache_destroy(cifs_mid_cachep);
  1567. return -ENOMEM;
  1568. }
  1569. return 0;
  1570. }
  1571. static void destroy_mids(void)
  1572. {
  1573. mempool_destroy(cifs_mid_poolp);
  1574. kmem_cache_destroy(cifs_mid_cachep);
  1575. }
  1576. static int cifs_init_netfs(void)
  1577. {
  1578. cifs_io_request_cachep =
  1579. kmem_cache_create("cifs_io_request",
  1580. sizeof(struct cifs_io_request), 0,
  1581. SLAB_HWCACHE_ALIGN, NULL);
  1582. if (!cifs_io_request_cachep)
  1583. goto nomem_req;
  1584. if (mempool_init_slab_pool(&cifs_io_request_pool, 100, cifs_io_request_cachep) < 0)
  1585. goto nomem_reqpool;
  1586. cifs_io_subrequest_cachep =
  1587. kmem_cache_create("cifs_io_subrequest",
  1588. sizeof(struct cifs_io_subrequest), 0,
  1589. SLAB_HWCACHE_ALIGN, NULL);
  1590. if (!cifs_io_subrequest_cachep)
  1591. goto nomem_subreq;
  1592. if (mempool_init_slab_pool(&cifs_io_subrequest_pool, 100, cifs_io_subrequest_cachep) < 0)
  1593. goto nomem_subreqpool;
  1594. return 0;
  1595. nomem_subreqpool:
  1596. kmem_cache_destroy(cifs_io_subrequest_cachep);
  1597. nomem_subreq:
  1598. mempool_exit(&cifs_io_request_pool);
  1599. nomem_reqpool:
  1600. kmem_cache_destroy(cifs_io_request_cachep);
  1601. nomem_req:
  1602. return -ENOMEM;
  1603. }
  1604. static void cifs_destroy_netfs(void)
  1605. {
  1606. mempool_exit(&cifs_io_subrequest_pool);
  1607. kmem_cache_destroy(cifs_io_subrequest_cachep);
  1608. mempool_exit(&cifs_io_request_pool);
  1609. kmem_cache_destroy(cifs_io_request_cachep);
  1610. }
  1611. static int __init
  1612. init_cifs(void)
  1613. {
  1614. int rc = 0;
  1615. cifs_proc_init();
  1616. INIT_LIST_HEAD(&cifs_tcp_ses_list);
  1617. /*
  1618. * Initialize Global counters
  1619. */
  1620. atomic_set(&sesInfoAllocCount, 0);
  1621. atomic_set(&tconInfoAllocCount, 0);
  1622. atomic_set(&tcpSesNextId, 0);
  1623. atomic_set(&tcpSesAllocCount, 0);
  1624. atomic_set(&tcpSesReconnectCount, 0);
  1625. atomic_set(&tconInfoReconnectCount, 0);
  1626. atomic_set(&buf_alloc_count, 0);
  1627. atomic_set(&small_buf_alloc_count, 0);
  1628. #ifdef CONFIG_CIFS_STATS2
  1629. atomic_set(&total_buf_alloc_count, 0);
  1630. atomic_set(&total_small_buf_alloc_count, 0);
  1631. if (slow_rsp_threshold < 1)
  1632. cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
  1633. else if (slow_rsp_threshold > 32767)
  1634. cifs_dbg(VFS,
  1635. "slow response threshold set higher than recommended (0 to 32767)\n");
  1636. #endif /* CONFIG_CIFS_STATS2 */
  1637. atomic_set(&mid_count, 0);
  1638. GlobalCurrentXid = 0;
  1639. GlobalTotalActiveXid = 0;
  1640. GlobalMaxActiveXid = 0;
  1641. spin_lock_init(&cifs_tcp_ses_lock);
  1642. spin_lock_init(&GlobalMid_Lock);
  1643. cifs_lock_secret = get_random_u32();
  1644. if (cifs_max_pending < 2) {
  1645. cifs_max_pending = 2;
  1646. cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
  1647. } else if (cifs_max_pending > CIFS_MAX_REQ) {
  1648. cifs_max_pending = CIFS_MAX_REQ;
  1649. cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
  1650. CIFS_MAX_REQ);
  1651. }
  1652. /* Limit max to about 18 hours, and setting to zero disables directory entry caching */
  1653. if (dir_cache_timeout > 65000) {
  1654. dir_cache_timeout = 65000;
  1655. cifs_dbg(VFS, "dir_cache_timeout set to max of 65000 seconds\n");
  1656. }
  1657. cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1658. if (!cifsiod_wq) {
  1659. rc = -ENOMEM;
  1660. goto out_clean_proc;
  1661. }
  1662. /*
  1663. * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
  1664. * so that we don't launch too many worker threads but
  1665. * Documentation/core-api/workqueue.rst recommends setting it to 0
  1666. */
  1667. /* WQ_UNBOUND allows decrypt tasks to run on any CPU */
  1668. decrypt_wq = alloc_workqueue("smb3decryptd",
  1669. WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1670. if (!decrypt_wq) {
  1671. rc = -ENOMEM;
  1672. goto out_destroy_cifsiod_wq;
  1673. }
  1674. fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
  1675. WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1676. if (!fileinfo_put_wq) {
  1677. rc = -ENOMEM;
  1678. goto out_destroy_decrypt_wq;
  1679. }
  1680. cifsoplockd_wq = alloc_workqueue("cifsoplockd",
  1681. WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1682. if (!cifsoplockd_wq) {
  1683. rc = -ENOMEM;
  1684. goto out_destroy_fileinfo_put_wq;
  1685. }
  1686. deferredclose_wq = alloc_workqueue("deferredclose",
  1687. WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1688. if (!deferredclose_wq) {
  1689. rc = -ENOMEM;
  1690. goto out_destroy_cifsoplockd_wq;
  1691. }
  1692. serverclose_wq = alloc_workqueue("serverclose",
  1693. WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1694. if (!serverclose_wq) {
  1695. rc = -ENOMEM;
  1696. goto out_destroy_deferredclose_wq;
  1697. }
  1698. cfid_put_wq = alloc_workqueue("cfid_put_wq",
  1699. WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1700. if (!cfid_put_wq) {
  1701. rc = -ENOMEM;
  1702. goto out_destroy_serverclose_wq;
  1703. }
  1704. rc = cifs_init_inodecache();
  1705. if (rc)
  1706. goto out_destroy_cfid_put_wq;
  1707. rc = cifs_init_netfs();
  1708. if (rc)
  1709. goto out_destroy_inodecache;
  1710. rc = init_mids();
  1711. if (rc)
  1712. goto out_destroy_netfs;
  1713. rc = cifs_init_request_bufs();
  1714. if (rc)
  1715. goto out_destroy_mids;
  1716. #ifdef CONFIG_CIFS_DFS_UPCALL
  1717. rc = dfs_cache_init();
  1718. if (rc)
  1719. goto out_destroy_request_bufs;
  1720. #endif /* CONFIG_CIFS_DFS_UPCALL */
  1721. #ifdef CONFIG_CIFS_UPCALL
  1722. rc = init_cifs_spnego();
  1723. if (rc)
  1724. goto out_destroy_dfs_cache;
  1725. #endif /* CONFIG_CIFS_UPCALL */
  1726. #ifdef CONFIG_CIFS_SWN_UPCALL
  1727. rc = cifs_genl_init();
  1728. if (rc)
  1729. goto out_register_key_type;
  1730. #endif /* CONFIG_CIFS_SWN_UPCALL */
  1731. rc = init_cifs_idmap();
  1732. if (rc)
  1733. goto out_cifs_swn_init;
  1734. rc = register_filesystem(&cifs_fs_type);
  1735. if (rc)
  1736. goto out_init_cifs_idmap;
  1737. rc = register_filesystem(&smb3_fs_type);
  1738. if (rc) {
  1739. unregister_filesystem(&cifs_fs_type);
  1740. goto out_init_cifs_idmap;
  1741. }
  1742. return 0;
  1743. out_init_cifs_idmap:
  1744. exit_cifs_idmap();
  1745. out_cifs_swn_init:
  1746. #ifdef CONFIG_CIFS_SWN_UPCALL
  1747. cifs_genl_exit();
  1748. out_register_key_type:
  1749. #endif
  1750. #ifdef CONFIG_CIFS_UPCALL
  1751. exit_cifs_spnego();
  1752. out_destroy_dfs_cache:
  1753. #endif
  1754. #ifdef CONFIG_CIFS_DFS_UPCALL
  1755. dfs_cache_destroy();
  1756. out_destroy_request_bufs:
  1757. #endif
  1758. cifs_destroy_request_bufs();
  1759. out_destroy_mids:
  1760. destroy_mids();
  1761. out_destroy_netfs:
  1762. cifs_destroy_netfs();
  1763. out_destroy_inodecache:
  1764. cifs_destroy_inodecache();
  1765. out_destroy_cfid_put_wq:
  1766. destroy_workqueue(cfid_put_wq);
  1767. out_destroy_serverclose_wq:
  1768. destroy_workqueue(serverclose_wq);
  1769. out_destroy_deferredclose_wq:
  1770. destroy_workqueue(deferredclose_wq);
  1771. out_destroy_cifsoplockd_wq:
  1772. destroy_workqueue(cifsoplockd_wq);
  1773. out_destroy_fileinfo_put_wq:
  1774. destroy_workqueue(fileinfo_put_wq);
  1775. out_destroy_decrypt_wq:
  1776. destroy_workqueue(decrypt_wq);
  1777. out_destroy_cifsiod_wq:
  1778. destroy_workqueue(cifsiod_wq);
  1779. out_clean_proc:
  1780. cifs_proc_clean();
  1781. return rc;
  1782. }
  1783. static void __exit
  1784. exit_cifs(void)
  1785. {
  1786. cifs_dbg(NOISY, "exit_smb3\n");
  1787. unregister_filesystem(&cifs_fs_type);
  1788. unregister_filesystem(&smb3_fs_type);
  1789. cifs_release_automount_timer();
  1790. exit_cifs_idmap();
  1791. #ifdef CONFIG_CIFS_SWN_UPCALL
  1792. cifs_genl_exit();
  1793. #endif
  1794. #ifdef CONFIG_CIFS_UPCALL
  1795. exit_cifs_spnego();
  1796. #endif
  1797. #ifdef CONFIG_CIFS_DFS_UPCALL
  1798. dfs_cache_destroy();
  1799. #endif
  1800. cifs_destroy_request_bufs();
  1801. destroy_mids();
  1802. cifs_destroy_netfs();
  1803. cifs_destroy_inodecache();
  1804. destroy_workqueue(deferredclose_wq);
  1805. destroy_workqueue(cifsoplockd_wq);
  1806. destroy_workqueue(decrypt_wq);
  1807. destroy_workqueue(fileinfo_put_wq);
  1808. destroy_workqueue(serverclose_wq);
  1809. destroy_workqueue(cfid_put_wq);
  1810. destroy_workqueue(cifsiod_wq);
  1811. cifs_proc_clean();
  1812. }
  1813. MODULE_AUTHOR("Steve French");
  1814. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  1815. MODULE_DESCRIPTION
  1816. ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
  1817. "also older servers complying with the SNIA CIFS Specification)");
  1818. MODULE_VERSION(CIFS_VERSION);
  1819. MODULE_SOFTDEP("ecb");
  1820. MODULE_SOFTDEP("hmac");
  1821. MODULE_SOFTDEP("md5");
  1822. MODULE_SOFTDEP("nls");
  1823. MODULE_SOFTDEP("aes");
  1824. MODULE_SOFTDEP("cmac");
  1825. MODULE_SOFTDEP("sha256");
  1826. MODULE_SOFTDEP("sha512");
  1827. MODULE_SOFTDEP("aead2");
  1828. MODULE_SOFTDEP("ccm");
  1829. MODULE_SOFTDEP("gcm");
  1830. module_init(init_cifs)
  1831. module_exit(exit_cifs)