proc.c 84 KB

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