proc.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508
  1. /*
  2. * proc.c
  3. *
  4. * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
  5. * Copyright (C) 1997 by Volker Lendecke
  6. *
  7. * Please add a note about your changes to smbfs in the ChangeLog file.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/capability.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include <linux/file.h>
  15. #include <linux/stat.h>
  16. #include <linux/fcntl.h>
  17. #include <linux/dcache.h>
  18. #include <linux/dirent.h>
  19. #include <linux/nls.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/net.h>
  22. #include <linux/vfs.h>
  23. #include <linux/smb_fs.h>
  24. #include <linux/smbno.h>
  25. #include <linux/smb_mount.h>
  26. #include <net/sock.h>
  27. #include <asm/string.h>
  28. #include <asm/div64.h>
  29. #include "smb_debug.h"
  30. #include "proto.h"
  31. #include "request.h"
  32. /* Features. Undefine if they cause problems, this should perhaps be a
  33. config option. */
  34. #define SMBFS_POSIX_UNLINK 1
  35. /* Allow smb_retry to be interrupted. */
  36. #define SMB_RETRY_INTR
  37. #define SMB_VWV(packet) ((packet) + SMB_HEADER_LEN)
  38. #define SMB_CMD(packet) (*(packet+8))
  39. #define SMB_WCT(packet) (*(packet+SMB_HEADER_LEN - 1))
  40. #define SMB_DIRINFO_SIZE 43
  41. #define SMB_STATUS_SIZE 21
  42. #define SMB_ST_BLKSIZE (PAGE_SIZE)
  43. #define SMB_ST_BLKSHIFT (PAGE_SHIFT)
  44. static struct smb_ops smb_ops_core;
  45. static struct smb_ops smb_ops_os2;
  46. static struct smb_ops smb_ops_win95;
  47. static struct smb_ops smb_ops_winNT;
  48. static struct smb_ops smb_ops_unix;
  49. static struct smb_ops smb_ops_null;
  50. static void
  51. smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
  52. static void
  53. smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
  54. static int
  55. smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
  56. struct smb_fattr *fattr);
  57. static int
  58. smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
  59. struct smb_fattr *fattr);
  60. static int
  61. smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
  62. u16 attr);
  63. static int
  64. smb_proc_setattr_ext(struct smb_sb_info *server,
  65. struct inode *inode, struct smb_fattr *fattr);
  66. static int
  67. smb_proc_query_cifsunix(struct smb_sb_info *server);
  68. static void
  69. install_ops(struct smb_ops *dst, struct smb_ops *src);
  70. static void
  71. str_upper(char *name, int len)
  72. {
  73. while (len--)
  74. {
  75. if (*name >= 'a' && *name <= 'z')
  76. *name -= ('a' - 'A');
  77. name++;
  78. }
  79. }
  80. #if 0
  81. static void
  82. str_lower(char *name, int len)
  83. {
  84. while (len--)
  85. {
  86. if (*name >= 'A' && *name <= 'Z')
  87. *name += ('a' - 'A');
  88. name++;
  89. }
  90. }
  91. #endif
  92. /* reverse a string inline. This is used by the dircache walking routines */
  93. static void reverse_string(char *buf, int len)
  94. {
  95. char c;
  96. char *end = buf+len-1;
  97. while(buf < end) {
  98. c = *buf;
  99. *(buf++) = *end;
  100. *(end--) = c;
  101. }
  102. }
  103. /* no conversion, just a wrapper for memcpy. */
  104. static int convert_memcpy(unsigned char *output, int olen,
  105. const unsigned char *input, int ilen,
  106. struct nls_table *nls_from,
  107. struct nls_table *nls_to)
  108. {
  109. if (olen < ilen)
  110. return -ENAMETOOLONG;
  111. memcpy(output, input, ilen);
  112. return ilen;
  113. }
  114. static inline int write_char(unsigned char ch, char *output, int olen)
  115. {
  116. if (olen < 4)
  117. return -ENAMETOOLONG;
  118. sprintf(output, ":x%02x", ch);
  119. return 4;
  120. }
  121. static inline int write_unichar(wchar_t ch, char *output, int olen)
  122. {
  123. if (olen < 5)
  124. return -ENAMETOOLONG;
  125. sprintf(output, ":%04x", ch);
  126. return 5;
  127. }
  128. /* convert from one "codepage" to another (possibly being utf8). */
  129. static int convert_cp(unsigned char *output, int olen,
  130. const unsigned char *input, int ilen,
  131. struct nls_table *nls_from,
  132. struct nls_table *nls_to)
  133. {
  134. int len = 0;
  135. int n;
  136. wchar_t ch;
  137. while (ilen > 0) {
  138. /* convert by changing to unicode and back to the new cp */
  139. n = nls_from->char2uni(input, ilen, &ch);
  140. if (n == -EINVAL) {
  141. ilen--;
  142. n = write_char(*input++, output, olen);
  143. if (n < 0)
  144. goto fail;
  145. output += n;
  146. olen -= n;
  147. len += n;
  148. continue;
  149. } else if (n < 0)
  150. goto fail;
  151. input += n;
  152. ilen -= n;
  153. n = nls_to->uni2char(ch, output, olen);
  154. if (n == -EINVAL)
  155. n = write_unichar(ch, output, olen);
  156. if (n < 0)
  157. goto fail;
  158. output += n;
  159. olen -= n;
  160. len += n;
  161. }
  162. return len;
  163. fail:
  164. return n;
  165. }
  166. /* ----------------------------------------------------------- */
  167. /*
  168. * nls_unicode
  169. *
  170. * This encodes/decodes little endian unicode format
  171. */
  172. static int uni2char(wchar_t uni, unsigned char *out, int boundlen)
  173. {
  174. if (boundlen < 2)
  175. return -EINVAL;
  176. *out++ = uni & 0xff;
  177. *out++ = uni >> 8;
  178. return 2;
  179. }
  180. static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
  181. {
  182. if (boundlen < 2)
  183. return -EINVAL;
  184. *uni = (rawstring[1] << 8) | rawstring[0];
  185. return 2;
  186. }
  187. static struct nls_table unicode_table = {
  188. .charset = "unicode",
  189. .uni2char = uni2char,
  190. .char2uni = char2uni,
  191. };
  192. /* ----------------------------------------------------------- */
  193. static int setcodepage(struct nls_table **p, char *name)
  194. {
  195. struct nls_table *nls;
  196. if (!name || !*name) {
  197. nls = NULL;
  198. } else if ( (nls = load_nls(name)) == NULL) {
  199. printk (KERN_ERR "smbfs: failed to load nls '%s'\n", name);
  200. return -EINVAL;
  201. }
  202. /* if already set, unload the previous one. */
  203. if (*p && *p != &unicode_table)
  204. unload_nls(*p);
  205. *p = nls;
  206. return 0;
  207. }
  208. /* Handles all changes to codepage settings. */
  209. int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp)
  210. {
  211. int n = 0;
  212. smb_lock_server(server);
  213. /* Don't load any nls_* at all, if no remote is requested */
  214. if (!*cp->remote_name)
  215. goto out;
  216. /* local */
  217. n = setcodepage(&server->local_nls, cp->local_name);
  218. if (n != 0)
  219. goto out;
  220. /* remote */
  221. if (!strcmp(cp->remote_name, "unicode")) {
  222. server->remote_nls = &unicode_table;
  223. } else {
  224. n = setcodepage(&server->remote_nls, cp->remote_name);
  225. if (n != 0)
  226. setcodepage(&server->local_nls, NULL);
  227. }
  228. out:
  229. if (server->local_nls != NULL && server->remote_nls != NULL)
  230. server->ops->convert = convert_cp;
  231. else
  232. server->ops->convert = convert_memcpy;
  233. smb_unlock_server(server);
  234. return n;
  235. }
  236. /*****************************************************************************/
  237. /* */
  238. /* Encoding/Decoding section */
  239. /* */
  240. /*****************************************************************************/
  241. static __u8 *
  242. smb_encode_smb_length(__u8 * p, __u32 len)
  243. {
  244. *p = 0;
  245. *(p+1) = 0;
  246. *(p+2) = (len & 0xFF00) >> 8;
  247. *(p+3) = (len & 0xFF);
  248. if (len > 0xFFFF)
  249. {
  250. *(p+1) = 1;
  251. }
  252. return p + 4;
  253. }
  254. /*
  255. * smb_build_path: build the path to entry and name storing it in buf.
  256. * The path returned will have the trailing '\0'.
  257. */
  258. static int smb_build_path(struct smb_sb_info *server, unsigned char *buf,
  259. int maxlen,
  260. struct dentry *entry, struct qstr *name)
  261. {
  262. unsigned char *path = buf;
  263. int len;
  264. int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE) != 0;
  265. if (maxlen < (2<<unicode))
  266. return -ENAMETOOLONG;
  267. if (maxlen > SMB_MAXPATHLEN + 1)
  268. maxlen = SMB_MAXPATHLEN + 1;
  269. if (entry == NULL)
  270. goto test_name_and_out;
  271. /*
  272. * If IS_ROOT, we have to do no walking at all.
  273. */
  274. if (IS_ROOT(entry) && !name) {
  275. *path++ = '\\';
  276. if (unicode) *path++ = '\0';
  277. *path++ = '\0';
  278. if (unicode) *path++ = '\0';
  279. return path-buf;
  280. }
  281. /*
  282. * Build the path string walking the tree backward from end to ROOT
  283. * and store it in reversed order [see reverse_string()]
  284. */
  285. dget(entry);
  286. spin_lock(&entry->d_lock);
  287. while (!IS_ROOT(entry)) {
  288. struct dentry *parent;
  289. if (maxlen < (3<<unicode)) {
  290. spin_unlock(&entry->d_lock);
  291. dput(entry);
  292. return -ENAMETOOLONG;
  293. }
  294. len = server->ops->convert(path, maxlen-2,
  295. entry->d_name.name, entry->d_name.len,
  296. server->local_nls, server->remote_nls);
  297. if (len < 0) {
  298. spin_unlock(&entry->d_lock);
  299. dput(entry);
  300. return len;
  301. }
  302. reverse_string(path, len);
  303. path += len;
  304. if (unicode) {
  305. /* Note: reverse order */
  306. *path++ = '\0';
  307. maxlen--;
  308. }
  309. *path++ = '\\';
  310. maxlen -= len+1;
  311. parent = entry->d_parent;
  312. dget(parent);
  313. spin_unlock(&entry->d_lock);
  314. dput(entry);
  315. entry = parent;
  316. spin_lock(&entry->d_lock);
  317. }
  318. spin_unlock(&entry->d_lock);
  319. dput(entry);
  320. reverse_string(buf, path-buf);
  321. /* maxlen has space for at least one char */
  322. test_name_and_out:
  323. if (name) {
  324. if (maxlen < (3<<unicode))
  325. return -ENAMETOOLONG;
  326. *path++ = '\\';
  327. if (unicode) {
  328. *path++ = '\0';
  329. maxlen--;
  330. }
  331. len = server->ops->convert(path, maxlen-2,
  332. name->name, name->len,
  333. server->local_nls, server->remote_nls);
  334. if (len < 0)
  335. return len;
  336. path += len;
  337. maxlen -= len+1;
  338. }
  339. /* maxlen has space for at least one char */
  340. *path++ = '\0';
  341. if (unicode) *path++ = '\0';
  342. return path-buf;
  343. }
  344. static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen,
  345. struct dentry *dir, struct qstr *name)
  346. {
  347. int result;
  348. result = smb_build_path(server, buf, maxlen, dir, name);
  349. if (result < 0)
  350. goto out;
  351. if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
  352. str_upper(buf, result);
  353. out:
  354. return result;
  355. }
  356. /* encode_path for non-trans2 request SMBs */
  357. static int smb_simple_encode_path(struct smb_request *req, char **p,
  358. struct dentry * entry, struct qstr * name)
  359. {
  360. struct smb_sb_info *server = req->rq_server;
  361. char *s = *p;
  362. int res;
  363. int maxlen = ((char *)req->rq_buffer + req->rq_bufsize) - s;
  364. int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
  365. if (!maxlen)
  366. return -ENAMETOOLONG;
  367. *s++ = 4; /* ASCII data format */
  368. /*
  369. * SMB Unicode strings must be 16bit aligned relative the start of the
  370. * packet. If they are not they must be padded with 0.
  371. */
  372. if (unicode) {
  373. int align = s - (char *)req->rq_buffer;
  374. if (!(align & 1)) {
  375. *s++ = '\0';
  376. maxlen--;
  377. }
  378. }
  379. res = smb_encode_path(server, s, maxlen-1, entry, name);
  380. if (res < 0)
  381. return res;
  382. *p = s + res;
  383. return 0;
  384. }
  385. /* The following are taken directly from msdos-fs */
  386. /* Linear day numbers of the respective 1sts in non-leap years. */
  387. static int day_n[] =
  388. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
  389. /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
  390. static time_t
  391. utc2local(struct smb_sb_info *server, time_t time)
  392. {
  393. return time - server->opt.serverzone*60;
  394. }
  395. static time_t
  396. local2utc(struct smb_sb_info *server, time_t time)
  397. {
  398. return time + server->opt.serverzone*60;
  399. }
  400. /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
  401. static time_t
  402. date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time)
  403. {
  404. int month, year;
  405. time_t secs;
  406. /* first subtract and mask after that... Otherwise, if
  407. date == 0, bad things happen */
  408. month = ((date >> 5) - 1) & 15;
  409. year = date >> 9;
  410. secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 *
  411. ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 &&
  412. month < 2 ? 1 : 0) + 3653);
  413. /* days since 1.1.70 plus 80's leap day */
  414. return local2utc(server, secs);
  415. }
  416. /* Convert linear UNIX date to a MS-DOS time/date pair. */
  417. static void
  418. date_unix2dos(struct smb_sb_info *server,
  419. int unix_date, __u16 *date, __u16 *time)
  420. {
  421. int day, year, nl_day, month;
  422. unix_date = utc2local(server, unix_date);
  423. if (unix_date < 315532800)
  424. unix_date = 315532800;
  425. *time = (unix_date % 60) / 2 +
  426. (((unix_date / 60) % 60) << 5) +
  427. (((unix_date / 3600) % 24) << 11);
  428. day = unix_date / 86400 - 3652;
  429. year = day / 365;
  430. if ((year + 3) / 4 + 365 * year > day)
  431. year--;
  432. day -= (year + 3) / 4 + 365 * year;
  433. if (day == 59 && !(year & 3)) {
  434. nl_day = day;
  435. month = 2;
  436. } else {
  437. nl_day = (year & 3) || day <= 59 ? day : day - 1;
  438. for (month = 0; month < 12; month++)
  439. if (day_n[month] > nl_day)
  440. break;
  441. }
  442. *date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
  443. }
  444. /* The following are taken from fs/ntfs/util.c */
  445. #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
  446. /*
  447. * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
  448. * into Unix UTC (based 1970-01-01, in seconds).
  449. */
  450. static struct timespec
  451. smb_ntutc2unixutc(u64 ntutc)
  452. {
  453. struct timespec ts;
  454. /* FIXME: what about the timezone difference? */
  455. /* Subtract the NTFS time offset, then convert to 1s intervals. */
  456. u64 t = ntutc - NTFS_TIME_OFFSET;
  457. ts.tv_nsec = do_div(t, 10000000) * 100;
  458. ts.tv_sec = t;
  459. return ts;
  460. }
  461. /* Convert the Unix UTC into NT time */
  462. static u64
  463. smb_unixutc2ntutc(struct timespec ts)
  464. {
  465. /* Note: timezone conversion is probably wrong. */
  466. /* return ((u64)utc2local(server, t)) * 10000000 + NTFS_TIME_OFFSET; */
  467. return ((u64)ts.tv_sec) * 10000000 + ts.tv_nsec/100 + NTFS_TIME_OFFSET;
  468. }
  469. #define MAX_FILE_MODE 6
  470. static mode_t file_mode[] = {
  471. S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK
  472. };
  473. static int smb_filetype_to_mode(u32 filetype)
  474. {
  475. if (filetype > MAX_FILE_MODE) {
  476. PARANOIA("Filetype out of range: %d\n", filetype);
  477. return S_IFREG;
  478. }
  479. return file_mode[filetype];
  480. }
  481. static u32 smb_filetype_from_mode(int mode)
  482. {
  483. if (S_ISREG(mode))
  484. return UNIX_TYPE_FILE;
  485. if (S_ISDIR(mode))
  486. return UNIX_TYPE_DIR;
  487. if (S_ISLNK(mode))
  488. return UNIX_TYPE_SYMLINK;
  489. if (S_ISCHR(mode))
  490. return UNIX_TYPE_CHARDEV;
  491. if (S_ISBLK(mode))
  492. return UNIX_TYPE_BLKDEV;
  493. if (S_ISFIFO(mode))
  494. return UNIX_TYPE_FIFO;
  495. if (S_ISSOCK(mode))
  496. return UNIX_TYPE_SOCKET;
  497. return UNIX_TYPE_UNKNOWN;
  498. }
  499. /*****************************************************************************/
  500. /* */
  501. /* Support section. */
  502. /* */
  503. /*****************************************************************************/
  504. __u32
  505. smb_len(__u8 * p)
  506. {
  507. return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3);
  508. }
  509. static __u16
  510. smb_bcc(__u8 * packet)
  511. {
  512. int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16);
  513. return WVAL(packet, pos);
  514. }
  515. /* smb_valid_packet: We check if packet fulfills the basic
  516. requirements of a smb packet */
  517. static int
  518. smb_valid_packet(__u8 * packet)
  519. {
  520. return (packet[4] == 0xff
  521. && packet[5] == 'S'
  522. && packet[6] == 'M'
  523. && packet[7] == 'B'
  524. && (smb_len(packet) + 4 == SMB_HEADER_LEN
  525. + SMB_WCT(packet) * 2 + smb_bcc(packet)));
  526. }
  527. /* smb_verify: We check if we got the answer we expected, and if we
  528. got enough data. If bcc == -1, we don't care. */
  529. static int
  530. smb_verify(__u8 * packet, int command, int wct, int bcc)
  531. {
  532. if (SMB_CMD(packet) != command)
  533. goto bad_command;
  534. if (SMB_WCT(packet) < wct)
  535. goto bad_wct;
  536. if (bcc != -1 && smb_bcc(packet) < bcc)
  537. goto bad_bcc;
  538. return 0;
  539. bad_command:
  540. printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n",
  541. command, SMB_CMD(packet));
  542. goto fail;
  543. bad_wct:
  544. printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
  545. command, wct, SMB_WCT(packet));
  546. goto fail;
  547. bad_bcc:
  548. printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
  549. command, bcc, smb_bcc(packet));
  550. fail:
  551. return -EIO;
  552. }
  553. /*
  554. * Returns the maximum read or write size for the "payload". Making all of the
  555. * packet fit within the negotiated max_xmit size.
  556. *
  557. * N.B. Since this value is usually computed before locking the server,
  558. * the server's packet size must never be decreased!
  559. */
  560. static inline int
  561. smb_get_xmitsize(struct smb_sb_info *server, int overhead)
  562. {
  563. return server->opt.max_xmit - overhead;
  564. }
  565. /*
  566. * Calculate the maximum read size
  567. */
  568. int
  569. smb_get_rsize(struct smb_sb_info *server)
  570. {
  571. /* readX has 12 parameters, read has 5 */
  572. int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2;
  573. int size = smb_get_xmitsize(server, overhead);
  574. VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
  575. return size;
  576. }
  577. /*
  578. * Calculate the maximum write size
  579. */
  580. int
  581. smb_get_wsize(struct smb_sb_info *server)
  582. {
  583. /* writeX has 14 parameters, write has 5 */
  584. int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2;
  585. int size = smb_get_xmitsize(server, overhead);
  586. VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
  587. return size;
  588. }
  589. /*
  590. * Convert SMB error codes to -E... errno values.
  591. */
  592. int
  593. smb_errno(struct smb_request *req)
  594. {
  595. int errcls = req->rq_rcls;
  596. int error = req->rq_err;
  597. char *class = "Unknown";
  598. VERBOSE("errcls %d code %d from command 0x%x\n",
  599. errcls, error, SMB_CMD(req->rq_header));
  600. if (errcls == ERRDOS) {
  601. switch (error) {
  602. case ERRbadfunc:
  603. return -EINVAL;
  604. case ERRbadfile:
  605. case ERRbadpath:
  606. return -ENOENT;
  607. case ERRnofids:
  608. return -EMFILE;
  609. case ERRnoaccess:
  610. return -EACCES;
  611. case ERRbadfid:
  612. return -EBADF;
  613. case ERRbadmcb:
  614. return -EREMOTEIO;
  615. case ERRnomem:
  616. return -ENOMEM;
  617. case ERRbadmem:
  618. return -EFAULT;
  619. case ERRbadenv:
  620. case ERRbadformat:
  621. return -EREMOTEIO;
  622. case ERRbadaccess:
  623. return -EACCES;
  624. case ERRbaddata:
  625. return -E2BIG;
  626. case ERRbaddrive:
  627. return -ENXIO;
  628. case ERRremcd:
  629. return -EREMOTEIO;
  630. case ERRdiffdevice:
  631. return -EXDEV;
  632. case ERRnofiles:
  633. return -ENOENT;
  634. case ERRbadshare:
  635. return -ETXTBSY;
  636. case ERRlock:
  637. return -EDEADLK;
  638. case ERRfilexists:
  639. return -EEXIST;
  640. case ERROR_INVALID_PARAMETER:
  641. return -EINVAL;
  642. case ERROR_DISK_FULL:
  643. return -ENOSPC;
  644. case ERROR_INVALID_NAME:
  645. return -ENOENT;
  646. case ERROR_DIR_NOT_EMPTY:
  647. return -ENOTEMPTY;
  648. case ERROR_NOT_LOCKED:
  649. return -ENOLCK;
  650. case ERROR_ALREADY_EXISTS:
  651. return -EEXIST;
  652. default:
  653. class = "ERRDOS";
  654. goto err_unknown;
  655. }
  656. } else if (errcls == ERRSRV) {
  657. switch (error) {
  658. /* N.B. This is wrong ... EIO ? */
  659. case ERRerror:
  660. return -ENFILE;
  661. case ERRbadpw:
  662. return -EINVAL;
  663. case ERRbadtype:
  664. case ERRtimeout:
  665. return -EIO;
  666. case ERRaccess:
  667. return -EACCES;
  668. /*
  669. * This is a fatal error, as it means the "tree ID"
  670. * for this connection is no longer valid. We map
  671. * to a special error code and get a new connection.
  672. */
  673. case ERRinvnid:
  674. return -EBADSLT;
  675. default:
  676. class = "ERRSRV";
  677. goto err_unknown;
  678. }
  679. } else if (errcls == ERRHRD) {
  680. switch (error) {
  681. case ERRnowrite:
  682. return -EROFS;
  683. case ERRbadunit:
  684. return -ENODEV;
  685. case ERRnotready:
  686. return -EUCLEAN;
  687. case ERRbadcmd:
  688. case ERRdata:
  689. return -EIO;
  690. case ERRbadreq:
  691. return -ERANGE;
  692. case ERRbadshare:
  693. return -ETXTBSY;
  694. case ERRlock:
  695. return -EDEADLK;
  696. case ERRdiskfull:
  697. return -ENOSPC;
  698. default:
  699. class = "ERRHRD";
  700. goto err_unknown;
  701. }
  702. } else if (errcls == ERRCMD) {
  703. class = "ERRCMD";
  704. } else if (errcls == SUCCESS) {
  705. return 0; /* This is the only valid 0 return */
  706. }
  707. err_unknown:
  708. printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n",
  709. class, error, SMB_CMD(req->rq_header));
  710. return -EIO;
  711. }
  712. /* smb_request_ok: We expect the server to be locked. Then we do the
  713. request and check the answer completely. When smb_request_ok
  714. returns 0, you can be quite sure that everything went well. When
  715. the answer is <=0, the returned number is a valid unix errno. */
  716. static int
  717. smb_request_ok(struct smb_request *req, int command, int wct, int bcc)
  718. {
  719. int result;
  720. req->rq_resp_wct = wct;
  721. req->rq_resp_bcc = bcc;
  722. result = smb_add_request(req);
  723. if (result != 0) {
  724. DEBUG1("smb_request failed\n");
  725. goto out;
  726. }
  727. if (smb_valid_packet(req->rq_header) != 0) {
  728. PARANOIA("invalid packet!\n");
  729. goto out;
  730. }
  731. result = smb_verify(req->rq_header, command, wct, bcc);
  732. out:
  733. return result;
  734. }
  735. /*
  736. * This implements the NEWCONN ioctl. It installs the server pid,
  737. * sets server->state to CONN_VALID, and wakes up the waiting process.
  738. */
  739. int
  740. smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt)
  741. {
  742. struct file *filp;
  743. struct sock *sk;
  744. int error;
  745. VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid);
  746. smb_lock_server(server);
  747. /*
  748. * Make sure we don't already have a valid connection ...
  749. */
  750. error = -EINVAL;
  751. if (server->state == CONN_VALID)
  752. goto out;
  753. error = -EACCES;
  754. if (current->uid != server->mnt->mounted_uid &&
  755. !capable(CAP_SYS_ADMIN))
  756. goto out;
  757. error = -EBADF;
  758. filp = fget(opt->fd);
  759. if (!filp)
  760. goto out;
  761. if (!smb_valid_socket(filp->f_path.dentry->d_inode))
  762. goto out_putf;
  763. server->sock_file = filp;
  764. server->conn_pid = get_pid(task_pid(current));
  765. server->opt = *opt;
  766. server->generation += 1;
  767. server->state = CONN_VALID;
  768. error = 0;
  769. if (server->conn_error) {
  770. /*
  771. * conn_error is the returncode we originally decided to
  772. * drop the old connection on. This message should be positive
  773. * and not make people ask questions on why smbfs is printing
  774. * error messages ...
  775. */
  776. printk(KERN_INFO "SMB connection re-established (%d)\n",
  777. server->conn_error);
  778. server->conn_error = 0;
  779. }
  780. /*
  781. * Store the server in sock user_data (Only used by sunrpc)
  782. */
  783. sk = SOCKET_I(filp->f_path.dentry->d_inode)->sk;
  784. sk->sk_user_data = server;
  785. /* chain into the data_ready callback */
  786. server->data_ready = xchg(&sk->sk_data_ready, smb_data_ready);
  787. /* check if we have an old smbmount that uses seconds for the
  788. serverzone */
  789. if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60)
  790. server->opt.serverzone /= 60;
  791. /* now that we have an established connection we can detect the server
  792. type and enable bug workarounds */
  793. if (server->opt.protocol < SMB_PROTOCOL_LANMAN2)
  794. install_ops(server->ops, &smb_ops_core);
  795. else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2)
  796. install_ops(server->ops, &smb_ops_os2);
  797. else if (server->opt.protocol == SMB_PROTOCOL_NT1 &&
  798. (server->opt.max_xmit < 0x1000) &&
  799. !(server->opt.capabilities & SMB_CAP_NT_SMBS)) {
  800. /* FIXME: can we kill the WIN95 flag now? */
  801. server->mnt->flags |= SMB_MOUNT_WIN95;
  802. VERBOSE("detected WIN95 server\n");
  803. install_ops(server->ops, &smb_ops_win95);
  804. } else {
  805. /*
  806. * Samba has max_xmit 65535
  807. * NT4spX has max_xmit 4536 (or something like that)
  808. * win2k has ...
  809. */
  810. VERBOSE("detected NT1 (Samba, NT4/5) server\n");
  811. install_ops(server->ops, &smb_ops_winNT);
  812. }
  813. /* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */
  814. if (server->mnt->flags & SMB_MOUNT_OLDATTR) {
  815. server->ops->getattr = smb_proc_getattr_core;
  816. } else if (server->mnt->flags & SMB_MOUNT_DIRATTR) {
  817. server->ops->getattr = smb_proc_getattr_ff;
  818. }
  819. /* Decode server capabilities */
  820. if (server->opt.capabilities & SMB_CAP_LARGE_FILES) {
  821. /* Should be ok to set this now, as no one can access the
  822. mount until the connection has been established. */
  823. SB_of(server)->s_maxbytes = ~0ULL >> 1;
  824. VERBOSE("LFS enabled\n");
  825. }
  826. if (server->opt.capabilities & SMB_CAP_UNICODE) {
  827. server->mnt->flags |= SMB_MOUNT_UNICODE;
  828. VERBOSE("Unicode enabled\n");
  829. } else {
  830. server->mnt->flags &= ~SMB_MOUNT_UNICODE;
  831. }
  832. #if 0
  833. /* flags we may test for other patches ... */
  834. if (server->opt.capabilities & SMB_CAP_LARGE_READX) {
  835. VERBOSE("Large reads enabled\n");
  836. }
  837. if (server->opt.capabilities & SMB_CAP_LARGE_WRITEX) {
  838. VERBOSE("Large writes enabled\n");
  839. }
  840. #endif
  841. if (server->opt.capabilities & SMB_CAP_UNIX) {
  842. struct inode *inode;
  843. VERBOSE("Using UNIX CIFS extensions\n");
  844. install_ops(server->ops, &smb_ops_unix);
  845. inode = SB_of(server)->s_root->d_inode;
  846. if (inode)
  847. inode->i_op = &smb_dir_inode_operations_unix;
  848. }
  849. VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n",
  850. server->opt.protocol, server->opt.max_xmit,
  851. pid_nr(server->conn_pid), server->opt.capabilities);
  852. /* FIXME: this really should be done by smbmount. */
  853. if (server->opt.max_xmit > SMB_MAX_PACKET_SIZE) {
  854. server->opt.max_xmit = SMB_MAX_PACKET_SIZE;
  855. }
  856. smb_unlock_server(server);
  857. smbiod_wake_up();
  858. if (server->opt.capabilities & SMB_CAP_UNIX)
  859. smb_proc_query_cifsunix(server);
  860. server->conn_complete++;
  861. wake_up_interruptible_all(&server->conn_wq);
  862. return error;
  863. out:
  864. smb_unlock_server(server);
  865. smbiod_wake_up();
  866. return error;
  867. out_putf:
  868. fput(filp);
  869. goto out;
  870. }
  871. /* smb_setup_header: We completely set up the packet. You only have to
  872. insert the command-specific fields */
  873. __u8 *
  874. smb_setup_header(struct smb_request *req, __u8 command, __u16 wct, __u16 bcc)
  875. {
  876. __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2;
  877. __u8 *p = req->rq_header;
  878. struct smb_sb_info *server = req->rq_server;
  879. p = smb_encode_smb_length(p, xmit_len - 4);
  880. *p++ = 0xff;
  881. *p++ = 'S';
  882. *p++ = 'M';
  883. *p++ = 'B';
  884. *p++ = command;
  885. memset(p, '\0', 19);
  886. p += 19;
  887. p += 8;
  888. if (server->opt.protocol > SMB_PROTOCOL_CORE) {
  889. int flags = SMB_FLAGS_CASELESS_PATHNAMES;
  890. int flags2 = SMB_FLAGS2_LONG_PATH_COMPONENTS |
  891. SMB_FLAGS2_EXTENDED_ATTRIBUTES; /* EA? not really ... */
  892. *(req->rq_header + smb_flg) = flags;
  893. if (server->mnt->flags & SMB_MOUNT_UNICODE)
  894. flags2 |= SMB_FLAGS2_UNICODE_STRINGS;
  895. WSET(req->rq_header, smb_flg2, flags2);
  896. }
  897. *p++ = wct; /* wct */
  898. p += 2 * wct;
  899. WSET(p, 0, bcc);
  900. /* Include the header in the data to send */
  901. req->rq_iovlen = 1;
  902. req->rq_iov[0].iov_base = req->rq_header;
  903. req->rq_iov[0].iov_len = xmit_len - bcc;
  904. return req->rq_buffer;
  905. }
  906. static void
  907. smb_setup_bcc(struct smb_request *req, __u8 *p)
  908. {
  909. u16 bcc = p - req->rq_buffer;
  910. u8 *pbcc = req->rq_header + SMB_HEADER_LEN + 2*SMB_WCT(req->rq_header);
  911. WSET(pbcc, 0, bcc);
  912. smb_encode_smb_length(req->rq_header, SMB_HEADER_LEN +
  913. 2*SMB_WCT(req->rq_header) - 2 + bcc);
  914. /* Include the "bytes" in the data to send */
  915. req->rq_iovlen = 2;
  916. req->rq_iov[1].iov_base = req->rq_buffer;
  917. req->rq_iov[1].iov_len = bcc;
  918. }
  919. static int
  920. smb_proc_seek(struct smb_sb_info *server, __u16 fileid,
  921. __u16 mode, off_t offset)
  922. {
  923. int result;
  924. struct smb_request *req;
  925. result = -ENOMEM;
  926. if (! (req = smb_alloc_request(server, 0)))
  927. goto out;
  928. smb_setup_header(req, SMBlseek, 4, 0);
  929. WSET(req->rq_header, smb_vwv0, fileid);
  930. WSET(req->rq_header, smb_vwv1, mode);
  931. DSET(req->rq_header, smb_vwv2, offset);
  932. req->rq_flags |= SMB_REQ_NORETRY;
  933. result = smb_request_ok(req, SMBlseek, 2, 0);
  934. if (result < 0) {
  935. result = 0;
  936. goto out_free;
  937. }
  938. result = DVAL(req->rq_header, smb_vwv0);
  939. out_free:
  940. smb_rput(req);
  941. out:
  942. return result;
  943. }
  944. static int
  945. smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish)
  946. {
  947. struct inode *ino = dentry->d_inode;
  948. struct smb_inode_info *ei = SMB_I(ino);
  949. int mode, read_write = 0x42, read_only = 0x40;
  950. int res;
  951. char *p;
  952. struct smb_request *req;
  953. /*
  954. * Attempt to open r/w, unless there are no write privileges.
  955. */
  956. mode = read_write;
  957. if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
  958. mode = read_only;
  959. #if 0
  960. /* FIXME: why is this code not in? below we fix it so that a caller
  961. wanting RO doesn't get RW. smb_revalidate_inode does some
  962. optimization based on access mode. tail -f needs it to be correct.
  963. We must open rw since we don't do the open if called a second time
  964. with different 'wish'. Is that not supported by smb servers? */
  965. if (!(wish & (O_WRONLY | O_RDWR)))
  966. mode = read_only;
  967. #endif
  968. res = -ENOMEM;
  969. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  970. goto out;
  971. retry:
  972. p = smb_setup_header(req, SMBopen, 2, 0);
  973. WSET(req->rq_header, smb_vwv0, mode);
  974. WSET(req->rq_header, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
  975. res = smb_simple_encode_path(req, &p, dentry, NULL);
  976. if (res < 0)
  977. goto out_free;
  978. smb_setup_bcc(req, p);
  979. res = smb_request_ok(req, SMBopen, 7, 0);
  980. if (res != 0) {
  981. if (mode == read_write &&
  982. (res == -EACCES || res == -ETXTBSY || res == -EROFS))
  983. {
  984. VERBOSE("%s/%s R/W failed, error=%d, retrying R/O\n",
  985. DENTRY_PATH(dentry), res);
  986. mode = read_only;
  987. req->rq_flags = 0;
  988. goto retry;
  989. }
  990. goto out_free;
  991. }
  992. /* We should now have data in vwv[0..6]. */
  993. ei->fileid = WVAL(req->rq_header, smb_vwv0);
  994. ei->attr = WVAL(req->rq_header, smb_vwv1);
  995. /* smb_vwv2 has mtime */
  996. /* smb_vwv4 has size */
  997. ei->access = (WVAL(req->rq_header, smb_vwv6) & SMB_ACCMASK);
  998. ei->open = server->generation;
  999. out_free:
  1000. smb_rput(req);
  1001. out:
  1002. return res;
  1003. }
  1004. /*
  1005. * Make sure the file is open, and check that the access
  1006. * is compatible with the desired access.
  1007. */
  1008. int
  1009. smb_open(struct dentry *dentry, int wish)
  1010. {
  1011. struct inode *inode = dentry->d_inode;
  1012. int result;
  1013. __u16 access;
  1014. result = -ENOENT;
  1015. if (!inode) {
  1016. printk(KERN_ERR "smb_open: no inode for dentry %s/%s\n",
  1017. DENTRY_PATH(dentry));
  1018. goto out;
  1019. }
  1020. if (!smb_is_open(inode)) {
  1021. struct smb_sb_info *server = server_from_inode(inode);
  1022. result = 0;
  1023. if (!smb_is_open(inode))
  1024. result = smb_proc_open(server, dentry, wish);
  1025. if (result)
  1026. goto out;
  1027. /*
  1028. * A successful open means the path is still valid ...
  1029. */
  1030. smb_renew_times(dentry);
  1031. }
  1032. /*
  1033. * Check whether the access is compatible with the desired mode.
  1034. */
  1035. result = 0;
  1036. access = SMB_I(inode)->access;
  1037. if (access != wish && access != SMB_O_RDWR) {
  1038. PARANOIA("%s/%s access denied, access=%x, wish=%x\n",
  1039. DENTRY_PATH(dentry), access, wish);
  1040. result = -EACCES;
  1041. }
  1042. out:
  1043. return result;
  1044. }
  1045. static int
  1046. smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
  1047. {
  1048. struct smb_request *req;
  1049. int result = -ENOMEM;
  1050. if (! (req = smb_alloc_request(server, 0)))
  1051. goto out;
  1052. smb_setup_header(req, SMBclose, 3, 0);
  1053. WSET(req->rq_header, smb_vwv0, fileid);
  1054. DSET(req->rq_header, smb_vwv1, utc2local(server, mtime));
  1055. req->rq_flags |= SMB_REQ_NORETRY;
  1056. result = smb_request_ok(req, SMBclose, 0, 0);
  1057. smb_rput(req);
  1058. out:
  1059. return result;
  1060. }
  1061. /*
  1062. * Win NT 4.0 has an apparent bug in that it fails to update the
  1063. * modify time when writing to a file. As a workaround, we update
  1064. * both modify and access time locally, and post the times to the
  1065. * server when closing the file.
  1066. */
  1067. static int
  1068. smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino)
  1069. {
  1070. struct smb_inode_info *ei = SMB_I(ino);
  1071. int result = 0;
  1072. if (smb_is_open(ino))
  1073. {
  1074. /*
  1075. * We clear the open flag in advance, in case another
  1076. * process observes the value while we block below.
  1077. */
  1078. ei->open = 0;
  1079. /*
  1080. * Kludge alert: SMB timestamps are accurate only to
  1081. * two seconds ... round the times to avoid needless
  1082. * cache invalidations!
  1083. */
  1084. if (ino->i_mtime.tv_sec & 1) {
  1085. ino->i_mtime.tv_sec--;
  1086. ino->i_mtime.tv_nsec = 0;
  1087. }
  1088. if (ino->i_atime.tv_sec & 1) {
  1089. ino->i_atime.tv_sec--;
  1090. ino->i_atime.tv_nsec = 0;
  1091. }
  1092. /*
  1093. * If the file is open with write permissions,
  1094. * update the time stamps to sync mtime and atime.
  1095. */
  1096. if ((server->opt.capabilities & SMB_CAP_UNIX) == 0 &&
  1097. (server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
  1098. !(ei->access == SMB_O_RDONLY))
  1099. {
  1100. struct smb_fattr fattr;
  1101. smb_get_inode_attr(ino, &fattr);
  1102. smb_proc_setattr_ext(server, ino, &fattr);
  1103. }
  1104. result = smb_proc_close(server, ei->fileid, ino->i_mtime.tv_sec);
  1105. /*
  1106. * Force a revalidation after closing ... some servers
  1107. * don't post the size until the file has been closed.
  1108. */
  1109. if (server->opt.protocol < SMB_PROTOCOL_NT1)
  1110. ei->oldmtime = 0;
  1111. ei->closed = jiffies;
  1112. }
  1113. return result;
  1114. }
  1115. int
  1116. smb_close(struct inode *ino)
  1117. {
  1118. int result = 0;
  1119. if (smb_is_open(ino)) {
  1120. struct smb_sb_info *server = server_from_inode(ino);
  1121. result = smb_proc_close_inode(server, ino);
  1122. }
  1123. return result;
  1124. }
  1125. /*
  1126. * This is used to close a file following a failed instantiate.
  1127. * Since we don't have an inode, we can't use any of the above.
  1128. */
  1129. int
  1130. smb_close_fileid(struct dentry *dentry, __u16 fileid)
  1131. {
  1132. struct smb_sb_info *server = server_from_dentry(dentry);
  1133. int result;
  1134. result = smb_proc_close(server, fileid, get_seconds());
  1135. return result;
  1136. }
  1137. /* In smb_proc_read and smb_proc_write we do not retry, because the
  1138. file-id would not be valid after a reconnection. */
  1139. static void
  1140. smb_proc_read_data(struct smb_request *req)
  1141. {
  1142. req->rq_iov[0].iov_base = req->rq_buffer;
  1143. req->rq_iov[0].iov_len = 3;
  1144. req->rq_iov[1].iov_base = req->rq_page;
  1145. req->rq_iov[1].iov_len = req->rq_rsize;
  1146. req->rq_iovlen = 2;
  1147. req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
  1148. }
  1149. static int
  1150. smb_proc_read(struct inode *inode, loff_t offset, int count, char *data)
  1151. {
  1152. struct smb_sb_info *server = server_from_inode(inode);
  1153. __u16 returned_count, data_len;
  1154. unsigned char *buf;
  1155. int result;
  1156. struct smb_request *req;
  1157. u8 rbuf[4];
  1158. result = -ENOMEM;
  1159. if (! (req = smb_alloc_request(server, 0)))
  1160. goto out;
  1161. smb_setup_header(req, SMBread, 5, 0);
  1162. buf = req->rq_header;
  1163. WSET(buf, smb_vwv0, SMB_I(inode)->fileid);
  1164. WSET(buf, smb_vwv1, count);
  1165. DSET(buf, smb_vwv2, offset);
  1166. WSET(buf, smb_vwv4, 0);
  1167. req->rq_page = data;
  1168. req->rq_rsize = count;
  1169. req->rq_callback = smb_proc_read_data;
  1170. req->rq_buffer = rbuf;
  1171. req->rq_flags |= SMB_REQ_NORETRY | SMB_REQ_STATIC;
  1172. result = smb_request_ok(req, SMBread, 5, -1);
  1173. if (result < 0)
  1174. goto out_free;
  1175. returned_count = WVAL(req->rq_header, smb_vwv0);
  1176. data_len = WVAL(rbuf, 1);
  1177. if (returned_count != data_len) {
  1178. printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
  1179. printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
  1180. returned_count, data_len);
  1181. }
  1182. result = data_len;
  1183. out_free:
  1184. smb_rput(req);
  1185. out:
  1186. VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
  1187. inode->i_ino, SMB_I(inode)->fileid, count, result);
  1188. return result;
  1189. }
  1190. static int
  1191. smb_proc_write(struct inode *inode, loff_t offset, int count, const char *data)
  1192. {
  1193. struct smb_sb_info *server = server_from_inode(inode);
  1194. int result;
  1195. u16 fileid = SMB_I(inode)->fileid;
  1196. u8 buf[4];
  1197. struct smb_request *req;
  1198. result = -ENOMEM;
  1199. if (! (req = smb_alloc_request(server, 0)))
  1200. goto out;
  1201. VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
  1202. inode->i_ino, fileid, count, offset);
  1203. smb_setup_header(req, SMBwrite, 5, count + 3);
  1204. WSET(req->rq_header, smb_vwv0, fileid);
  1205. WSET(req->rq_header, smb_vwv1, count);
  1206. DSET(req->rq_header, smb_vwv2, offset);
  1207. WSET(req->rq_header, smb_vwv4, 0);
  1208. buf[0] = 1;
  1209. WSET(buf, 1, count); /* yes, again ... */
  1210. req->rq_iov[1].iov_base = buf;
  1211. req->rq_iov[1].iov_len = 3;
  1212. req->rq_iov[2].iov_base = (char *) data;
  1213. req->rq_iov[2].iov_len = count;
  1214. req->rq_iovlen = 3;
  1215. req->rq_flags |= SMB_REQ_NORETRY;
  1216. result = smb_request_ok(req, SMBwrite, 1, 0);
  1217. if (result >= 0)
  1218. result = WVAL(req->rq_header, smb_vwv0);
  1219. smb_rput(req);
  1220. out:
  1221. return result;
  1222. }
  1223. /*
  1224. * In smb_proc_readX and smb_proc_writeX we do not retry, because the
  1225. * file-id would not be valid after a reconnection.
  1226. */
  1227. #define SMB_READX_MAX_PAD 64
  1228. static void
  1229. smb_proc_readX_data(struct smb_request *req)
  1230. {
  1231. /* header length, excluding the netbios length (-4) */
  1232. int hdrlen = SMB_HEADER_LEN + req->rq_resp_wct*2 - 2;
  1233. int data_off = WVAL(req->rq_header, smb_vwv6);
  1234. /*
  1235. * Some genius made the padding to the data bytes arbitrary.
  1236. * So we must first calculate the amount of padding used by the server.
  1237. */
  1238. data_off -= hdrlen;
  1239. if (data_off > SMB_READX_MAX_PAD || data_off < 0) {
  1240. PARANOIA("offset is larger than SMB_READX_MAX_PAD or negative!\n");
  1241. PARANOIA("%d > %d || %d < 0\n", data_off, SMB_READX_MAX_PAD, data_off);
  1242. req->rq_rlen = req->rq_bufsize + 1;
  1243. return;
  1244. }
  1245. req->rq_iov[0].iov_base = req->rq_buffer;
  1246. req->rq_iov[0].iov_len = data_off;
  1247. req->rq_iov[1].iov_base = req->rq_page;
  1248. req->rq_iov[1].iov_len = req->rq_rsize;
  1249. req->rq_iovlen = 2;
  1250. req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
  1251. }
  1252. static int
  1253. smb_proc_readX(struct inode *inode, loff_t offset, int count, char *data)
  1254. {
  1255. struct smb_sb_info *server = server_from_inode(inode);
  1256. unsigned char *buf;
  1257. int result;
  1258. struct smb_request *req;
  1259. static char pad[SMB_READX_MAX_PAD];
  1260. result = -ENOMEM;
  1261. if (! (req = smb_alloc_request(server, 0)))
  1262. goto out;
  1263. smb_setup_header(req, SMBreadX, 12, 0);
  1264. buf = req->rq_header;
  1265. WSET(buf, smb_vwv0, 0x00ff);
  1266. WSET(buf, smb_vwv1, 0);
  1267. WSET(buf, smb_vwv2, SMB_I(inode)->fileid);
  1268. DSET(buf, smb_vwv3, (u32)offset); /* low 32 bits */
  1269. WSET(buf, smb_vwv5, count);
  1270. WSET(buf, smb_vwv6, 0);
  1271. DSET(buf, smb_vwv7, 0);
  1272. WSET(buf, smb_vwv9, 0);
  1273. DSET(buf, smb_vwv10, (u32)(offset >> 32)); /* high 32 bits */
  1274. WSET(buf, smb_vwv11, 0);
  1275. req->rq_page = data;
  1276. req->rq_rsize = count;
  1277. req->rq_callback = smb_proc_readX_data;
  1278. req->rq_buffer = pad;
  1279. req->rq_bufsize = SMB_READX_MAX_PAD;
  1280. req->rq_flags |= SMB_REQ_STATIC | SMB_REQ_NORETRY;
  1281. result = smb_request_ok(req, SMBreadX, 12, -1);
  1282. if (result < 0)
  1283. goto out_free;
  1284. result = WVAL(req->rq_header, smb_vwv5);
  1285. out_free:
  1286. smb_rput(req);
  1287. out:
  1288. VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
  1289. inode->i_ino, SMB_I(inode)->fileid, count, result);
  1290. return result;
  1291. }
  1292. static int
  1293. smb_proc_writeX(struct inode *inode, loff_t offset, int count, const char *data)
  1294. {
  1295. struct smb_sb_info *server = server_from_inode(inode);
  1296. int result;
  1297. u8 *p;
  1298. static u8 pad[4];
  1299. struct smb_request *req;
  1300. result = -ENOMEM;
  1301. if (! (req = smb_alloc_request(server, 0)))
  1302. goto out;
  1303. VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
  1304. inode->i_ino, SMB_I(inode)->fileid, count, offset);
  1305. p = smb_setup_header(req, SMBwriteX, 14, count + 1);
  1306. WSET(req->rq_header, smb_vwv0, 0x00ff);
  1307. WSET(req->rq_header, smb_vwv1, 0);
  1308. WSET(req->rq_header, smb_vwv2, SMB_I(inode)->fileid);
  1309. DSET(req->rq_header, smb_vwv3, (u32)offset); /* low 32 bits */
  1310. DSET(req->rq_header, smb_vwv5, 0);
  1311. WSET(req->rq_header, smb_vwv7, 0); /* write mode */
  1312. WSET(req->rq_header, smb_vwv8, 0);
  1313. WSET(req->rq_header, smb_vwv9, 0);
  1314. WSET(req->rq_header, smb_vwv10, count); /* data length */
  1315. WSET(req->rq_header, smb_vwv11, smb_vwv12 + 2 + 1);
  1316. DSET(req->rq_header, smb_vwv12, (u32)(offset >> 32));
  1317. req->rq_iov[1].iov_base = pad;
  1318. req->rq_iov[1].iov_len = 1;
  1319. req->rq_iov[2].iov_base = (char *) data;
  1320. req->rq_iov[2].iov_len = count;
  1321. req->rq_iovlen = 3;
  1322. req->rq_flags |= SMB_REQ_NORETRY;
  1323. result = smb_request_ok(req, SMBwriteX, 6, 0);
  1324. if (result >= 0)
  1325. result = WVAL(req->rq_header, smb_vwv2);
  1326. smb_rput(req);
  1327. out:
  1328. return result;
  1329. }
  1330. int
  1331. smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid)
  1332. {
  1333. struct smb_sb_info *server = server_from_dentry(dentry);
  1334. char *p;
  1335. int result;
  1336. struct smb_request *req;
  1337. result = -ENOMEM;
  1338. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  1339. goto out;
  1340. p = smb_setup_header(req, SMBcreate, 3, 0);
  1341. WSET(req->rq_header, smb_vwv0, attr);
  1342. DSET(req->rq_header, smb_vwv1, utc2local(server, ctime));
  1343. result = smb_simple_encode_path(req, &p, dentry, NULL);
  1344. if (result < 0)
  1345. goto out_free;
  1346. smb_setup_bcc(req, p);
  1347. result = smb_request_ok(req, SMBcreate, 1, 0);
  1348. if (result < 0)
  1349. goto out_free;
  1350. *fileid = WVAL(req->rq_header, smb_vwv0);
  1351. result = 0;
  1352. out_free:
  1353. smb_rput(req);
  1354. out:
  1355. return result;
  1356. }
  1357. int
  1358. smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry)
  1359. {
  1360. struct smb_sb_info *server = server_from_dentry(old_dentry);
  1361. char *p;
  1362. int result;
  1363. struct smb_request *req;
  1364. result = -ENOMEM;
  1365. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  1366. goto out;
  1367. p = smb_setup_header(req, SMBmv, 1, 0);
  1368. WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN | aDIR);
  1369. result = smb_simple_encode_path(req, &p, old_dentry, NULL);
  1370. if (result < 0)
  1371. goto out_free;
  1372. result = smb_simple_encode_path(req, &p, new_dentry, NULL);
  1373. if (result < 0)
  1374. goto out_free;
  1375. smb_setup_bcc(req, p);
  1376. if ((result = smb_request_ok(req, SMBmv, 0, 0)) < 0)
  1377. goto out_free;
  1378. result = 0;
  1379. out_free:
  1380. smb_rput(req);
  1381. out:
  1382. return result;
  1383. }
  1384. /*
  1385. * Code common to mkdir and rmdir.
  1386. */
  1387. static int
  1388. smb_proc_generic_command(struct dentry *dentry, __u8 command)
  1389. {
  1390. struct smb_sb_info *server = server_from_dentry(dentry);
  1391. char *p;
  1392. int result;
  1393. struct smb_request *req;
  1394. result = -ENOMEM;
  1395. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  1396. goto out;
  1397. p = smb_setup_header(req, command, 0, 0);
  1398. result = smb_simple_encode_path(req, &p, dentry, NULL);
  1399. if (result < 0)
  1400. goto out_free;
  1401. smb_setup_bcc(req, p);
  1402. result = smb_request_ok(req, command, 0, 0);
  1403. if (result < 0)
  1404. goto out_free;
  1405. result = 0;
  1406. out_free:
  1407. smb_rput(req);
  1408. out:
  1409. return result;
  1410. }
  1411. int
  1412. smb_proc_mkdir(struct dentry *dentry)
  1413. {
  1414. return smb_proc_generic_command(dentry, SMBmkdir);
  1415. }
  1416. int
  1417. smb_proc_rmdir(struct dentry *dentry)
  1418. {
  1419. return smb_proc_generic_command(dentry, SMBrmdir);
  1420. }
  1421. #if SMBFS_POSIX_UNLINK
  1422. /*
  1423. * Removes readonly attribute from a file. Used by unlink to give posix
  1424. * semantics.
  1425. */
  1426. static int
  1427. smb_set_rw(struct dentry *dentry,struct smb_sb_info *server)
  1428. {
  1429. int result;
  1430. struct smb_fattr fattr;
  1431. /* FIXME: cifsUE should allow removing a readonly file. */
  1432. /* first get current attribute */
  1433. smb_init_dirent(server, &fattr);
  1434. result = server->ops->getattr(server, dentry, &fattr);
  1435. smb_finish_dirent(server, &fattr);
  1436. if (result < 0)
  1437. return result;
  1438. /* if RONLY attribute is set, remove it */
  1439. if (fattr.attr & aRONLY) { /* read only attribute is set */
  1440. fattr.attr &= ~aRONLY;
  1441. result = smb_proc_setattr_core(server, dentry, fattr.attr);
  1442. }
  1443. return result;
  1444. }
  1445. #endif
  1446. int
  1447. smb_proc_unlink(struct dentry *dentry)
  1448. {
  1449. struct smb_sb_info *server = server_from_dentry(dentry);
  1450. int flag = 0;
  1451. char *p;
  1452. int result;
  1453. struct smb_request *req;
  1454. result = -ENOMEM;
  1455. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  1456. goto out;
  1457. retry:
  1458. p = smb_setup_header(req, SMBunlink, 1, 0);
  1459. WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN);
  1460. result = smb_simple_encode_path(req, &p, dentry, NULL);
  1461. if (result < 0)
  1462. goto out_free;
  1463. smb_setup_bcc(req, p);
  1464. if ((result = smb_request_ok(req, SMBunlink, 0, 0)) < 0) {
  1465. #if SMBFS_POSIX_UNLINK
  1466. if (result == -EACCES && !flag) {
  1467. /* Posix semantics is for the read-only state
  1468. of a file to be ignored in unlink(). In the
  1469. SMB world a unlink() is refused on a
  1470. read-only file. To make things easier for
  1471. unix users we try to override the files
  1472. permission if the unlink fails with the
  1473. right error.
  1474. This introduces a race condition that could
  1475. lead to a file being written by someone who
  1476. shouldn't have access, but as far as I can
  1477. tell that is unavoidable */
  1478. /* remove RONLY attribute and try again */
  1479. result = smb_set_rw(dentry,server);
  1480. if (result == 0) {
  1481. flag = 1;
  1482. req->rq_flags = 0;
  1483. goto retry;
  1484. }
  1485. }
  1486. #endif
  1487. goto out_free;
  1488. }
  1489. result = 0;
  1490. out_free:
  1491. smb_rput(req);
  1492. out:
  1493. return result;
  1494. }
  1495. int
  1496. smb_proc_flush(struct smb_sb_info *server, __u16 fileid)
  1497. {
  1498. int result;
  1499. struct smb_request *req;
  1500. result = -ENOMEM;
  1501. if (! (req = smb_alloc_request(server, 0)))
  1502. goto out;
  1503. smb_setup_header(req, SMBflush, 1, 0);
  1504. WSET(req->rq_header, smb_vwv0, fileid);
  1505. req->rq_flags |= SMB_REQ_NORETRY;
  1506. result = smb_request_ok(req, SMBflush, 0, 0);
  1507. smb_rput(req);
  1508. out:
  1509. return result;
  1510. }
  1511. static int
  1512. smb_proc_trunc32(struct inode *inode, loff_t length)
  1513. {
  1514. /*
  1515. * Writing 0bytes is old-SMB magic for truncating files.
  1516. * MAX_NON_LFS should prevent this from being called with a too
  1517. * large offset.
  1518. */
  1519. return smb_proc_write(inode, length, 0, NULL);
  1520. }
  1521. static int
  1522. smb_proc_trunc64(struct inode *inode, loff_t length)
  1523. {
  1524. struct smb_sb_info *server = server_from_inode(inode);
  1525. int result;
  1526. char *param;
  1527. char *data;
  1528. struct smb_request *req;
  1529. result = -ENOMEM;
  1530. if (! (req = smb_alloc_request(server, 14)))
  1531. goto out;
  1532. param = req->rq_buffer;
  1533. data = req->rq_buffer + 6;
  1534. /* FIXME: must we also set allocation size? winNT seems to do that */
  1535. WSET(param, 0, SMB_I(inode)->fileid);
  1536. WSET(param, 2, SMB_SET_FILE_END_OF_FILE_INFO);
  1537. WSET(param, 4, 0);
  1538. LSET(data, 0, length);
  1539. req->rq_trans2_command = TRANSACT2_SETFILEINFO;
  1540. req->rq_ldata = 8;
  1541. req->rq_data = data;
  1542. req->rq_lparm = 6;
  1543. req->rq_parm = param;
  1544. req->rq_flags |= SMB_REQ_NORETRY;
  1545. result = smb_add_request(req);
  1546. if (result < 0)
  1547. goto out_free;
  1548. result = 0;
  1549. if (req->rq_rcls != 0)
  1550. result = smb_errno(req);
  1551. out_free:
  1552. smb_rput(req);
  1553. out:
  1554. return result;
  1555. }
  1556. static int
  1557. smb_proc_trunc95(struct inode *inode, loff_t length)
  1558. {
  1559. struct smb_sb_info *server = server_from_inode(inode);
  1560. int result = smb_proc_trunc32(inode, length);
  1561. /*
  1562. * win9x doesn't appear to update the size immediately.
  1563. * It will return the old file size after the truncate,
  1564. * confusing smbfs. So we force an update.
  1565. *
  1566. * FIXME: is this still necessary?
  1567. */
  1568. smb_proc_flush(server, SMB_I(inode)->fileid);
  1569. return result;
  1570. }
  1571. static void
  1572. smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
  1573. {
  1574. memset(fattr, 0, sizeof(*fattr));
  1575. fattr->f_nlink = 1;
  1576. fattr->f_uid = server->mnt->uid;
  1577. fattr->f_gid = server->mnt->gid;
  1578. fattr->f_unix = 0;
  1579. }
  1580. static void
  1581. smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
  1582. {
  1583. if (fattr->f_unix)
  1584. return;
  1585. fattr->f_mode = server->mnt->file_mode;
  1586. if (fattr->attr & aDIR) {
  1587. fattr->f_mode = server->mnt->dir_mode;
  1588. fattr->f_size = SMB_ST_BLKSIZE;
  1589. }
  1590. /* Check the read-only flag */
  1591. if (fattr->attr & aRONLY)
  1592. fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  1593. /* How many 512 byte blocks do we need for this file? */
  1594. fattr->f_blocks = 0;
  1595. if (fattr->f_size != 0)
  1596. fattr->f_blocks = 1 + ((fattr->f_size-1) >> 9);
  1597. return;
  1598. }
  1599. void
  1600. smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr,
  1601. struct super_block *sb)
  1602. {
  1603. smb_init_dirent(server, fattr);
  1604. fattr->attr = aDIR;
  1605. fattr->f_ino = 2; /* traditional root inode number */
  1606. fattr->f_mtime = current_fs_time(sb);
  1607. smb_finish_dirent(server, fattr);
  1608. }
  1609. /*
  1610. * Decode a dirent for old protocols
  1611. *
  1612. * qname is filled with the decoded, and possibly translated, name.
  1613. * fattr receives decoded attributes
  1614. *
  1615. * Bugs Noted:
  1616. * (1) Pathworks servers may pad the name with extra spaces.
  1617. */
  1618. static char *
  1619. smb_decode_short_dirent(struct smb_sb_info *server, char *p,
  1620. struct qstr *qname, struct smb_fattr *fattr,
  1621. unsigned char *name_buf)
  1622. {
  1623. int len;
  1624. /*
  1625. * SMB doesn't have a concept of inode numbers ...
  1626. */
  1627. smb_init_dirent(server, fattr);
  1628. fattr->f_ino = 0; /* FIXME: do we need this? */
  1629. p += SMB_STATUS_SIZE; /* reserved (search_status) */
  1630. fattr->attr = *p;
  1631. fattr->f_mtime.tv_sec = date_dos2unix(server, WVAL(p, 3), WVAL(p, 1));
  1632. fattr->f_mtime.tv_nsec = 0;
  1633. fattr->f_size = DVAL(p, 5);
  1634. fattr->f_ctime = fattr->f_mtime;
  1635. fattr->f_atime = fattr->f_mtime;
  1636. qname->name = p + 9;
  1637. len = strnlen(qname->name, 12);
  1638. /*
  1639. * Trim trailing blanks for Pathworks servers
  1640. */
  1641. while (len > 2 && qname->name[len-1] == ' ')
  1642. len--;
  1643. smb_finish_dirent(server, fattr);
  1644. #if 0
  1645. /* FIXME: These only work for ascii chars, and recent smbmount doesn't
  1646. allow the flag to be set anyway. It kills const. Remove? */
  1647. switch (server->opt.case_handling) {
  1648. case SMB_CASE_UPPER:
  1649. str_upper(entry->name, len);
  1650. break;
  1651. case SMB_CASE_LOWER:
  1652. str_lower(entry->name, len);
  1653. break;
  1654. default:
  1655. break;
  1656. }
  1657. #endif
  1658. qname->len = 0;
  1659. len = server->ops->convert(name_buf, SMB_MAXNAMELEN,
  1660. qname->name, len,
  1661. server->remote_nls, server->local_nls);
  1662. if (len > 0) {
  1663. qname->len = len;
  1664. qname->name = name_buf;
  1665. DEBUG1("len=%d, name=%.*s\n",qname->len,qname->len,qname->name);
  1666. }
  1667. return p + 22;
  1668. }
  1669. /*
  1670. * This routine is used to read in directory entries from the network.
  1671. * Note that it is for short directory name seeks, i.e.: protocol <
  1672. * SMB_PROTOCOL_LANMAN2
  1673. */
  1674. static int
  1675. smb_proc_readdir_short(struct file *filp, void *dirent, filldir_t filldir,
  1676. struct smb_cache_control *ctl)
  1677. {
  1678. struct dentry *dir = filp->f_path.dentry;
  1679. struct smb_sb_info *server = server_from_dentry(dir);
  1680. struct qstr qname;
  1681. struct smb_fattr fattr;
  1682. char *p;
  1683. int result;
  1684. int i, first, entries_seen, entries;
  1685. int entries_asked = (server->opt.max_xmit - 100) / SMB_DIRINFO_SIZE;
  1686. __u16 bcc;
  1687. __u16 count;
  1688. char status[SMB_STATUS_SIZE];
  1689. static struct qstr mask = {
  1690. .name = "*.*",
  1691. .len = 3,
  1692. };
  1693. unsigned char *last_status;
  1694. struct smb_request *req;
  1695. unsigned char *name_buf;
  1696. VERBOSE("%s/%s\n", DENTRY_PATH(dir));
  1697. lock_kernel();
  1698. result = -ENOMEM;
  1699. if (! (name_buf = kmalloc(SMB_MAXNAMELEN, GFP_KERNEL)))
  1700. goto out;
  1701. first = 1;
  1702. entries = 0;
  1703. entries_seen = 2; /* implicit . and .. */
  1704. result = -ENOMEM;
  1705. if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
  1706. goto out_name;
  1707. while (1) {
  1708. p = smb_setup_header(req, SMBsearch, 2, 0);
  1709. WSET(req->rq_header, smb_vwv0, entries_asked);
  1710. WSET(req->rq_header, smb_vwv1, aDIR);
  1711. if (first == 1) {
  1712. result = smb_simple_encode_path(req, &p, dir, &mask);
  1713. if (result < 0)
  1714. goto out_free;
  1715. if (p + 3 > (char *)req->rq_buffer + req->rq_bufsize) {
  1716. result = -ENAMETOOLONG;
  1717. goto out_free;
  1718. }
  1719. *p++ = 5;
  1720. WSET(p, 0, 0);
  1721. p += 2;
  1722. first = 0;
  1723. } else {
  1724. if (p + 5 + SMB_STATUS_SIZE >
  1725. (char *)req->rq_buffer + req->rq_bufsize) {
  1726. result = -ENAMETOOLONG;
  1727. goto out_free;
  1728. }
  1729. *p++ = 4;
  1730. *p++ = 0;
  1731. *p++ = 5;
  1732. WSET(p, 0, SMB_STATUS_SIZE);
  1733. p += 2;
  1734. memcpy(p, status, SMB_STATUS_SIZE);
  1735. p += SMB_STATUS_SIZE;
  1736. }
  1737. smb_setup_bcc(req, p);
  1738. result = smb_request_ok(req, SMBsearch, 1, -1);
  1739. if (result < 0) {
  1740. if ((req->rq_rcls == ERRDOS) &&
  1741. (req->rq_err == ERRnofiles))
  1742. break;
  1743. goto out_free;
  1744. }
  1745. count = WVAL(req->rq_header, smb_vwv0);
  1746. if (count <= 0)
  1747. break;
  1748. result = -EIO;
  1749. bcc = smb_bcc(req->rq_header);
  1750. if (bcc != count * SMB_DIRINFO_SIZE + 3)
  1751. goto out_free;
  1752. p = req->rq_buffer + 3;
  1753. /* Make sure the response fits in the buffer. Fixed sized
  1754. entries means we don't have to check in the decode loop. */
  1755. last_status = req->rq_buffer + 3 + (count-1) * SMB_DIRINFO_SIZE;
  1756. if (last_status + SMB_DIRINFO_SIZE >=
  1757. req->rq_buffer + req->rq_bufsize) {
  1758. printk(KERN_ERR "smb_proc_readdir_short: "
  1759. "last dir entry outside buffer! "
  1760. "%d@%p %d@%p\n", SMB_DIRINFO_SIZE, last_status,
  1761. req->rq_bufsize, req->rq_buffer);
  1762. goto out_free;
  1763. }
  1764. /* Read the last entry into the status field. */
  1765. memcpy(status, last_status, SMB_STATUS_SIZE);
  1766. /* Now we are ready to parse smb directory entries. */
  1767. for (i = 0; i < count; i++) {
  1768. p = smb_decode_short_dirent(server, p,
  1769. &qname, &fattr, name_buf);
  1770. if (qname.len == 0)
  1771. continue;
  1772. if (entries_seen == 2 && qname.name[0] == '.') {
  1773. if (qname.len == 1)
  1774. continue;
  1775. if (qname.name[1] == '.' && qname.len == 2)
  1776. continue;
  1777. }
  1778. if (!smb_fill_cache(filp, dirent, filldir, ctl,
  1779. &qname, &fattr))
  1780. ; /* stop reading? */
  1781. entries_seen++;
  1782. }
  1783. }
  1784. result = entries;
  1785. out_free:
  1786. smb_rput(req);
  1787. out_name:
  1788. kfree(name_buf);
  1789. out:
  1790. unlock_kernel();
  1791. return result;
  1792. }
  1793. static void smb_decode_unix_basic(struct smb_fattr *fattr, struct smb_sb_info *server, char *p)
  1794. {
  1795. u64 size, disk_bytes;
  1796. /* FIXME: verify nls support. all is sent as utf8? */
  1797. fattr->f_unix = 1;
  1798. fattr->f_mode = 0;
  1799. /* FIXME: use the uniqueID from the remote instead? */
  1800. /* 0 L file size in bytes */
  1801. /* 8 L file size on disk in bytes (block count) */
  1802. /* 40 L uid */
  1803. /* 48 L gid */
  1804. /* 56 W file type */
  1805. /* 60 L devmajor */
  1806. /* 68 L devminor */
  1807. /* 76 L unique ID (inode) */
  1808. /* 84 L permissions */
  1809. /* 92 L link count */
  1810. size = LVAL(p, 0);
  1811. disk_bytes = LVAL(p, 8);
  1812. /*
  1813. * Some samba versions round up on-disk byte usage
  1814. * to 1MB boundaries, making it useless. When seeing
  1815. * that, use the size instead.
  1816. */
  1817. if (!(disk_bytes & 0xfffff))
  1818. disk_bytes = size+511;
  1819. fattr->f_size = size;
  1820. fattr->f_blocks = disk_bytes >> 9;
  1821. fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 16));
  1822. fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 24));
  1823. fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 32));
  1824. if (server->mnt->flags & SMB_MOUNT_UID)
  1825. fattr->f_uid = server->mnt->uid;
  1826. else
  1827. fattr->f_uid = LVAL(p, 40);
  1828. if (server->mnt->flags & SMB_MOUNT_GID)
  1829. fattr->f_gid = server->mnt->gid;
  1830. else
  1831. fattr->f_gid = LVAL(p, 48);
  1832. fattr->f_mode |= smb_filetype_to_mode(WVAL(p, 56));
  1833. if (S_ISBLK(fattr->f_mode) || S_ISCHR(fattr->f_mode)) {
  1834. __u64 major = LVAL(p, 60);
  1835. __u64 minor = LVAL(p, 68);
  1836. fattr->f_rdev = MKDEV(major & 0xffffffff, minor & 0xffffffff);
  1837. if (MAJOR(fattr->f_rdev) != (major & 0xffffffff) ||
  1838. MINOR(fattr->f_rdev) != (minor & 0xffffffff))
  1839. fattr->f_rdev = 0;
  1840. }
  1841. fattr->f_mode |= LVAL(p, 84);
  1842. if ( (server->mnt->flags & SMB_MOUNT_DMODE) &&
  1843. (S_ISDIR(fattr->f_mode)) )
  1844. fattr->f_mode = (server->mnt->dir_mode & S_IRWXUGO) | S_IFDIR;
  1845. else if ( (server->mnt->flags & SMB_MOUNT_FMODE) &&
  1846. !(S_ISDIR(fattr->f_mode)) )
  1847. fattr->f_mode = (server->mnt->file_mode & S_IRWXUGO) |
  1848. (fattr->f_mode & S_IFMT);
  1849. }
  1850. /*
  1851. * Interpret a long filename structure using the specified info level:
  1852. * level 1 for anything below NT1 protocol
  1853. * level 260 for NT1 protocol
  1854. *
  1855. * qname is filled with the decoded, and possibly translated, name
  1856. * fattr receives decoded attributes.
  1857. *
  1858. * Bugs Noted:
  1859. * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
  1860. */
  1861. static char *
  1862. smb_decode_long_dirent(struct smb_sb_info *server, char *p, int level,
  1863. struct qstr *qname, struct smb_fattr *fattr,
  1864. unsigned char *name_buf)
  1865. {
  1866. char *result;
  1867. unsigned int len = 0;
  1868. int n;
  1869. __u16 date, time;
  1870. int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
  1871. /*
  1872. * SMB doesn't have a concept of inode numbers ...
  1873. */
  1874. smb_init_dirent(server, fattr);
  1875. fattr->f_ino = 0; /* FIXME: do we need this? */
  1876. switch (level) {
  1877. case 1:
  1878. len = *((unsigned char *) p + 22);
  1879. qname->name = p + 23;
  1880. result = p + 24 + len;
  1881. date = WVAL(p, 0);
  1882. time = WVAL(p, 2);
  1883. fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
  1884. fattr->f_ctime.tv_nsec = 0;
  1885. date = WVAL(p, 4);
  1886. time = WVAL(p, 6);
  1887. fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
  1888. fattr->f_atime.tv_nsec = 0;
  1889. date = WVAL(p, 8);
  1890. time = WVAL(p, 10);
  1891. fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
  1892. fattr->f_mtime.tv_nsec = 0;
  1893. fattr->f_size = DVAL(p, 12);
  1894. /* ULONG allocation size */
  1895. fattr->attr = WVAL(p, 20);
  1896. VERBOSE("info 1 at %p, len=%d, name=%.*s\n",
  1897. p, len, len, qname->name);
  1898. break;
  1899. case 260:
  1900. result = p + WVAL(p, 0);
  1901. len = DVAL(p, 60);
  1902. if (len > 255) len = 255;
  1903. /* NT4 null terminates, unless we are using unicode ... */
  1904. qname->name = p + 94;
  1905. if (!unicode && len && qname->name[len-1] == '\0')
  1906. len--;
  1907. fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 8));
  1908. fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 16));
  1909. fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 24));
  1910. /* change time (32) */
  1911. fattr->f_size = LVAL(p, 40);
  1912. /* alloc size (48) */
  1913. fattr->attr = DVAL(p, 56);
  1914. VERBOSE("info 260 at %p, len=%d, name=%.*s\n",
  1915. p, len, len, qname->name);
  1916. break;
  1917. case SMB_FIND_FILE_UNIX:
  1918. result = p + WVAL(p, 0);
  1919. qname->name = p + 108;
  1920. len = strlen(qname->name);
  1921. /* FIXME: should we check the length?? */
  1922. p += 8;
  1923. smb_decode_unix_basic(fattr, server, p);
  1924. VERBOSE("info SMB_FIND_FILE_UNIX at %p, len=%d, name=%.*s\n",
  1925. p, len, len, qname->name);
  1926. break;
  1927. default:
  1928. PARANOIA("Unknown info level %d\n", level);
  1929. result = p + WVAL(p, 0);
  1930. goto out;
  1931. }
  1932. smb_finish_dirent(server, fattr);
  1933. #if 0
  1934. /* FIXME: These only work for ascii chars, and recent smbmount doesn't
  1935. allow the flag to be set anyway. Remove? */
  1936. switch (server->opt.case_handling) {
  1937. case SMB_CASE_UPPER:
  1938. str_upper(qname->name, len);
  1939. break;
  1940. case SMB_CASE_LOWER:
  1941. str_lower(qname->name, len);
  1942. break;
  1943. default:
  1944. break;
  1945. }
  1946. #endif
  1947. qname->len = 0;
  1948. n = server->ops->convert(name_buf, SMB_MAXNAMELEN,
  1949. qname->name, len,
  1950. server->remote_nls, server->local_nls);
  1951. if (n > 0) {
  1952. qname->len = n;
  1953. qname->name = name_buf;
  1954. }
  1955. out:
  1956. return result;
  1957. }
  1958. /* findfirst/findnext flags */
  1959. #define SMB_CLOSE_AFTER_FIRST (1<<0)
  1960. #define SMB_CLOSE_IF_END (1<<1)
  1961. #define SMB_REQUIRE_RESUME_KEY (1<<2)
  1962. #define SMB_CONTINUE_BIT (1<<3)
  1963. /*
  1964. * Note: samba-2.0.7 (at least) has a very similar routine, cli_list, in
  1965. * source/libsmb/clilist.c. When looking for smb bugs in the readdir code,
  1966. * go there for advise.
  1967. *
  1968. * Bugs Noted:
  1969. * (1) When using Info Level 1 Win NT 4.0 truncates directory listings
  1970. * for certain patterns of names and/or lengths. The breakage pattern
  1971. * is completely reproducible and can be toggled by the creation of a
  1972. * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
  1973. */
  1974. static int
  1975. smb_proc_readdir_long(struct file *filp, void *dirent, filldir_t filldir,
  1976. struct smb_cache_control *ctl)
  1977. {
  1978. struct dentry *dir = filp->f_path.dentry;
  1979. struct smb_sb_info *server = server_from_dentry(dir);
  1980. struct qstr qname;
  1981. struct smb_fattr fattr;
  1982. unsigned char *p, *lastname;
  1983. char *mask, *param;
  1984. __u16 command;
  1985. int first, entries_seen;
  1986. /* Both NT and OS/2 accept info level 1 (but see note below). */
  1987. int info_level = 260;
  1988. const int max_matches = 512;
  1989. unsigned int ff_searchcount = 0;
  1990. unsigned int ff_eos = 0;
  1991. unsigned int ff_lastname = 0;
  1992. unsigned int ff_dir_handle = 0;
  1993. unsigned int loop_count = 0;
  1994. unsigned int mask_len, i;
  1995. int result;
  1996. struct smb_request *req;
  1997. unsigned char *name_buf;
  1998. static struct qstr star = {
  1999. .name = "*",
  2000. .len = 1,
  2001. };
  2002. lock_kernel();
  2003. /*
  2004. * We always prefer unix style. Use info level 1 for older
  2005. * servers that don't do 260.
  2006. */
  2007. if (server->opt.capabilities & SMB_CAP_UNIX)
  2008. info_level = SMB_FIND_FILE_UNIX;
  2009. else if (server->opt.protocol < SMB_PROTOCOL_NT1)
  2010. info_level = 1;
  2011. result = -ENOMEM;
  2012. if (! (name_buf = kmalloc(SMB_MAXNAMELEN+2, GFP_KERNEL)))
  2013. goto out;
  2014. if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
  2015. goto out_name;
  2016. param = req->rq_buffer;
  2017. /*
  2018. * Encode the initial path
  2019. */
  2020. mask = param + 12;
  2021. result = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dir, &star);
  2022. if (result <= 0)
  2023. goto out_free;
  2024. mask_len = result - 1; /* mask_len is strlen, not #bytes */
  2025. result = 0;
  2026. first = 1;
  2027. VERBOSE("starting mask_len=%d, mask=%s\n", mask_len, mask);
  2028. entries_seen = 2;
  2029. ff_eos = 0;
  2030. while (ff_eos == 0) {
  2031. loop_count += 1;
  2032. if (loop_count > 10) {
  2033. printk(KERN_WARNING "smb_proc_readdir_long: "
  2034. "Looping in FIND_NEXT??\n");
  2035. result = -EIO;
  2036. break;
  2037. }
  2038. if (first != 0) {
  2039. command = TRANSACT2_FINDFIRST;
  2040. WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
  2041. WSET(param, 2, max_matches); /* max count */
  2042. WSET(param, 4, SMB_CLOSE_IF_END);
  2043. WSET(param, 6, info_level);
  2044. DSET(param, 8, 0);
  2045. } else {
  2046. command = TRANSACT2_FINDNEXT;
  2047. VERBOSE("handle=0x%X, lastname=%d, mask=%.*s\n",
  2048. ff_dir_handle, ff_lastname, mask_len, mask);
  2049. WSET(param, 0, ff_dir_handle); /* search handle */
  2050. WSET(param, 2, max_matches); /* max count */
  2051. WSET(param, 4, info_level);
  2052. DSET(param, 6, 0);
  2053. WSET(param, 10, SMB_CONTINUE_BIT|SMB_CLOSE_IF_END);
  2054. }
  2055. req->rq_trans2_command = command;
  2056. req->rq_ldata = 0;
  2057. req->rq_data = NULL;
  2058. req->rq_lparm = 12 + mask_len + 1;
  2059. req->rq_parm = param;
  2060. req->rq_flags = 0;
  2061. result = smb_add_request(req);
  2062. if (result < 0) {
  2063. PARANOIA("error=%d, breaking\n", result);
  2064. break;
  2065. }
  2066. if (req->rq_rcls == ERRSRV && req->rq_err == ERRerror) {
  2067. /* a damn Win95 bug - sometimes it clags if you
  2068. ask it too fast */
  2069. schedule_timeout_interruptible(msecs_to_jiffies(200));
  2070. continue;
  2071. }
  2072. if (req->rq_rcls != 0) {
  2073. result = smb_errno(req);
  2074. PARANOIA("name=%s, result=%d, rcls=%d, err=%d\n",
  2075. mask, result, req->rq_rcls, req->rq_err);
  2076. break;
  2077. }
  2078. /* parse out some important return info */
  2079. if (first != 0) {
  2080. ff_dir_handle = WVAL(req->rq_parm, 0);
  2081. ff_searchcount = WVAL(req->rq_parm, 2);
  2082. ff_eos = WVAL(req->rq_parm, 4);
  2083. ff_lastname = WVAL(req->rq_parm, 8);
  2084. } else {
  2085. ff_searchcount = WVAL(req->rq_parm, 0);
  2086. ff_eos = WVAL(req->rq_parm, 2);
  2087. ff_lastname = WVAL(req->rq_parm, 6);
  2088. }
  2089. if (ff_searchcount == 0)
  2090. break;
  2091. /* Now we are ready to parse smb directory entries. */
  2092. /* point to the data bytes */
  2093. p = req->rq_data;
  2094. for (i = 0; i < ff_searchcount; i++) {
  2095. /* make sure we stay within the buffer */
  2096. if (p >= req->rq_data + req->rq_ldata) {
  2097. printk(KERN_ERR "smb_proc_readdir_long: "
  2098. "dirent pointer outside buffer! "
  2099. "%p %d@%p\n",
  2100. p, req->rq_ldata, req->rq_data);
  2101. result = -EIO; /* always a comm. error? */
  2102. goto out_free;
  2103. }
  2104. p = smb_decode_long_dirent(server, p, info_level,
  2105. &qname, &fattr, name_buf);
  2106. /* ignore . and .. from the server */
  2107. if (entries_seen == 2 && qname.name[0] == '.') {
  2108. if (qname.len == 1)
  2109. continue;
  2110. if (qname.name[1] == '.' && qname.len == 2)
  2111. continue;
  2112. }
  2113. if (!smb_fill_cache(filp, dirent, filldir, ctl,
  2114. &qname, &fattr))
  2115. ; /* stop reading? */
  2116. entries_seen++;
  2117. }
  2118. VERBOSE("received %d entries, eos=%d\n", ff_searchcount,ff_eos);
  2119. /*
  2120. * We might need the lastname for continuations.
  2121. *
  2122. * Note that some servers (win95?) point to the filename and
  2123. * others (NT4, Samba using NT1) to the dir entry. We assume
  2124. * here that those who do not point to a filename do not need
  2125. * this info to continue the listing.
  2126. *
  2127. * OS/2 needs this and talks infolevel 1.
  2128. * NetApps want lastname with infolevel 260.
  2129. * win2k want lastname with infolevel 260, and points to
  2130. * the record not to the name.
  2131. * Samba+CifsUnixExt doesn't need lastname.
  2132. *
  2133. * Both are happy if we return the data they point to. So we do.
  2134. * (FIXME: above is not true with win2k)
  2135. */
  2136. mask_len = 0;
  2137. if (info_level != SMB_FIND_FILE_UNIX &&
  2138. ff_lastname > 0 && ff_lastname < req->rq_ldata) {
  2139. lastname = req->rq_data + ff_lastname;
  2140. switch (info_level) {
  2141. case 260:
  2142. mask_len = req->rq_ldata - ff_lastname;
  2143. break;
  2144. case 1:
  2145. /* lastname points to a length byte */
  2146. mask_len = *lastname++;
  2147. if (ff_lastname + 1 + mask_len > req->rq_ldata)
  2148. mask_len = req->rq_ldata - ff_lastname - 1;
  2149. break;
  2150. }
  2151. /*
  2152. * Update the mask string for the next message.
  2153. */
  2154. if (mask_len > 255)
  2155. mask_len = 255;
  2156. if (mask_len)
  2157. strncpy(mask, lastname, mask_len);
  2158. }
  2159. mask_len = strnlen(mask, mask_len);
  2160. VERBOSE("new mask, len=%d@%d of %d, mask=%.*s\n",
  2161. mask_len, ff_lastname, req->rq_ldata, mask_len, mask);
  2162. first = 0;
  2163. loop_count = 0;
  2164. }
  2165. out_free:
  2166. smb_rput(req);
  2167. out_name:
  2168. kfree(name_buf);
  2169. out:
  2170. unlock_kernel();
  2171. return result;
  2172. }
  2173. /*
  2174. * This version uses the trans2 TRANSACT2_FINDFIRST message
  2175. * to get the attribute data.
  2176. *
  2177. * Bugs Noted:
  2178. */
  2179. static int
  2180. smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
  2181. struct smb_fattr *fattr)
  2182. {
  2183. char *param, *mask;
  2184. __u16 date, time;
  2185. int mask_len, result;
  2186. struct smb_request *req;
  2187. result = -ENOMEM;
  2188. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2189. goto out;
  2190. param = req->rq_buffer;
  2191. mask = param + 12;
  2192. mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dentry,NULL);
  2193. if (mask_len < 0) {
  2194. result = mask_len;
  2195. goto out_free;
  2196. }
  2197. VERBOSE("name=%s, len=%d\n", mask, mask_len);
  2198. WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
  2199. WSET(param, 2, 1); /* max count */
  2200. WSET(param, 4, 1); /* close after this call */
  2201. WSET(param, 6, 1); /* info_level */
  2202. DSET(param, 8, 0);
  2203. req->rq_trans2_command = TRANSACT2_FINDFIRST;
  2204. req->rq_ldata = 0;
  2205. req->rq_data = NULL;
  2206. req->rq_lparm = 12 + mask_len;
  2207. req->rq_parm = param;
  2208. req->rq_flags = 0;
  2209. result = smb_add_request(req);
  2210. if (result < 0)
  2211. goto out_free;
  2212. if (req->rq_rcls != 0) {
  2213. result = smb_errno(req);
  2214. #ifdef SMBFS_PARANOIA
  2215. if (result != -ENOENT)
  2216. PARANOIA("error for %s, rcls=%d, err=%d\n",
  2217. mask, req->rq_rcls, req->rq_err);
  2218. #endif
  2219. goto out_free;
  2220. }
  2221. /* Make sure we got enough data ... */
  2222. result = -EINVAL;
  2223. if (req->rq_ldata < 22 || WVAL(req->rq_parm, 2) != 1) {
  2224. PARANOIA("bad result for %s, len=%d, count=%d\n",
  2225. mask, req->rq_ldata, WVAL(req->rq_parm, 2));
  2226. goto out_free;
  2227. }
  2228. /*
  2229. * Decode the response into the fattr ...
  2230. */
  2231. date = WVAL(req->rq_data, 0);
  2232. time = WVAL(req->rq_data, 2);
  2233. fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
  2234. fattr->f_ctime.tv_nsec = 0;
  2235. date = WVAL(req->rq_data, 4);
  2236. time = WVAL(req->rq_data, 6);
  2237. fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
  2238. fattr->f_atime.tv_nsec = 0;
  2239. date = WVAL(req->rq_data, 8);
  2240. time = WVAL(req->rq_data, 10);
  2241. fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
  2242. fattr->f_mtime.tv_nsec = 0;
  2243. VERBOSE("name=%s, date=%x, time=%x, mtime=%ld\n",
  2244. mask, date, time, fattr->f_mtime);
  2245. fattr->f_size = DVAL(req->rq_data, 12);
  2246. /* ULONG allocation size */
  2247. fattr->attr = WVAL(req->rq_data, 20);
  2248. result = 0;
  2249. out_free:
  2250. smb_rput(req);
  2251. out:
  2252. return result;
  2253. }
  2254. static int
  2255. smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
  2256. struct smb_fattr *fattr)
  2257. {
  2258. int result;
  2259. char *p;
  2260. struct smb_request *req;
  2261. result = -ENOMEM;
  2262. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2263. goto out;
  2264. p = smb_setup_header(req, SMBgetatr, 0, 0);
  2265. result = smb_simple_encode_path(req, &p, dir, NULL);
  2266. if (result < 0)
  2267. goto out_free;
  2268. smb_setup_bcc(req, p);
  2269. if ((result = smb_request_ok(req, SMBgetatr, 10, 0)) < 0)
  2270. goto out_free;
  2271. fattr->attr = WVAL(req->rq_header, smb_vwv0);
  2272. fattr->f_mtime.tv_sec = local2utc(server, DVAL(req->rq_header, smb_vwv1));
  2273. fattr->f_mtime.tv_nsec = 0;
  2274. fattr->f_size = DVAL(req->rq_header, smb_vwv3);
  2275. fattr->f_ctime = fattr->f_mtime;
  2276. fattr->f_atime = fattr->f_mtime;
  2277. #ifdef SMBFS_DEBUG_TIMESTAMP
  2278. printk("getattr_core: %s/%s, mtime=%ld\n",
  2279. DENTRY_PATH(dir), fattr->f_mtime);
  2280. #endif
  2281. result = 0;
  2282. out_free:
  2283. smb_rput(req);
  2284. out:
  2285. return result;
  2286. }
  2287. /*
  2288. * Bugs Noted:
  2289. * (1) Win 95 swaps the date and time fields in the standard info level.
  2290. */
  2291. static int
  2292. smb_proc_getattr_trans2(struct smb_sb_info *server, struct dentry *dir,
  2293. struct smb_request *req, int infolevel)
  2294. {
  2295. char *p, *param;
  2296. int result;
  2297. param = req->rq_buffer;
  2298. WSET(param, 0, infolevel);
  2299. DSET(param, 2, 0);
  2300. result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
  2301. if (result < 0)
  2302. goto out;
  2303. p = param + 6 + result;
  2304. req->rq_trans2_command = TRANSACT2_QPATHINFO;
  2305. req->rq_ldata = 0;
  2306. req->rq_data = NULL;
  2307. req->rq_lparm = p - param;
  2308. req->rq_parm = param;
  2309. req->rq_flags = 0;
  2310. result = smb_add_request(req);
  2311. if (result < 0)
  2312. goto out;
  2313. if (req->rq_rcls != 0) {
  2314. VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
  2315. &param[6], result, req->rq_rcls, req->rq_err);
  2316. result = smb_errno(req);
  2317. goto out;
  2318. }
  2319. result = -ENOENT;
  2320. if (req->rq_ldata < 22) {
  2321. PARANOIA("not enough data for %s, len=%d\n",
  2322. &param[6], req->rq_ldata);
  2323. goto out;
  2324. }
  2325. result = 0;
  2326. out:
  2327. return result;
  2328. }
  2329. static int
  2330. smb_proc_getattr_trans2_std(struct smb_sb_info *server, struct dentry *dir,
  2331. struct smb_fattr *attr)
  2332. {
  2333. u16 date, time;
  2334. int off_date = 0, off_time = 2;
  2335. int result;
  2336. struct smb_request *req;
  2337. result = -ENOMEM;
  2338. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2339. goto out;
  2340. result = smb_proc_getattr_trans2(server, dir, req, SMB_INFO_STANDARD);
  2341. if (result < 0)
  2342. goto out_free;
  2343. /*
  2344. * Kludge alert: Win 95 swaps the date and time field,
  2345. * contrary to the CIFS docs and Win NT practice.
  2346. */
  2347. if (server->mnt->flags & SMB_MOUNT_WIN95) {
  2348. off_date = 2;
  2349. off_time = 0;
  2350. }
  2351. date = WVAL(req->rq_data, off_date);
  2352. time = WVAL(req->rq_data, off_time);
  2353. attr->f_ctime.tv_sec = date_dos2unix(server, date, time);
  2354. attr->f_ctime.tv_nsec = 0;
  2355. date = WVAL(req->rq_data, 4 + off_date);
  2356. time = WVAL(req->rq_data, 4 + off_time);
  2357. attr->f_atime.tv_sec = date_dos2unix(server, date, time);
  2358. attr->f_atime.tv_nsec = 0;
  2359. date = WVAL(req->rq_data, 8 + off_date);
  2360. time = WVAL(req->rq_data, 8 + off_time);
  2361. attr->f_mtime.tv_sec = date_dos2unix(server, date, time);
  2362. attr->f_mtime.tv_nsec = 0;
  2363. #ifdef SMBFS_DEBUG_TIMESTAMP
  2364. printk(KERN_DEBUG "getattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
  2365. DENTRY_PATH(dir), date, time, attr->f_mtime);
  2366. #endif
  2367. attr->f_size = DVAL(req->rq_data, 12);
  2368. attr->attr = WVAL(req->rq_data, 20);
  2369. out_free:
  2370. smb_rput(req);
  2371. out:
  2372. return result;
  2373. }
  2374. static int
  2375. smb_proc_getattr_trans2_all(struct smb_sb_info *server, struct dentry *dir,
  2376. struct smb_fattr *attr)
  2377. {
  2378. struct smb_request *req;
  2379. int result;
  2380. result = -ENOMEM;
  2381. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2382. goto out;
  2383. result = smb_proc_getattr_trans2(server, dir, req,
  2384. SMB_QUERY_FILE_ALL_INFO);
  2385. if (result < 0)
  2386. goto out_free;
  2387. attr->f_ctime = smb_ntutc2unixutc(LVAL(req->rq_data, 0));
  2388. attr->f_atime = smb_ntutc2unixutc(LVAL(req->rq_data, 8));
  2389. attr->f_mtime = smb_ntutc2unixutc(LVAL(req->rq_data, 16));
  2390. /* change (24) */
  2391. attr->attr = WVAL(req->rq_data, 32);
  2392. /* pad? (34) */
  2393. /* allocated size (40) */
  2394. attr->f_size = LVAL(req->rq_data, 48);
  2395. out_free:
  2396. smb_rput(req);
  2397. out:
  2398. return result;
  2399. }
  2400. static int
  2401. smb_proc_getattr_unix(struct smb_sb_info *server, struct dentry *dir,
  2402. struct smb_fattr *attr)
  2403. {
  2404. struct smb_request *req;
  2405. int result;
  2406. result = -ENOMEM;
  2407. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2408. goto out;
  2409. result = smb_proc_getattr_trans2(server, dir, req,
  2410. SMB_QUERY_FILE_UNIX_BASIC);
  2411. if (result < 0)
  2412. goto out_free;
  2413. smb_decode_unix_basic(attr, server, req->rq_data);
  2414. out_free:
  2415. smb_rput(req);
  2416. out:
  2417. return result;
  2418. }
  2419. static int
  2420. smb_proc_getattr_95(struct smb_sb_info *server, struct dentry *dir,
  2421. struct smb_fattr *attr)
  2422. {
  2423. struct inode *inode = dir->d_inode;
  2424. int result;
  2425. /* FIXME: why not use the "all" version? */
  2426. result = smb_proc_getattr_trans2_std(server, dir, attr);
  2427. if (result < 0)
  2428. goto out;
  2429. /*
  2430. * None of the getattr versions here can make win9x return the right
  2431. * filesize if there are changes made to an open file.
  2432. * A seek-to-end does return the right size, but we only need to do
  2433. * that on files we have written.
  2434. */
  2435. if (inode && SMB_I(inode)->flags & SMB_F_LOCALWRITE &&
  2436. smb_is_open(inode))
  2437. {
  2438. __u16 fileid = SMB_I(inode)->fileid;
  2439. attr->f_size = smb_proc_seek(server, fileid, 2, 0);
  2440. }
  2441. out:
  2442. return result;
  2443. }
  2444. static int
  2445. smb_proc_ops_wait(struct smb_sb_info *server)
  2446. {
  2447. int result;
  2448. result = wait_event_interruptible_timeout(server->conn_wq,
  2449. server->conn_complete, 30*HZ);
  2450. if (!result || signal_pending(current))
  2451. return -EIO;
  2452. return 0;
  2453. }
  2454. static int
  2455. smb_proc_getattr_null(struct smb_sb_info *server, struct dentry *dir,
  2456. struct smb_fattr *fattr)
  2457. {
  2458. int result;
  2459. if (smb_proc_ops_wait(server) < 0)
  2460. return -EIO;
  2461. smb_init_dirent(server, fattr);
  2462. result = server->ops->getattr(server, dir, fattr);
  2463. smb_finish_dirent(server, fattr);
  2464. return result;
  2465. }
  2466. static int
  2467. smb_proc_readdir_null(struct file *filp, void *dirent, filldir_t filldir,
  2468. struct smb_cache_control *ctl)
  2469. {
  2470. struct smb_sb_info *server = server_from_dentry(filp->f_path.dentry);
  2471. if (smb_proc_ops_wait(server) < 0)
  2472. return -EIO;
  2473. return server->ops->readdir(filp, dirent, filldir, ctl);
  2474. }
  2475. int
  2476. smb_proc_getattr(struct dentry *dir, struct smb_fattr *fattr)
  2477. {
  2478. struct smb_sb_info *server = server_from_dentry(dir);
  2479. int result;
  2480. smb_init_dirent(server, fattr);
  2481. result = server->ops->getattr(server, dir, fattr);
  2482. smb_finish_dirent(server, fattr);
  2483. return result;
  2484. }
  2485. /*
  2486. * Because of bugs in the core protocol, we use this only to set
  2487. * attributes. See smb_proc_settime() below for timestamp handling.
  2488. *
  2489. * Bugs Noted:
  2490. * (1) If mtime is non-zero, both Win 3.1 and Win 95 fail
  2491. * with an undocumented error (ERRDOS code 50). Setting
  2492. * mtime to 0 allows the attributes to be set.
  2493. * (2) The extra parameters following the name string aren't
  2494. * in the CIFS docs, but seem to be necessary for operation.
  2495. */
  2496. static int
  2497. smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
  2498. __u16 attr)
  2499. {
  2500. char *p;
  2501. int result;
  2502. struct smb_request *req;
  2503. result = -ENOMEM;
  2504. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2505. goto out;
  2506. p = smb_setup_header(req, SMBsetatr, 8, 0);
  2507. WSET(req->rq_header, smb_vwv0, attr);
  2508. DSET(req->rq_header, smb_vwv1, 0); /* mtime */
  2509. WSET(req->rq_header, smb_vwv3, 0); /* reserved values */
  2510. WSET(req->rq_header, smb_vwv4, 0);
  2511. WSET(req->rq_header, smb_vwv5, 0);
  2512. WSET(req->rq_header, smb_vwv6, 0);
  2513. WSET(req->rq_header, smb_vwv7, 0);
  2514. result = smb_simple_encode_path(req, &p, dentry, NULL);
  2515. if (result < 0)
  2516. goto out_free;
  2517. if (p + 2 > (char *)req->rq_buffer + req->rq_bufsize) {
  2518. result = -ENAMETOOLONG;
  2519. goto out_free;
  2520. }
  2521. *p++ = 4;
  2522. *p++ = 0;
  2523. smb_setup_bcc(req, p);
  2524. result = smb_request_ok(req, SMBsetatr, 0, 0);
  2525. if (result < 0)
  2526. goto out_free;
  2527. result = 0;
  2528. out_free:
  2529. smb_rput(req);
  2530. out:
  2531. return result;
  2532. }
  2533. /*
  2534. * Because of bugs in the trans2 setattr messages, we must set
  2535. * attributes and timestamps separately. The core SMBsetatr
  2536. * message seems to be the only reliable way to set attributes.
  2537. */
  2538. int
  2539. smb_proc_setattr(struct dentry *dir, struct smb_fattr *fattr)
  2540. {
  2541. struct smb_sb_info *server = server_from_dentry(dir);
  2542. int result;
  2543. VERBOSE("setting %s/%s, open=%d\n",
  2544. DENTRY_PATH(dir), smb_is_open(dir->d_inode));
  2545. result = smb_proc_setattr_core(server, dir, fattr->attr);
  2546. return result;
  2547. }
  2548. /*
  2549. * Sets the timestamps for an file open with write permissions.
  2550. */
  2551. static int
  2552. smb_proc_setattr_ext(struct smb_sb_info *server,
  2553. struct inode *inode, struct smb_fattr *fattr)
  2554. {
  2555. __u16 date, time;
  2556. int result;
  2557. struct smb_request *req;
  2558. result = -ENOMEM;
  2559. if (! (req = smb_alloc_request(server, 0)))
  2560. goto out;
  2561. smb_setup_header(req, SMBsetattrE, 7, 0);
  2562. WSET(req->rq_header, smb_vwv0, SMB_I(inode)->fileid);
  2563. /* We don't change the creation time */
  2564. WSET(req->rq_header, smb_vwv1, 0);
  2565. WSET(req->rq_header, smb_vwv2, 0);
  2566. date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
  2567. WSET(req->rq_header, smb_vwv3, date);
  2568. WSET(req->rq_header, smb_vwv4, time);
  2569. date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
  2570. WSET(req->rq_header, smb_vwv5, date);
  2571. WSET(req->rq_header, smb_vwv6, time);
  2572. #ifdef SMBFS_DEBUG_TIMESTAMP
  2573. printk(KERN_DEBUG "smb_proc_setattr_ext: date=%d, time=%d, mtime=%ld\n",
  2574. date, time, fattr->f_mtime);
  2575. #endif
  2576. req->rq_flags |= SMB_REQ_NORETRY;
  2577. result = smb_request_ok(req, SMBsetattrE, 0, 0);
  2578. if (result < 0)
  2579. goto out_free;
  2580. result = 0;
  2581. out_free:
  2582. smb_rput(req);
  2583. out:
  2584. return result;
  2585. }
  2586. /*
  2587. * Bugs Noted:
  2588. * (1) The TRANSACT2_SETPATHINFO message under Win NT 4.0 doesn't
  2589. * set the file's attribute flags.
  2590. */
  2591. static int
  2592. smb_proc_setattr_trans2(struct smb_sb_info *server,
  2593. struct dentry *dir, struct smb_fattr *fattr)
  2594. {
  2595. __u16 date, time;
  2596. char *p, *param;
  2597. int result;
  2598. char data[26];
  2599. struct smb_request *req;
  2600. result = -ENOMEM;
  2601. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2602. goto out;
  2603. param = req->rq_buffer;
  2604. WSET(param, 0, 1); /* Info level SMB_INFO_STANDARD */
  2605. DSET(param, 2, 0);
  2606. result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
  2607. if (result < 0)
  2608. goto out_free;
  2609. p = param + 6 + result;
  2610. WSET(data, 0, 0); /* creation time */
  2611. WSET(data, 2, 0);
  2612. date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
  2613. WSET(data, 4, date);
  2614. WSET(data, 6, time);
  2615. date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
  2616. WSET(data, 8, date);
  2617. WSET(data, 10, time);
  2618. #ifdef SMBFS_DEBUG_TIMESTAMP
  2619. printk(KERN_DEBUG "setattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
  2620. DENTRY_PATH(dir), date, time, fattr->f_mtime);
  2621. #endif
  2622. DSET(data, 12, 0); /* size */
  2623. DSET(data, 16, 0); /* blksize */
  2624. WSET(data, 20, 0); /* attr */
  2625. DSET(data, 22, 0); /* ULONG EA size */
  2626. req->rq_trans2_command = TRANSACT2_SETPATHINFO;
  2627. req->rq_ldata = 26;
  2628. req->rq_data = data;
  2629. req->rq_lparm = p - param;
  2630. req->rq_parm = param;
  2631. req->rq_flags = 0;
  2632. result = smb_add_request(req);
  2633. if (result < 0)
  2634. goto out_free;
  2635. result = 0;
  2636. if (req->rq_rcls != 0)
  2637. result = smb_errno(req);
  2638. out_free:
  2639. smb_rput(req);
  2640. out:
  2641. return result;
  2642. }
  2643. /*
  2644. * ATTR_MODE 0x001
  2645. * ATTR_UID 0x002
  2646. * ATTR_GID 0x004
  2647. * ATTR_SIZE 0x008
  2648. * ATTR_ATIME 0x010
  2649. * ATTR_MTIME 0x020
  2650. * ATTR_CTIME 0x040
  2651. * ATTR_ATIME_SET 0x080
  2652. * ATTR_MTIME_SET 0x100
  2653. * ATTR_FORCE 0x200
  2654. * ATTR_ATTR_FLAG 0x400
  2655. *
  2656. * major/minor should only be set by mknod.
  2657. */
  2658. int
  2659. smb_proc_setattr_unix(struct dentry *d, struct iattr *attr,
  2660. unsigned int major, unsigned int minor)
  2661. {
  2662. struct smb_sb_info *server = server_from_dentry(d);
  2663. u64 nttime;
  2664. char *p, *param;
  2665. int result;
  2666. char data[100];
  2667. struct smb_request *req;
  2668. result = -ENOMEM;
  2669. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2670. goto out;
  2671. param = req->rq_buffer;
  2672. DEBUG1("valid flags = 0x%04x\n", attr->ia_valid);
  2673. WSET(param, 0, SMB_SET_FILE_UNIX_BASIC);
  2674. DSET(param, 2, 0);
  2675. result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
  2676. if (result < 0)
  2677. goto out_free;
  2678. p = param + 6 + result;
  2679. /* 0 L file size in bytes */
  2680. /* 8 L file size on disk in bytes (block count) */
  2681. /* 40 L uid */
  2682. /* 48 L gid */
  2683. /* 56 W file type enum */
  2684. /* 60 L devmajor */
  2685. /* 68 L devminor */
  2686. /* 76 L unique ID (inode) */
  2687. /* 84 L permissions */
  2688. /* 92 L link count */
  2689. LSET(data, 0, SMB_SIZE_NO_CHANGE);
  2690. LSET(data, 8, SMB_SIZE_NO_CHANGE);
  2691. LSET(data, 16, SMB_TIME_NO_CHANGE);
  2692. LSET(data, 24, SMB_TIME_NO_CHANGE);
  2693. LSET(data, 32, SMB_TIME_NO_CHANGE);
  2694. LSET(data, 40, SMB_UID_NO_CHANGE);
  2695. LSET(data, 48, SMB_GID_NO_CHANGE);
  2696. DSET(data, 56, smb_filetype_from_mode(attr->ia_mode));
  2697. LSET(data, 60, major);
  2698. LSET(data, 68, minor);
  2699. LSET(data, 76, 0);
  2700. LSET(data, 84, SMB_MODE_NO_CHANGE);
  2701. LSET(data, 92, 0);
  2702. if (attr->ia_valid & ATTR_SIZE) {
  2703. LSET(data, 0, attr->ia_size);
  2704. LSET(data, 8, 0); /* can't set anyway */
  2705. }
  2706. /*
  2707. * FIXME: check the conversion function it the correct one
  2708. *
  2709. * we can't set ctime but we might as well pass this to the server
  2710. * and let it ignore it.
  2711. */
  2712. if (attr->ia_valid & ATTR_CTIME) {
  2713. nttime = smb_unixutc2ntutc(attr->ia_ctime);
  2714. LSET(data, 16, nttime);
  2715. }
  2716. if (attr->ia_valid & ATTR_ATIME) {
  2717. nttime = smb_unixutc2ntutc(attr->ia_atime);
  2718. LSET(data, 24, nttime);
  2719. }
  2720. if (attr->ia_valid & ATTR_MTIME) {
  2721. nttime = smb_unixutc2ntutc(attr->ia_mtime);
  2722. LSET(data, 32, nttime);
  2723. }
  2724. if (attr->ia_valid & ATTR_UID) {
  2725. LSET(data, 40, attr->ia_uid);
  2726. }
  2727. if (attr->ia_valid & ATTR_GID) {
  2728. LSET(data, 48, attr->ia_gid);
  2729. }
  2730. if (attr->ia_valid & ATTR_MODE) {
  2731. LSET(data, 84, attr->ia_mode);
  2732. }
  2733. req->rq_trans2_command = TRANSACT2_SETPATHINFO;
  2734. req->rq_ldata = 100;
  2735. req->rq_data = data;
  2736. req->rq_lparm = p - param;
  2737. req->rq_parm = param;
  2738. req->rq_flags = 0;
  2739. result = smb_add_request(req);
  2740. out_free:
  2741. smb_rput(req);
  2742. out:
  2743. return result;
  2744. }
  2745. /*
  2746. * Set the modify and access timestamps for a file.
  2747. *
  2748. * Incredibly enough, in all of SMB there is no message to allow
  2749. * setting both attributes and timestamps at once.
  2750. *
  2751. * Bugs Noted:
  2752. * (1) Win 95 doesn't support the TRANSACT2_SETFILEINFO message
  2753. * with info level 1 (INFO_STANDARD).
  2754. * (2) Win 95 seems not to support setting directory timestamps.
  2755. * (3) Under the core protocol apparently the only way to set the
  2756. * timestamp is to open and close the file.
  2757. */
  2758. int
  2759. smb_proc_settime(struct dentry *dentry, struct smb_fattr *fattr)
  2760. {
  2761. struct smb_sb_info *server = server_from_dentry(dentry);
  2762. struct inode *inode = dentry->d_inode;
  2763. int result;
  2764. VERBOSE("setting %s/%s, open=%d\n",
  2765. DENTRY_PATH(dentry), smb_is_open(inode));
  2766. /* setting the time on a Win95 server fails (tridge) */
  2767. if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2 &&
  2768. !(server->mnt->flags & SMB_MOUNT_WIN95)) {
  2769. if (smb_is_open(inode) && SMB_I(inode)->access != SMB_O_RDONLY)
  2770. result = smb_proc_setattr_ext(server, inode, fattr);
  2771. else
  2772. result = smb_proc_setattr_trans2(server, dentry, fattr);
  2773. } else {
  2774. /*
  2775. * Fail silently on directories ... timestamp can't be set?
  2776. */
  2777. result = 0;
  2778. if (S_ISREG(inode->i_mode)) {
  2779. /*
  2780. * Set the mtime by opening and closing the file.
  2781. * Note that the file is opened read-only, but this
  2782. * still allows us to set the date (tridge)
  2783. */
  2784. result = -EACCES;
  2785. if (!smb_is_open(inode))
  2786. smb_proc_open(server, dentry, SMB_O_RDONLY);
  2787. if (smb_is_open(inode)) {
  2788. inode->i_mtime = fattr->f_mtime;
  2789. result = smb_proc_close_inode(server, inode);
  2790. }
  2791. }
  2792. }
  2793. return result;
  2794. }
  2795. int
  2796. smb_proc_dskattr(struct dentry *dentry, struct kstatfs *attr)
  2797. {
  2798. struct smb_sb_info *server = SMB_SB(dentry->d_sb);
  2799. int result;
  2800. char *p;
  2801. long unit;
  2802. struct smb_request *req;
  2803. result = -ENOMEM;
  2804. if (! (req = smb_alloc_request(server, 0)))
  2805. goto out;
  2806. smb_setup_header(req, SMBdskattr, 0, 0);
  2807. if ((result = smb_request_ok(req, SMBdskattr, 5, 0)) < 0)
  2808. goto out_free;
  2809. p = SMB_VWV(req->rq_header);
  2810. unit = (WVAL(p, 2) * WVAL(p, 4)) >> SMB_ST_BLKSHIFT;
  2811. attr->f_blocks = WVAL(p, 0) * unit;
  2812. attr->f_bsize = SMB_ST_BLKSIZE;
  2813. attr->f_bavail = attr->f_bfree = WVAL(p, 6) * unit;
  2814. result = 0;
  2815. out_free:
  2816. smb_rput(req);
  2817. out:
  2818. return result;
  2819. }
  2820. int
  2821. smb_proc_read_link(struct smb_sb_info *server, struct dentry *d,
  2822. char *buffer, int len)
  2823. {
  2824. char *p, *param;
  2825. int result;
  2826. struct smb_request *req;
  2827. DEBUG1("readlink of %s/%s\n", DENTRY_PATH(d));
  2828. result = -ENOMEM;
  2829. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2830. goto out;
  2831. param = req->rq_buffer;
  2832. WSET(param, 0, SMB_QUERY_FILE_UNIX_LINK);
  2833. DSET(param, 2, 0);
  2834. result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
  2835. if (result < 0)
  2836. goto out_free;
  2837. p = param + 6 + result;
  2838. req->rq_trans2_command = TRANSACT2_QPATHINFO;
  2839. req->rq_ldata = 0;
  2840. req->rq_data = NULL;
  2841. req->rq_lparm = p - param;
  2842. req->rq_parm = param;
  2843. req->rq_flags = 0;
  2844. result = smb_add_request(req);
  2845. if (result < 0)
  2846. goto out_free;
  2847. DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
  2848. &param[6], result, req->rq_rcls, req->rq_err);
  2849. /* copy data up to the \0 or buffer length */
  2850. result = len;
  2851. if (req->rq_ldata < len)
  2852. result = req->rq_ldata;
  2853. strncpy(buffer, req->rq_data, result);
  2854. out_free:
  2855. smb_rput(req);
  2856. out:
  2857. return result;
  2858. }
  2859. /*
  2860. * Create a symlink object called dentry which points to oldpath.
  2861. * Samba does not permit dangling links but returns a suitable error message.
  2862. */
  2863. int
  2864. smb_proc_symlink(struct smb_sb_info *server, struct dentry *d,
  2865. const char *oldpath)
  2866. {
  2867. char *p, *param;
  2868. int result;
  2869. struct smb_request *req;
  2870. result = -ENOMEM;
  2871. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2872. goto out;
  2873. param = req->rq_buffer;
  2874. WSET(param, 0, SMB_SET_FILE_UNIX_LINK);
  2875. DSET(param, 2, 0);
  2876. result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, d, NULL);
  2877. if (result < 0)
  2878. goto out_free;
  2879. p = param + 6 + result;
  2880. req->rq_trans2_command = TRANSACT2_SETPATHINFO;
  2881. req->rq_ldata = strlen(oldpath) + 1;
  2882. req->rq_data = (char *) oldpath;
  2883. req->rq_lparm = p - param;
  2884. req->rq_parm = param;
  2885. req->rq_flags = 0;
  2886. result = smb_add_request(req);
  2887. if (result < 0)
  2888. goto out_free;
  2889. DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
  2890. &param[6], result, req->rq_rcls, req->rq_err);
  2891. result = 0;
  2892. out_free:
  2893. smb_rput(req);
  2894. out:
  2895. return result;
  2896. }
  2897. /*
  2898. * Create a hard link object called new_dentry which points to dentry.
  2899. */
  2900. int
  2901. smb_proc_link(struct smb_sb_info *server, struct dentry *dentry,
  2902. struct dentry *new_dentry)
  2903. {
  2904. char *p, *param;
  2905. int result;
  2906. struct smb_request *req;
  2907. result = -ENOMEM;
  2908. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  2909. goto out;
  2910. param = req->rq_buffer;
  2911. WSET(param, 0, SMB_SET_FILE_UNIX_HLINK);
  2912. DSET(param, 2, 0);
  2913. result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1,
  2914. new_dentry, NULL);
  2915. if (result < 0)
  2916. goto out_free;
  2917. p = param + 6 + result;
  2918. /* Grr, pointless separation of parameters and data ... */
  2919. req->rq_data = p;
  2920. req->rq_ldata = smb_encode_path(server, p, SMB_MAXPATHLEN+1,
  2921. dentry, NULL);
  2922. req->rq_trans2_command = TRANSACT2_SETPATHINFO;
  2923. req->rq_lparm = p - param;
  2924. req->rq_parm = param;
  2925. req->rq_flags = 0;
  2926. result = smb_add_request(req);
  2927. if (result < 0)
  2928. goto out_free;
  2929. DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
  2930. &param[6], result, req->rq_rcls, req->rq_err);
  2931. result = 0;
  2932. out_free:
  2933. smb_rput(req);
  2934. out:
  2935. return result;
  2936. }
  2937. static int
  2938. smb_proc_query_cifsunix(struct smb_sb_info *server)
  2939. {
  2940. int result;
  2941. int major, minor;
  2942. u64 caps;
  2943. char param[2];
  2944. struct smb_request *req;
  2945. result = -ENOMEM;
  2946. if (! (req = smb_alloc_request(server, 100)))
  2947. goto out;
  2948. WSET(param, 0, SMB_QUERY_CIFS_UNIX_INFO);
  2949. req->rq_trans2_command = TRANSACT2_QFSINFO;
  2950. req->rq_ldata = 0;
  2951. req->rq_data = NULL;
  2952. req->rq_lparm = 2;
  2953. req->rq_parm = param;
  2954. req->rq_flags = 0;
  2955. result = smb_add_request(req);
  2956. if (result < 0)
  2957. goto out_free;
  2958. if (req->rq_ldata < 12) {
  2959. PARANOIA("Not enough data\n");
  2960. goto out_free;
  2961. }
  2962. major = WVAL(req->rq_data, 0);
  2963. minor = WVAL(req->rq_data, 2);
  2964. DEBUG1("Server implements CIFS Extensions for UNIX systems v%d.%d\n",
  2965. major, minor);
  2966. /* FIXME: verify that we are ok with this major/minor? */
  2967. caps = LVAL(req->rq_data, 4);
  2968. DEBUG1("Server capabilities 0x%016llx\n", caps);
  2969. out_free:
  2970. smb_rput(req);
  2971. out:
  2972. return result;
  2973. }
  2974. static void
  2975. install_ops(struct smb_ops *dst, struct smb_ops *src)
  2976. {
  2977. memcpy(dst, src, sizeof(void *) * SMB_OPS_NUM_STATIC);
  2978. }
  2979. /* < LANMAN2 */
  2980. static struct smb_ops smb_ops_core =
  2981. {
  2982. .read = smb_proc_read,
  2983. .write = smb_proc_write,
  2984. .readdir = smb_proc_readdir_short,
  2985. .getattr = smb_proc_getattr_core,
  2986. .truncate = smb_proc_trunc32,
  2987. };
  2988. /* LANMAN2, OS/2, others? */
  2989. static struct smb_ops smb_ops_os2 =
  2990. {
  2991. .read = smb_proc_read,
  2992. .write = smb_proc_write,
  2993. .readdir = smb_proc_readdir_long,
  2994. .getattr = smb_proc_getattr_trans2_std,
  2995. .truncate = smb_proc_trunc32,
  2996. };
  2997. /* Win95, and possibly some NetApp versions too */
  2998. static struct smb_ops smb_ops_win95 =
  2999. {
  3000. .read = smb_proc_read, /* does not support 12word readX */
  3001. .write = smb_proc_write,
  3002. .readdir = smb_proc_readdir_long,
  3003. .getattr = smb_proc_getattr_95,
  3004. .truncate = smb_proc_trunc95,
  3005. };
  3006. /* Samba, NT4 and NT5 */
  3007. static struct smb_ops smb_ops_winNT =
  3008. {
  3009. .read = smb_proc_readX,
  3010. .write = smb_proc_writeX,
  3011. .readdir = smb_proc_readdir_long,
  3012. .getattr = smb_proc_getattr_trans2_all,
  3013. .truncate = smb_proc_trunc64,
  3014. };
  3015. /* Samba w/ unix extensions. Others? */
  3016. static struct smb_ops smb_ops_unix =
  3017. {
  3018. .read = smb_proc_readX,
  3019. .write = smb_proc_writeX,
  3020. .readdir = smb_proc_readdir_long,
  3021. .getattr = smb_proc_getattr_unix,
  3022. /* FIXME: core/ext/time setattr needs to be cleaned up! */
  3023. /* .setattr = smb_proc_setattr_unix, */
  3024. .truncate = smb_proc_trunc64,
  3025. };
  3026. /* Place holder until real ops are in place */
  3027. static struct smb_ops smb_ops_null =
  3028. {
  3029. .readdir = smb_proc_readdir_null,
  3030. .getattr = smb_proc_getattr_null,
  3031. };
  3032. void smb_install_null_ops(struct smb_ops *ops)
  3033. {
  3034. install_ops(ops, &smb_ops_null);
  3035. }