proc.c 84 KB

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