proc.c 84 KB

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