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/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/fs.h>
  13. #include <linux/file.h>
  14. #include <linux/stat.h>
  15. #include <linux/fcntl.h>
  16. #include <linux/dcache.h>
  17. #include <linux/dirent.h>
  18. #include <linux/nls.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/net.h>
  21. #include <linux/vfs.h>
  22. #include <linux/smb_fs.h>
  23. #include <linux/smbno.h>
  24. #include <linux/smb_mount.h>
  25. #include <net/sock.h>
  26. #include <asm/string.h>
  27. #include <asm/div64.h>
  28. #include "smb_debug.h"
  29. #include "proto.h"
  30. #include "request.h"
  31. /* Features. Undefine if they cause problems, this should perhaps be a
  32. config option. */
  33. #define SMBFS_POSIX_UNLINK 1
  34. /* Allow smb_retry to be interrupted. */
  35. #define SMB_RETRY_INTR
  36. #define SMB_VWV(packet) ((packet) + SMB_HEADER_LEN)
  37. #define SMB_CMD(packet) (*(packet+8))
  38. #define SMB_WCT(packet) (*(packet+SMB_HEADER_LEN - 1))
  39. #define SMB_DIRINFO_SIZE 43
  40. #define SMB_STATUS_SIZE 21
  41. #define SMB_ST_BLKSIZE (PAGE_SIZE)
  42. #define SMB_ST_BLKSHIFT (PAGE_SHIFT)
  43. static struct smb_ops smb_ops_core;
  44. static struct smb_ops smb_ops_os2;
  45. static struct smb_ops smb_ops_win95;
  46. static struct smb_ops smb_ops_winNT;
  47. static struct smb_ops smb_ops_unix;
  48. static struct smb_ops smb_ops_null;
  49. static void
  50. smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
  51. static void
  52. smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
  53. static int
  54. smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
  55. struct smb_fattr *fattr);
  56. static int
  57. smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
  58. struct smb_fattr *fattr);
  59. static int
  60. smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
  61. u16 attr);
  62. static int
  63. smb_proc_setattr_ext(struct smb_sb_info *server,
  64. struct inode *inode, struct smb_fattr *fattr);
  65. static int
  66. smb_proc_query_cifsunix(struct smb_sb_info *server);
  67. static void
  68. install_ops(struct smb_ops *dst, struct smb_ops *src);
  69. static void
  70. str_upper(char *name, int len)
  71. {
  72. while (len--)
  73. {
  74. if (*name >= 'a' && *name <= 'z')
  75. *name -= ('a' - 'A');
  76. name++;
  77. }
  78. }
  79. #if 0
  80. static void
  81. str_lower(char *name, int len)
  82. {
  83. while (len--)
  84. {
  85. if (*name >= 'A' && *name <= 'Z')
  86. *name += ('a' - 'A');
  87. name++;
  88. }
  89. }
  90. #endif
  91. /* reverse a string inline. This is used by the dircache walking routines */
  92. static void reverse_string(char *buf, int len)
  93. {
  94. char c;
  95. char *end = buf+len-1;
  96. while(buf < end) {
  97. c = *buf;
  98. *(buf++) = *end;
  99. *(end--) = c;
  100. }
  101. }
  102. /* no conversion, just a wrapper for memcpy. */
  103. static int convert_memcpy(unsigned char *output, int olen,
  104. const unsigned char *input, int ilen,
  105. struct nls_table *nls_from,
  106. struct nls_table *nls_to)
  107. {
  108. if (olen < ilen)
  109. return -ENAMETOOLONG;
  110. memcpy(output, input, ilen);
  111. return ilen;
  112. }
  113. static inline int write_char(unsigned char ch, char *output, int olen)
  114. {
  115. if (olen < 4)
  116. return -ENAMETOOLONG;
  117. sprintf(output, ":x%02x", ch);
  118. return 4;
  119. }
  120. static inline int write_unichar(wchar_t ch, char *output, int olen)
  121. {
  122. if (olen < 5)
  123. return -ENAMETOOLONG;
  124. sprintf(output, ":%04x", ch);
  125. return 5;
  126. }
  127. /* convert from one "codepage" to another (possibly being utf8). */
  128. static int convert_cp(unsigned char *output, int olen,
  129. const unsigned char *input, int ilen,
  130. struct nls_table *nls_from,
  131. struct nls_table *nls_to)
  132. {
  133. int len = 0;
  134. int n;
  135. wchar_t ch;
  136. while (ilen > 0) {
  137. /* convert by changing to unicode and back to the new cp */
  138. n = nls_from->char2uni(input, ilen, &ch);
  139. if (n == -EINVAL) {
  140. ilen--;
  141. n = write_char(*input++, output, olen);
  142. if (n < 0)
  143. goto fail;
  144. output += n;
  145. olen -= n;
  146. len += n;
  147. continue;
  148. } else if (n < 0)
  149. goto fail;
  150. input += n;
  151. ilen -= n;
  152. n = nls_to->uni2char(ch, output, olen);
  153. if (n == -EINVAL)
  154. n = write_unichar(ch, output, olen);
  155. if (n < 0)
  156. goto fail;
  157. output += n;
  158. olen -= n;
  159. len += n;
  160. }
  161. return len;
  162. fail:
  163. return n;
  164. }
  165. /* ----------------------------------------------------------- */
  166. /*
  167. * nls_unicode
  168. *
  169. * This encodes/decodes little endian unicode format
  170. */
  171. static int uni2char(wchar_t uni, unsigned char *out, int boundlen)
  172. {
  173. if (boundlen < 2)
  174. return -EINVAL;
  175. *out++ = uni & 0xff;
  176. *out++ = uni >> 8;
  177. return 2;
  178. }
  179. static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
  180. {
  181. if (boundlen < 2)
  182. return -EINVAL;
  183. *uni = (rawstring[1] << 8) | rawstring[0];
  184. return 2;
  185. }
  186. static struct nls_table unicode_table = {
  187. .charset = "unicode",
  188. .uni2char = uni2char,
  189. .char2uni = char2uni,
  190. };
  191. /* ----------------------------------------------------------- */
  192. static int setcodepage(struct nls_table **p, char *name)
  193. {
  194. struct nls_table *nls;
  195. if (!name || !*name) {
  196. nls = NULL;
  197. } else if ( (nls = load_nls(name)) == NULL) {
  198. printk (KERN_ERR "smbfs: failed to load nls '%s'\n", name);
  199. return -EINVAL;
  200. }
  201. /* if already set, unload the previous one. */
  202. if (*p && *p != &unicode_table)
  203. unload_nls(*p);
  204. *p = nls;
  205. return 0;
  206. }
  207. /* Handles all changes to codepage settings. */
  208. int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp)
  209. {
  210. int n = 0;
  211. smb_lock_server(server);
  212. /* Don't load any nls_* at all, if no remote is requested */
  213. if (!*cp->remote_name)
  214. goto out;
  215. /* local */
  216. n = setcodepage(&server->local_nls, cp->local_name);
  217. if (n != 0)
  218. goto out;
  219. /* remote */
  220. if (!strcmp(cp->remote_name, "unicode")) {
  221. server->remote_nls = &unicode_table;
  222. } else {
  223. n = setcodepage(&server->remote_nls, cp->remote_name);
  224. if (n != 0)
  225. setcodepage(&server->local_nls, NULL);
  226. }
  227. out:
  228. if (server->local_nls != NULL && server->remote_nls != NULL)
  229. server->ops->convert = convert_cp;
  230. else
  231. server->ops->convert = convert_memcpy;
  232. smb_unlock_server(server);
  233. return n;
  234. }
  235. /*****************************************************************************/
  236. /* */
  237. /* Encoding/Decoding section */
  238. /* */
  239. /*****************************************************************************/
  240. static __u8 *
  241. smb_encode_smb_length(__u8 * p, __u32 len)
  242. {
  243. *p = 0;
  244. *(p+1) = 0;
  245. *(p+2) = (len & 0xFF00) >> 8;
  246. *(p+3) = (len & 0xFF);
  247. if (len > 0xFFFF)
  248. {
  249. *(p+1) = 1;
  250. }
  251. return p + 4;
  252. }
  253. /*
  254. * smb_build_path: build the path to entry and name storing it in buf.
  255. * The path returned will have the trailing '\0'.
  256. */
  257. static int smb_build_path(struct smb_sb_info *server, unsigned char *buf,
  258. int maxlen,
  259. struct dentry *entry, struct qstr *name)
  260. {
  261. unsigned char *path = buf;
  262. int len;
  263. int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE) != 0;
  264. if (maxlen < (2<<unicode))
  265. return -ENAMETOOLONG;
  266. if (maxlen > SMB_MAXPATHLEN + 1)
  267. maxlen = SMB_MAXPATHLEN + 1;
  268. if (entry == NULL)
  269. goto test_name_and_out;
  270. /*
  271. * If IS_ROOT, we have to do no walking at all.
  272. */
  273. if (IS_ROOT(entry) && !name) {
  274. *path++ = '\\';
  275. if (unicode) *path++ = '\0';
  276. *path++ = '\0';
  277. if (unicode) *path++ = '\0';
  278. return path-buf;
  279. }
  280. /*
  281. * Build the path string walking the tree backward from end to ROOT
  282. * and store it in reversed order [see reverse_string()]
  283. */
  284. dget(entry);
  285. spin_lock(&entry->d_lock);
  286. while (!IS_ROOT(entry)) {
  287. struct dentry *parent;
  288. if (maxlen < (3<<unicode)) {
  289. spin_unlock(&entry->d_lock);
  290. dput(entry);
  291. return -ENAMETOOLONG;
  292. }
  293. len = server->ops->convert(path, maxlen-2,
  294. entry->d_name.name, entry->d_name.len,
  295. server->local_nls, server->remote_nls);
  296. if (len < 0) {
  297. spin_unlock(&entry->d_lock);
  298. dput(entry);
  299. return len;
  300. }
  301. reverse_string(path, len);
  302. path += len;
  303. if (unicode) {
  304. /* Note: reverse order */
  305. *path++ = '\0';
  306. maxlen--;
  307. }
  308. *path++ = '\\';
  309. maxlen -= len+1;
  310. parent = entry->d_parent;
  311. dget(parent);
  312. spin_unlock(&entry->d_lock);
  313. dput(entry);
  314. entry = parent;
  315. spin_lock(&entry->d_lock);
  316. }
  317. spin_unlock(&entry->d_lock);
  318. dput(entry);
  319. reverse_string(buf, path-buf);
  320. /* maxlen has space for at least one char */
  321. test_name_and_out:
  322. if (name) {
  323. if (maxlen < (3<<unicode))
  324. return -ENAMETOOLONG;
  325. *path++ = '\\';
  326. if (unicode) {
  327. *path++ = '\0';
  328. maxlen--;
  329. }
  330. len = server->ops->convert(path, maxlen-2,
  331. name->name, name->len,
  332. server->local_nls, server->remote_nls);
  333. if (len < 0)
  334. return len;
  335. path += len;
  336. maxlen -= len+1;
  337. }
  338. /* maxlen has space for at least one char */
  339. *path++ = '\0';
  340. if (unicode) *path++ = '\0';
  341. return path-buf;
  342. }
  343. static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen,
  344. struct dentry *dir, struct qstr *name)
  345. {
  346. int result;
  347. result = smb_build_path(server, buf, maxlen, dir, name);
  348. if (result < 0)
  349. goto out;
  350. if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
  351. str_upper(buf, result);
  352. out:
  353. return result;
  354. }
  355. /* encode_path for non-trans2 request SMBs */
  356. static int smb_simple_encode_path(struct smb_request *req, char **p,
  357. struct dentry * entry, struct qstr * name)
  358. {
  359. struct smb_sb_info *server = req->rq_server;
  360. char *s = *p;
  361. int res;
  362. int maxlen = ((char *)req->rq_buffer + req->rq_bufsize) - s;
  363. int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
  364. if (!maxlen)
  365. return -ENAMETOOLONG;
  366. *s++ = 4; /* ASCII data format */
  367. /*
  368. * SMB Unicode strings must be 16bit aligned relative the start of the
  369. * packet. If they are not they must be padded with 0.
  370. */
  371. if (unicode) {
  372. int align = s - (char *)req->rq_buffer;
  373. if (!(align & 1)) {
  374. *s++ = '\0';
  375. maxlen--;
  376. }
  377. }
  378. res = smb_encode_path(server, s, maxlen-1, entry, name);
  379. if (res < 0)
  380. return res;
  381. *p = s + res;
  382. return 0;
  383. }
  384. /* The following are taken directly from msdos-fs */
  385. /* Linear day numbers of the respective 1sts in non-leap years. */
  386. static int day_n[] =
  387. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
  388. /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
  389. static time_t
  390. utc2local(struct smb_sb_info *server, time_t time)
  391. {
  392. return time - server->opt.serverzone*60;
  393. }
  394. static time_t
  395. local2utc(struct smb_sb_info *server, time_t time)
  396. {
  397. return time + server->opt.serverzone*60;
  398. }
  399. /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
  400. static time_t
  401. date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time)
  402. {
  403. int month, year;
  404. time_t secs;
  405. /* first subtract and mask after that... Otherwise, if
  406. date == 0, bad things happen */
  407. month = ((date >> 5) - 1) & 15;
  408. year = date >> 9;
  409. secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 *
  410. ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 &&
  411. month < 2 ? 1 : 0) + 3653);
  412. /* days since 1.1.70 plus 80's leap day */
  413. return local2utc(server, secs);
  414. }
  415. /* Convert linear UNIX date to a MS-DOS time/date pair. */
  416. static void
  417. date_unix2dos(struct smb_sb_info *server,
  418. int unix_date, __u16 *date, __u16 *time)
  419. {
  420. int day, year, nl_day, month;
  421. unix_date = utc2local(server, unix_date);
  422. if (unix_date < 315532800)
  423. unix_date = 315532800;
  424. *time = (unix_date % 60) / 2 +
  425. (((unix_date / 60) % 60) << 5) +
  426. (((unix_date / 3600) % 24) << 11);
  427. day = unix_date / 86400 - 3652;
  428. year = day / 365;
  429. if ((year + 3) / 4 + 365 * year > day)
  430. year--;
  431. day -= (year + 3) / 4 + 365 * year;
  432. if (day == 59 && !(year & 3)) {
  433. nl_day = day;
  434. month = 2;
  435. } else {
  436. nl_day = (year & 3) || day <= 59 ? day : day - 1;
  437. for (month = 0; month < 12; month++)
  438. if (day_n[month] > nl_day)
  439. break;
  440. }
  441. *date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
  442. }
  443. /* The following are taken from fs/ntfs/util.c */
  444. #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
  445. /*
  446. * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
  447. * into Unix UTC (based 1970-01-01, in seconds).
  448. */
  449. static struct timespec
  450. smb_ntutc2unixutc(u64 ntutc)
  451. {
  452. struct timespec ts;
  453. /* FIXME: what about the timezone difference? */
  454. /* Subtract the NTFS time offset, then convert to 1s intervals. */
  455. u64 t = ntutc - NTFS_TIME_OFFSET;
  456. ts.tv_nsec = do_div(t, 10000000) * 100;
  457. ts.tv_sec = t;
  458. return ts;
  459. }
  460. /* Convert the Unix UTC into NT time */
  461. static u64
  462. smb_unixutc2ntutc(struct timespec ts)
  463. {
  464. /* Note: timezone conversion is probably wrong. */
  465. /* return ((u64)utc2local(server, t)) * 10000000 + NTFS_TIME_OFFSET; */
  466. return ((u64)ts.tv_sec) * 10000000 + ts.tv_nsec/100 + NTFS_TIME_OFFSET;
  467. }
  468. #define MAX_FILE_MODE 6
  469. static mode_t file_mode[] = {
  470. S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK
  471. };
  472. static int smb_filetype_to_mode(u32 filetype)
  473. {
  474. if (filetype > MAX_FILE_MODE) {
  475. PARANOIA("Filetype out of range: %d\n", filetype);
  476. return S_IFREG;
  477. }
  478. return file_mode[filetype];
  479. }
  480. static u32 smb_filetype_from_mode(int mode)
  481. {
  482. if (S_ISREG(mode))
  483. return UNIX_TYPE_FILE;
  484. if (S_ISDIR(mode))
  485. return UNIX_TYPE_DIR;
  486. if (S_ISLNK(mode))
  487. return UNIX_TYPE_SYMLINK;
  488. if (S_ISCHR(mode))
  489. return UNIX_TYPE_CHARDEV;
  490. if (S_ISBLK(mode))
  491. return UNIX_TYPE_BLKDEV;
  492. if (S_ISFIFO(mode))
  493. return UNIX_TYPE_FIFO;
  494. if (S_ISSOCK(mode))
  495. return UNIX_TYPE_SOCKET;
  496. return UNIX_TYPE_UNKNOWN;
  497. }
  498. /*****************************************************************************/
  499. /* */
  500. /* Support section. */
  501. /* */
  502. /*****************************************************************************/
  503. __u32
  504. smb_len(__u8 * p)
  505. {
  506. return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3);
  507. }
  508. static __u16
  509. smb_bcc(__u8 * packet)
  510. {
  511. int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16);
  512. return WVAL(packet, pos);
  513. }
  514. /* smb_valid_packet: We check if packet fulfills the basic
  515. requirements of a smb packet */
  516. static int
  517. smb_valid_packet(__u8 * packet)
  518. {
  519. return (packet[4] == 0xff
  520. && packet[5] == 'S'
  521. && packet[6] == 'M'
  522. && packet[7] == 'B'
  523. && (smb_len(packet) + 4 == SMB_HEADER_LEN
  524. + SMB_WCT(packet) * 2 + smb_bcc(packet)));
  525. }
  526. /* smb_verify: We check if we got the answer we expected, and if we
  527. got enough data. If bcc == -1, we don't care. */
  528. static int
  529. smb_verify(__u8 * packet, int command, int wct, int bcc)
  530. {
  531. if (SMB_CMD(packet) != command)
  532. goto bad_command;
  533. if (SMB_WCT(packet) < wct)
  534. goto bad_wct;
  535. if (bcc != -1 && smb_bcc(packet) < bcc)
  536. goto bad_bcc;
  537. return 0;
  538. bad_command:
  539. printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n",
  540. command, SMB_CMD(packet));
  541. goto fail;
  542. bad_wct:
  543. printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
  544. command, wct, SMB_WCT(packet));
  545. goto fail;
  546. bad_bcc:
  547. printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
  548. command, bcc, smb_bcc(packet));
  549. fail:
  550. return -EIO;
  551. }
  552. /*
  553. * Returns the maximum read or write size for the "payload". Making all of the
  554. * packet fit within the negotiated max_xmit size.
  555. *
  556. * N.B. Since this value is usually computed before locking the server,
  557. * the server's packet size must never be decreased!
  558. */
  559. static inline int
  560. smb_get_xmitsize(struct smb_sb_info *server, int overhead)
  561. {
  562. return server->opt.max_xmit - overhead;
  563. }
  564. /*
  565. * Calculate the maximum read size
  566. */
  567. int
  568. smb_get_rsize(struct smb_sb_info *server)
  569. {
  570. /* readX has 12 parameters, read has 5 */
  571. int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2;
  572. int size = smb_get_xmitsize(server, overhead);
  573. VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
  574. return size;
  575. }
  576. /*
  577. * Calculate the maximum write size
  578. */
  579. int
  580. smb_get_wsize(struct smb_sb_info *server)
  581. {
  582. /* writeX has 14 parameters, write has 5 */
  583. int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2;
  584. int size = smb_get_xmitsize(server, overhead);
  585. VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
  586. return size;
  587. }
  588. /*
  589. * Convert SMB error codes to -E... errno values.
  590. */
  591. int
  592. smb_errno(struct smb_request *req)
  593. {
  594. int errcls = req->rq_rcls;
  595. int error = req->rq_err;
  596. char *class = "Unknown";
  597. VERBOSE("errcls %d code %d from command 0x%x\n",
  598. errcls, error, SMB_CMD(req->rq_header));
  599. if (errcls == ERRDOS) {
  600. switch (error) {
  601. case ERRbadfunc:
  602. return -EINVAL;
  603. case ERRbadfile:
  604. case ERRbadpath:
  605. return -ENOENT;
  606. case ERRnofids:
  607. return -EMFILE;
  608. case ERRnoaccess:
  609. return -EACCES;
  610. case ERRbadfid:
  611. return -EBADF;
  612. case ERRbadmcb:
  613. return -EREMOTEIO;
  614. case ERRnomem:
  615. return -ENOMEM;
  616. case ERRbadmem:
  617. return -EFAULT;
  618. case ERRbadenv:
  619. case ERRbadformat:
  620. return -EREMOTEIO;
  621. case ERRbadaccess:
  622. return -EACCES;
  623. case ERRbaddata:
  624. return -E2BIG;
  625. case ERRbaddrive:
  626. return -ENXIO;
  627. case ERRremcd:
  628. return -EREMOTEIO;
  629. case ERRdiffdevice:
  630. return -EXDEV;
  631. case ERRnofiles:
  632. return -ENOENT;
  633. case ERRbadshare:
  634. return -ETXTBSY;
  635. case ERRlock:
  636. return -EDEADLK;
  637. case ERRfilexists:
  638. return -EEXIST;
  639. case ERROR_INVALID_PARAMETER:
  640. return -EINVAL;
  641. case ERROR_DISK_FULL:
  642. return -ENOSPC;
  643. case ERROR_INVALID_NAME:
  644. return -ENOENT;
  645. case ERROR_DIR_NOT_EMPTY:
  646. return -ENOTEMPTY;
  647. case ERROR_NOT_LOCKED:
  648. return -ENOLCK;
  649. case ERROR_ALREADY_EXISTS:
  650. return -EEXIST;
  651. default:
  652. class = "ERRDOS";
  653. goto err_unknown;
  654. }
  655. } else if (errcls == ERRSRV) {
  656. switch (error) {
  657. /* N.B. This is wrong ... EIO ? */
  658. case ERRerror:
  659. return -ENFILE;
  660. case ERRbadpw:
  661. return -EINVAL;
  662. case ERRbadtype:
  663. case ERRtimeout:
  664. return -EIO;
  665. case ERRaccess:
  666. return -EACCES;
  667. /*
  668. * This is a fatal error, as it means the "tree ID"
  669. * for this connection is no longer valid. We map
  670. * to a special error code and get a new connection.
  671. */
  672. case ERRinvnid:
  673. return -EBADSLT;
  674. default:
  675. class = "ERRSRV";
  676. goto err_unknown;
  677. }
  678. } else if (errcls == ERRHRD) {
  679. switch (error) {
  680. case ERRnowrite:
  681. return -EROFS;
  682. case ERRbadunit:
  683. return -ENODEV;
  684. case ERRnotready:
  685. return -EUCLEAN;
  686. case ERRbadcmd:
  687. case ERRdata:
  688. return -EIO;
  689. case ERRbadreq:
  690. return -ERANGE;
  691. case ERRbadshare:
  692. return -ETXTBSY;
  693. case ERRlock:
  694. return -EDEADLK;
  695. case ERRdiskfull:
  696. return -ENOSPC;
  697. default:
  698. class = "ERRHRD";
  699. goto err_unknown;
  700. }
  701. } else if (errcls == ERRCMD) {
  702. class = "ERRCMD";
  703. } else if (errcls == SUCCESS) {
  704. return 0; /* This is the only valid 0 return */
  705. }
  706. err_unknown:
  707. printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n",
  708. class, error, SMB_CMD(req->rq_header));
  709. return -EIO;
  710. }
  711. /* smb_request_ok: We expect the server to be locked. Then we do the
  712. request and check the answer completely. When smb_request_ok
  713. returns 0, you can be quite sure that everything went well. When
  714. the answer is <=0, the returned number is a valid unix errno. */
  715. static int
  716. smb_request_ok(struct smb_request *req, int command, int wct, int bcc)
  717. {
  718. int result;
  719. req->rq_resp_wct = wct;
  720. req->rq_resp_bcc = bcc;
  721. result = smb_add_request(req);
  722. if (result != 0) {
  723. DEBUG1("smb_request failed\n");
  724. goto out;
  725. }
  726. if (smb_valid_packet(req->rq_header) != 0) {
  727. PARANOIA("invalid packet!\n");
  728. goto out;
  729. }
  730. result = smb_verify(req->rq_header, command, wct, bcc);
  731. out:
  732. return result;
  733. }
  734. /*
  735. * This implements the NEWCONN ioctl. It installs the server pid,
  736. * sets server->state to CONN_VALID, and wakes up the waiting process.
  737. */
  738. int
  739. smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt)
  740. {
  741. struct file *filp;
  742. struct sock *sk;
  743. int error;
  744. VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid);
  745. smb_lock_server(server);
  746. /*
  747. * Make sure we don't already have a valid connection ...
  748. */
  749. error = -EINVAL;
  750. if (server->state == CONN_VALID)
  751. goto out;
  752. error = -EACCES;
  753. if (current->uid != server->mnt->mounted_uid &&
  754. !capable(CAP_SYS_ADMIN))
  755. goto out;
  756. error = -EBADF;
  757. filp = fget(opt->fd);
  758. if (!filp)
  759. goto out;
  760. if (!smb_valid_socket(filp->f_dentry->d_inode))
  761. goto out_putf;
  762. server->sock_file = filp;
  763. server->conn_pid = current->pid;
  764. server->opt = *opt;
  765. server->generation += 1;
  766. server->state = CONN_VALID;
  767. error = 0;
  768. if (server->conn_error) {
  769. /*
  770. * conn_error is the returncode we originally decided to
  771. * drop the old connection on. This message should be positive
  772. * and not make people ask questions on why smbfs is printing
  773. * error messages ...
  774. */
  775. printk(KERN_INFO "SMB connection re-established (%d)\n",
  776. server->conn_error);
  777. server->conn_error = 0;
  778. }
  779. /*
  780. * Store the server in sock user_data (Only used by sunrpc)
  781. */
  782. sk = SOCKET_I(filp->f_dentry->d_inode)->sk;
  783. sk->sk_user_data = server;
  784. /* chain into the data_ready callback */
  785. server->data_ready = xchg(&sk->sk_data_ready, smb_data_ready);
  786. /* check if we have an old smbmount that uses seconds for the
  787. serverzone */
  788. if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60)
  789. server->opt.serverzone /= 60;
  790. /* now that we have an established connection we can detect the server
  791. type and enable bug workarounds */
  792. if (server->opt.protocol < SMB_PROTOCOL_LANMAN2)
  793. install_ops(server->ops, &smb_ops_core);
  794. else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2)
  795. install_ops(server->ops, &smb_ops_os2);
  796. else if (server->opt.protocol == SMB_PROTOCOL_NT1 &&
  797. (server->opt.max_xmit < 0x1000) &&
  798. !(server->opt.capabilities & SMB_CAP_NT_SMBS)) {
  799. /* FIXME: can we kill the WIN95 flag now? */
  800. server->mnt->flags |= SMB_MOUNT_WIN95;
  801. VERBOSE("detected WIN95 server\n");
  802. install_ops(server->ops, &smb_ops_win95);
  803. } else {
  804. /*
  805. * Samba has max_xmit 65535
  806. * NT4spX has max_xmit 4536 (or something like that)
  807. * win2k has ...
  808. */
  809. VERBOSE("detected NT1 (Samba, NT4/5) server\n");
  810. install_ops(server->ops, &smb_ops_winNT);
  811. }
  812. /* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */
  813. if (server->mnt->flags & SMB_MOUNT_OLDATTR) {
  814. server->ops->getattr = smb_proc_getattr_core;
  815. } else if (server->mnt->flags & SMB_MOUNT_DIRATTR) {
  816. server->ops->getattr = smb_proc_getattr_ff;
  817. }
  818. /* Decode server capabilities */
  819. if (server->opt.capabilities & SMB_CAP_LARGE_FILES) {
  820. /* Should be ok to set this now, as no one can access the
  821. mount until the connection has been established. */
  822. SB_of(server)->s_maxbytes = ~0ULL >> 1;
  823. VERBOSE("LFS enabled\n");
  824. }
  825. if (server->opt.capabilities & SMB_CAP_UNICODE) {
  826. server->mnt->flags |= SMB_MOUNT_UNICODE;
  827. VERBOSE("Unicode enabled\n");
  828. } else {
  829. server->mnt->flags &= ~SMB_MOUNT_UNICODE;
  830. }
  831. #if 0
  832. /* flags we may test for other patches ... */
  833. if (server->opt.capabilities & SMB_CAP_LARGE_READX) {
  834. VERBOSE("Large reads enabled\n");
  835. }
  836. if (server->opt.capabilities & SMB_CAP_LARGE_WRITEX) {
  837. VERBOSE("Large writes enabled\n");
  838. }
  839. #endif
  840. if (server->opt.capabilities & SMB_CAP_UNIX) {
  841. struct inode *inode;
  842. VERBOSE("Using UNIX CIFS extensions\n");
  843. install_ops(server->ops, &smb_ops_unix);
  844. inode = SB_of(server)->s_root->d_inode;
  845. if (inode)
  846. inode->i_op = &smb_dir_inode_operations_unix;
  847. }
  848. VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n",
  849. server->opt.protocol, server->opt.max_xmit, server->conn_pid,
  850. server->opt.capabilities);
  851. /* FIXME: this really should be done by smbmount. */
  852. if (server->opt.max_xmit > SMB_MAX_PACKET_SIZE) {
  853. server->opt.max_xmit = SMB_MAX_PACKET_SIZE;
  854. }
  855. smb_unlock_server(server);
  856. smbiod_wake_up();
  857. if (server->opt.capabilities & SMB_CAP_UNIX)
  858. smb_proc_query_cifsunix(server);
  859. server->conn_complete++;
  860. wake_up_interruptible_all(&server->conn_wq);
  861. return error;
  862. out:
  863. smb_unlock_server(server);
  864. smbiod_wake_up();
  865. return error;
  866. out_putf:
  867. fput(filp);
  868. goto out;
  869. }
  870. /* smb_setup_header: We completely set up the packet. You only have to
  871. insert the command-specific fields */
  872. __u8 *
  873. smb_setup_header(struct smb_request *req, __u8 command, __u16 wct, __u16 bcc)
  874. {
  875. __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2;
  876. __u8 *p = req->rq_header;
  877. struct smb_sb_info *server = req->rq_server;
  878. p = smb_encode_smb_length(p, xmit_len - 4);
  879. *p++ = 0xff;
  880. *p++ = 'S';
  881. *p++ = 'M';
  882. *p++ = 'B';
  883. *p++ = command;
  884. memset(p, '\0', 19);
  885. p += 19;
  886. p += 8;
  887. if (server->opt.protocol > SMB_PROTOCOL_CORE) {
  888. int flags = SMB_FLAGS_CASELESS_PATHNAMES;
  889. int flags2 = SMB_FLAGS2_LONG_PATH_COMPONENTS |
  890. SMB_FLAGS2_EXTENDED_ATTRIBUTES; /* EA? not really ... */
  891. *(req->rq_header + smb_flg) = flags;
  892. if (server->mnt->flags & SMB_MOUNT_UNICODE)
  893. flags2 |= SMB_FLAGS2_UNICODE_STRINGS;
  894. WSET(req->rq_header, smb_flg2, flags2);
  895. }
  896. *p++ = wct; /* wct */
  897. p += 2 * wct;
  898. WSET(p, 0, bcc);
  899. /* Include the header in the data to send */
  900. req->rq_iovlen = 1;
  901. req->rq_iov[0].iov_base = req->rq_header;
  902. req->rq_iov[0].iov_len = xmit_len - bcc;
  903. return req->rq_buffer;
  904. }
  905. static void
  906. smb_setup_bcc(struct smb_request *req, __u8 *p)
  907. {
  908. u16 bcc = p - req->rq_buffer;
  909. u8 *pbcc = req->rq_header + SMB_HEADER_LEN + 2*SMB_WCT(req->rq_header);
  910. WSET(pbcc, 0, bcc);
  911. smb_encode_smb_length(req->rq_header, SMB_HEADER_LEN +
  912. 2*SMB_WCT(req->rq_header) - 2 + bcc);
  913. /* Include the "bytes" in the data to send */
  914. req->rq_iovlen = 2;
  915. req->rq_iov[1].iov_base = req->rq_buffer;
  916. req->rq_iov[1].iov_len = bcc;
  917. }
  918. static int
  919. smb_proc_seek(struct smb_sb_info *server, __u16 fileid,
  920. __u16 mode, off_t offset)
  921. {
  922. int result;
  923. struct smb_request *req;
  924. result = -ENOMEM;
  925. if (! (req = smb_alloc_request(server, 0)))
  926. goto out;
  927. smb_setup_header(req, SMBlseek, 4, 0);
  928. WSET(req->rq_header, smb_vwv0, fileid);
  929. WSET(req->rq_header, smb_vwv1, mode);
  930. DSET(req->rq_header, smb_vwv2, offset);
  931. req->rq_flags |= SMB_REQ_NORETRY;
  932. result = smb_request_ok(req, SMBlseek, 2, 0);
  933. if (result < 0) {
  934. result = 0;
  935. goto out_free;
  936. }
  937. result = DVAL(req->rq_header, smb_vwv0);
  938. out_free:
  939. smb_rput(req);
  940. out:
  941. return result;
  942. }
  943. static int
  944. smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish)
  945. {
  946. struct inode *ino = dentry->d_inode;
  947. struct smb_inode_info *ei = SMB_I(ino);
  948. int mode, read_write = 0x42, read_only = 0x40;
  949. int res;
  950. char *p;
  951. struct smb_request *req;
  952. /*
  953. * Attempt to open r/w, unless there are no write privileges.
  954. */
  955. mode = read_write;
  956. if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
  957. mode = read_only;
  958. #if 0
  959. /* FIXME: why is this code not in? below we fix it so that a caller
  960. wanting RO doesn't get RW. smb_revalidate_inode does some
  961. optimization based on access mode. tail -f needs it to be correct.
  962. We must open rw since we don't do the open if called a second time
  963. with different 'wish'. Is that not supported by smb servers? */
  964. if (!(wish & (O_WRONLY | O_RDWR)))
  965. mode = read_only;
  966. #endif
  967. res = -ENOMEM;
  968. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  969. goto out;
  970. retry:
  971. p = smb_setup_header(req, SMBopen, 2, 0);
  972. WSET(req->rq_header, smb_vwv0, mode);
  973. WSET(req->rq_header, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
  974. res = smb_simple_encode_path(req, &p, dentry, NULL);
  975. if (res < 0)
  976. goto out_free;
  977. smb_setup_bcc(req, p);
  978. res = smb_request_ok(req, SMBopen, 7, 0);
  979. if (res != 0) {
  980. if (mode == read_write &&
  981. (res == -EACCES || res == -ETXTBSY || res == -EROFS))
  982. {
  983. VERBOSE("%s/%s R/W failed, error=%d, retrying R/O\n",
  984. DENTRY_PATH(dentry), res);
  985. mode = read_only;
  986. req->rq_flags = 0;
  987. goto retry;
  988. }
  989. goto out_free;
  990. }
  991. /* We should now have data in vwv[0..6]. */
  992. ei->fileid = WVAL(req->rq_header, smb_vwv0);
  993. ei->attr = WVAL(req->rq_header, smb_vwv1);
  994. /* smb_vwv2 has mtime */
  995. /* smb_vwv4 has size */
  996. ei->access = (WVAL(req->rq_header, smb_vwv6) & SMB_ACCMASK);
  997. ei->open = server->generation;
  998. out_free:
  999. smb_rput(req);
  1000. out:
  1001. return res;
  1002. }
  1003. /*
  1004. * Make sure the file is open, and check that the access
  1005. * is compatible with the desired access.
  1006. */
  1007. int
  1008. smb_open(struct dentry *dentry, int wish)
  1009. {
  1010. struct inode *inode = dentry->d_inode;
  1011. int result;
  1012. __u16 access;
  1013. result = -ENOENT;
  1014. if (!inode) {
  1015. printk(KERN_ERR "smb_open: no inode for dentry %s/%s\n",
  1016. DENTRY_PATH(dentry));
  1017. goto out;
  1018. }
  1019. if (!smb_is_open(inode)) {
  1020. struct smb_sb_info *server = server_from_inode(inode);
  1021. result = 0;
  1022. if (!smb_is_open(inode))
  1023. result = smb_proc_open(server, dentry, wish);
  1024. if (result)
  1025. goto out;
  1026. /*
  1027. * A successful open means the path is still valid ...
  1028. */
  1029. smb_renew_times(dentry);
  1030. }
  1031. /*
  1032. * Check whether the access is compatible with the desired mode.
  1033. */
  1034. result = 0;
  1035. access = SMB_I(inode)->access;
  1036. if (access != wish && access != SMB_O_RDWR) {
  1037. PARANOIA("%s/%s access denied, access=%x, wish=%x\n",
  1038. DENTRY_PATH(dentry), access, wish);
  1039. result = -EACCES;
  1040. }
  1041. out:
  1042. return result;
  1043. }
  1044. static int
  1045. smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
  1046. {
  1047. struct smb_request *req;
  1048. int result = -ENOMEM;
  1049. if (! (req = smb_alloc_request(server, 0)))
  1050. goto out;
  1051. smb_setup_header(req, SMBclose, 3, 0);
  1052. WSET(req->rq_header, smb_vwv0, fileid);
  1053. DSET(req->rq_header, smb_vwv1, utc2local(server, mtime));
  1054. req->rq_flags |= SMB_REQ_NORETRY;
  1055. result = smb_request_ok(req, SMBclose, 0, 0);
  1056. smb_rput(req);
  1057. out:
  1058. return result;
  1059. }
  1060. /*
  1061. * Win NT 4.0 has an apparent bug in that it fails to update the
  1062. * modify time when writing to a file. As a workaround, we update
  1063. * both modify and access time locally, and post the times to the
  1064. * server when closing the file.
  1065. */
  1066. static int
  1067. smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino)
  1068. {
  1069. struct smb_inode_info *ei = SMB_I(ino);
  1070. int result = 0;
  1071. if (smb_is_open(ino))
  1072. {
  1073. /*
  1074. * We clear the open flag in advance, in case another
  1075. * process observes the value while we block below.
  1076. */
  1077. ei->open = 0;
  1078. /*
  1079. * Kludge alert: SMB timestamps are accurate only to
  1080. * two seconds ... round the times to avoid needless
  1081. * cache invalidations!
  1082. */
  1083. if (ino->i_mtime.tv_sec & 1) {
  1084. ino->i_mtime.tv_sec--;
  1085. ino->i_mtime.tv_nsec = 0;
  1086. }
  1087. if (ino->i_atime.tv_sec & 1) {
  1088. ino->i_atime.tv_sec--;
  1089. ino->i_atime.tv_nsec = 0;
  1090. }
  1091. /*
  1092. * If the file is open with write permissions,
  1093. * update the time stamps to sync mtime and atime.
  1094. */
  1095. if ((server->opt.capabilities & SMB_CAP_UNIX) == 0 &&
  1096. (server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
  1097. !(ei->access == SMB_O_RDONLY))
  1098. {
  1099. struct smb_fattr fattr;
  1100. smb_get_inode_attr(ino, &fattr);
  1101. smb_proc_setattr_ext(server, ino, &fattr);
  1102. }
  1103. result = smb_proc_close(server, ei->fileid, ino->i_mtime.tv_sec);
  1104. /*
  1105. * Force a revalidation after closing ... some servers
  1106. * don't post the size until the file has been closed.
  1107. */
  1108. if (server->opt.protocol < SMB_PROTOCOL_NT1)
  1109. ei->oldmtime = 0;
  1110. ei->closed = jiffies;
  1111. }
  1112. return result;
  1113. }
  1114. int
  1115. smb_close(struct inode *ino)
  1116. {
  1117. int result = 0;
  1118. if (smb_is_open(ino)) {
  1119. struct smb_sb_info *server = server_from_inode(ino);
  1120. result = smb_proc_close_inode(server, ino);
  1121. }
  1122. return result;
  1123. }
  1124. /*
  1125. * This is used to close a file following a failed instantiate.
  1126. * Since we don't have an inode, we can't use any of the above.
  1127. */
  1128. int
  1129. smb_close_fileid(struct dentry *dentry, __u16 fileid)
  1130. {
  1131. struct smb_sb_info *server = server_from_dentry(dentry);
  1132. int result;
  1133. result = smb_proc_close(server, fileid, get_seconds());
  1134. return result;
  1135. }
  1136. /* In smb_proc_read and smb_proc_write we do not retry, because the
  1137. file-id would not be valid after a reconnection. */
  1138. static void
  1139. smb_proc_read_data(struct smb_request *req)
  1140. {
  1141. req->rq_iov[0].iov_base = req->rq_buffer;
  1142. req->rq_iov[0].iov_len = 3;
  1143. req->rq_iov[1].iov_base = req->rq_page;
  1144. req->rq_iov[1].iov_len = req->rq_rsize;
  1145. req->rq_iovlen = 2;
  1146. req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
  1147. }
  1148. static int
  1149. smb_proc_read(struct inode *inode, loff_t offset, int count, char *data)
  1150. {
  1151. struct smb_sb_info *server = server_from_inode(inode);
  1152. __u16 returned_count, data_len;
  1153. unsigned char *buf;
  1154. int result;
  1155. struct smb_request *req;
  1156. u8 rbuf[4];
  1157. result = -ENOMEM;
  1158. if (! (req = smb_alloc_request(server, 0)))
  1159. goto out;
  1160. smb_setup_header(req, SMBread, 5, 0);
  1161. buf = req->rq_header;
  1162. WSET(buf, smb_vwv0, SMB_I(inode)->fileid);
  1163. WSET(buf, smb_vwv1, count);
  1164. DSET(buf, smb_vwv2, offset);
  1165. WSET(buf, smb_vwv4, 0);
  1166. req->rq_page = data;
  1167. req->rq_rsize = count;
  1168. req->rq_callback = smb_proc_read_data;
  1169. req->rq_buffer = rbuf;
  1170. req->rq_flags |= SMB_REQ_NORETRY | SMB_REQ_STATIC;
  1171. result = smb_request_ok(req, SMBread, 5, -1);
  1172. if (result < 0)
  1173. goto out_free;
  1174. returned_count = WVAL(req->rq_header, smb_vwv0);
  1175. data_len = WVAL(rbuf, 1);
  1176. if (returned_count != data_len) {
  1177. printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
  1178. printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
  1179. returned_count, data_len);
  1180. }
  1181. result = data_len;
  1182. out_free:
  1183. smb_rput(req);
  1184. out:
  1185. VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
  1186. inode->i_ino, SMB_I(inode)->fileid, count, result);
  1187. return result;
  1188. }
  1189. static int
  1190. smb_proc_write(struct inode *inode, loff_t offset, int count, const char *data)
  1191. {
  1192. struct smb_sb_info *server = server_from_inode(inode);
  1193. int result;
  1194. u16 fileid = SMB_I(inode)->fileid;
  1195. u8 buf[4];
  1196. struct smb_request *req;
  1197. result = -ENOMEM;
  1198. if (! (req = smb_alloc_request(server, 0)))
  1199. goto out;
  1200. VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
  1201. inode->i_ino, fileid, count, offset);
  1202. smb_setup_header(req, SMBwrite, 5, count + 3);
  1203. WSET(req->rq_header, smb_vwv0, fileid);
  1204. WSET(req->rq_header, smb_vwv1, count);
  1205. DSET(req->rq_header, smb_vwv2, offset);
  1206. WSET(req->rq_header, smb_vwv4, 0);
  1207. buf[0] = 1;
  1208. WSET(buf, 1, count); /* yes, again ... */
  1209. req->rq_iov[1].iov_base = buf;
  1210. req->rq_iov[1].iov_len = 3;
  1211. req->rq_iov[2].iov_base = (char *) data;
  1212. req->rq_iov[2].iov_len = count;
  1213. req->rq_iovlen = 3;
  1214. req->rq_flags |= SMB_REQ_NORETRY;
  1215. result = smb_request_ok(req, SMBwrite, 1, 0);
  1216. if (result >= 0)
  1217. result = WVAL(req->rq_header, smb_vwv0);
  1218. smb_rput(req);
  1219. out:
  1220. return result;
  1221. }
  1222. /*
  1223. * In smb_proc_readX and smb_proc_writeX we do not retry, because the
  1224. * file-id would not be valid after a reconnection.
  1225. */
  1226. #define SMB_READX_MAX_PAD 64
  1227. static void
  1228. smb_proc_readX_data(struct smb_request *req)
  1229. {
  1230. /* header length, excluding the netbios length (-4) */
  1231. int hdrlen = SMB_HEADER_LEN + req->rq_resp_wct*2 - 2;
  1232. int data_off = WVAL(req->rq_header, smb_vwv6);
  1233. /*
  1234. * Some genius made the padding to the data bytes arbitrary.
  1235. * So we must first calculate the amount of padding used by the server.
  1236. */
  1237. data_off -= hdrlen;
  1238. if (data_off > SMB_READX_MAX_PAD || data_off < 0) {
  1239. PARANOIA("offset is larger than SMB_READX_MAX_PAD or negative!\n");
  1240. PARANOIA("%d > %d || %d < 0\n", data_off, SMB_READX_MAX_PAD, data_off);
  1241. req->rq_rlen = req->rq_bufsize + 1;
  1242. return;
  1243. }
  1244. req->rq_iov[0].iov_base = req->rq_buffer;
  1245. req->rq_iov[0].iov_len = data_off;
  1246. req->rq_iov[1].iov_base = req->rq_page;
  1247. req->rq_iov[1].iov_len = req->rq_rsize;
  1248. req->rq_iovlen = 2;
  1249. req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
  1250. }
  1251. static int
  1252. smb_proc_readX(struct inode *inode, loff_t offset, int count, char *data)
  1253. {
  1254. struct smb_sb_info *server = server_from_inode(inode);
  1255. unsigned char *buf;
  1256. int result;
  1257. struct smb_request *req;
  1258. static char pad[SMB_READX_MAX_PAD];
  1259. result = -ENOMEM;
  1260. if (! (req = smb_alloc_request(server, 0)))
  1261. goto out;
  1262. smb_setup_header(req, SMBreadX, 12, 0);
  1263. buf = req->rq_header;
  1264. WSET(buf, smb_vwv0, 0x00ff);
  1265. WSET(buf, smb_vwv1, 0);
  1266. WSET(buf, smb_vwv2, SMB_I(inode)->fileid);
  1267. DSET(buf, smb_vwv3, (u32)offset); /* low 32 bits */
  1268. WSET(buf, smb_vwv5, count);
  1269. WSET(buf, smb_vwv6, 0);
  1270. DSET(buf, smb_vwv7, 0);
  1271. WSET(buf, smb_vwv9, 0);
  1272. DSET(buf, smb_vwv10, (u32)(offset >> 32)); /* high 32 bits */
  1273. WSET(buf, smb_vwv11, 0);
  1274. req->rq_page = data;
  1275. req->rq_rsize = count;
  1276. req->rq_callback = smb_proc_readX_data;
  1277. req->rq_buffer = pad;
  1278. req->rq_bufsize = SMB_READX_MAX_PAD;
  1279. req->rq_flags |= SMB_REQ_STATIC | SMB_REQ_NORETRY;
  1280. result = smb_request_ok(req, SMBreadX, 12, -1);
  1281. if (result < 0)
  1282. goto out_free;
  1283. result = WVAL(req->rq_header, smb_vwv5);
  1284. out_free:
  1285. smb_rput(req);
  1286. out:
  1287. VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
  1288. inode->i_ino, SMB_I(inode)->fileid, count, result);
  1289. return result;
  1290. }
  1291. static int
  1292. smb_proc_writeX(struct inode *inode, loff_t offset, int count, const char *data)
  1293. {
  1294. struct smb_sb_info *server = server_from_inode(inode);
  1295. int result;
  1296. u8 *p;
  1297. static u8 pad[4];
  1298. struct smb_request *req;
  1299. result = -ENOMEM;
  1300. if (! (req = smb_alloc_request(server, 0)))
  1301. goto out;
  1302. VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
  1303. inode->i_ino, SMB_I(inode)->fileid, count, offset);
  1304. p = smb_setup_header(req, SMBwriteX, 14, count + 1);
  1305. WSET(req->rq_header, smb_vwv0, 0x00ff);
  1306. WSET(req->rq_header, smb_vwv1, 0);
  1307. WSET(req->rq_header, smb_vwv2, SMB_I(inode)->fileid);
  1308. DSET(req->rq_header, smb_vwv3, (u32)offset); /* low 32 bits */
  1309. DSET(req->rq_header, smb_vwv5, 0);
  1310. WSET(req->rq_header, smb_vwv7, 0); /* write mode */
  1311. WSET(req->rq_header, smb_vwv8, 0);
  1312. WSET(req->rq_header, smb_vwv9, 0);
  1313. WSET(req->rq_header, smb_vwv10, count); /* data length */
  1314. WSET(req->rq_header, smb_vwv11, smb_vwv12 + 2 + 1);
  1315. DSET(req->rq_header, smb_vwv12, (u32)(offset >> 32));
  1316. req->rq_iov[1].iov_base = pad;
  1317. req->rq_iov[1].iov_len = 1;
  1318. req->rq_iov[2].iov_base = (char *) data;
  1319. req->rq_iov[2].iov_len = count;
  1320. req->rq_iovlen = 3;
  1321. req->rq_flags |= SMB_REQ_NORETRY;
  1322. result = smb_request_ok(req, SMBwriteX, 6, 0);
  1323. if (result >= 0)
  1324. result = WVAL(req->rq_header, smb_vwv2);
  1325. smb_rput(req);
  1326. out:
  1327. return result;
  1328. }
  1329. int
  1330. smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid)
  1331. {
  1332. struct smb_sb_info *server = server_from_dentry(dentry);
  1333. char *p;
  1334. int result;
  1335. struct smb_request *req;
  1336. result = -ENOMEM;
  1337. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  1338. goto out;
  1339. p = smb_setup_header(req, SMBcreate, 3, 0);
  1340. WSET(req->rq_header, smb_vwv0, attr);
  1341. DSET(req->rq_header, smb_vwv1, utc2local(server, ctime));
  1342. result = smb_simple_encode_path(req, &p, dentry, NULL);
  1343. if (result < 0)
  1344. goto out_free;
  1345. smb_setup_bcc(req, p);
  1346. result = smb_request_ok(req, SMBcreate, 1, 0);
  1347. if (result < 0)
  1348. goto out_free;
  1349. *fileid = WVAL(req->rq_header, smb_vwv0);
  1350. result = 0;
  1351. out_free:
  1352. smb_rput(req);
  1353. out:
  1354. return result;
  1355. }
  1356. int
  1357. smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry)
  1358. {
  1359. struct smb_sb_info *server = server_from_dentry(old_dentry);
  1360. char *p;
  1361. int result;
  1362. struct smb_request *req;
  1363. result = -ENOMEM;
  1364. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  1365. goto out;
  1366. p = smb_setup_header(req, SMBmv, 1, 0);
  1367. WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN | aDIR);
  1368. result = smb_simple_encode_path(req, &p, old_dentry, NULL);
  1369. if (result < 0)
  1370. goto out_free;
  1371. result = smb_simple_encode_path(req, &p, new_dentry, NULL);
  1372. if (result < 0)
  1373. goto out_free;
  1374. smb_setup_bcc(req, p);
  1375. if ((result = smb_request_ok(req, SMBmv, 0, 0)) < 0)
  1376. goto out_free;
  1377. result = 0;
  1378. out_free:
  1379. smb_rput(req);
  1380. out:
  1381. return result;
  1382. }
  1383. /*
  1384. * Code common to mkdir and rmdir.
  1385. */
  1386. static int
  1387. smb_proc_generic_command(struct dentry *dentry, __u8 command)
  1388. {
  1389. struct smb_sb_info *server = server_from_dentry(dentry);
  1390. char *p;
  1391. int result;
  1392. struct smb_request *req;
  1393. result = -ENOMEM;
  1394. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  1395. goto out;
  1396. p = smb_setup_header(req, command, 0, 0);
  1397. result = smb_simple_encode_path(req, &p, dentry, NULL);
  1398. if (result < 0)
  1399. goto out_free;
  1400. smb_setup_bcc(req, p);
  1401. result = smb_request_ok(req, command, 0, 0);
  1402. if (result < 0)
  1403. goto out_free;
  1404. result = 0;
  1405. out_free:
  1406. smb_rput(req);
  1407. out:
  1408. return result;
  1409. }
  1410. int
  1411. smb_proc_mkdir(struct dentry *dentry)
  1412. {
  1413. return smb_proc_generic_command(dentry, SMBmkdir);
  1414. }
  1415. int
  1416. smb_proc_rmdir(struct dentry *dentry)
  1417. {
  1418. return smb_proc_generic_command(dentry, SMBrmdir);
  1419. }
  1420. #if SMBFS_POSIX_UNLINK
  1421. /*
  1422. * Removes readonly attribute from a file. Used by unlink to give posix
  1423. * semantics.
  1424. */
  1425. static int
  1426. smb_set_rw(struct dentry *dentry,struct smb_sb_info *server)
  1427. {
  1428. int result;
  1429. struct smb_fattr fattr;
  1430. /* FIXME: cifsUE should allow removing a readonly file. */
  1431. /* first get current attribute */
  1432. smb_init_dirent(server, &fattr);
  1433. result = server->ops->getattr(server, dentry, &fattr);
  1434. smb_finish_dirent(server, &fattr);
  1435. if (result < 0)
  1436. return result;
  1437. /* if RONLY attribute is set, remove it */
  1438. if (fattr.attr & aRONLY) { /* read only attribute is set */
  1439. fattr.attr &= ~aRONLY;
  1440. result = smb_proc_setattr_core(server, dentry, fattr.attr);
  1441. }
  1442. return result;
  1443. }
  1444. #endif
  1445. int
  1446. smb_proc_unlink(struct dentry *dentry)
  1447. {
  1448. struct smb_sb_info *server = server_from_dentry(dentry);
  1449. int flag = 0;
  1450. char *p;
  1451. int result;
  1452. struct smb_request *req;
  1453. result = -ENOMEM;
  1454. if (! (req = smb_alloc_request(server, PAGE_SIZE)))
  1455. goto out;
  1456. retry:
  1457. p = smb_setup_header(req, SMBunlink, 1, 0);
  1458. WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN);
  1459. result = smb_simple_encode_path(req, &p, dentry, NULL);
  1460. if (result < 0)
  1461. goto out_free;
  1462. smb_setup_bcc(req, p);
  1463. if ((result = smb_request_ok(req, SMBunlink, 0, 0)) < 0) {
  1464. #if SMBFS_POSIX_UNLINK
  1465. if (result == -EACCES && !flag) {
  1466. /* Posix semantics is for the read-only state
  1467. of a file to be ignored in unlink(). In the
  1468. SMB world a unlink() is refused on a
  1469. read-only file. To make things easier for
  1470. unix users we try to override the files
  1471. permission if the unlink fails with the
  1472. right error.
  1473. This introduces a race condition that could
  1474. lead to a file being written by someone who
  1475. shouldn't have access, but as far as I can
  1476. tell that is unavoidable */
  1477. /* remove RONLY attribute and try again */
  1478. result = smb_set_rw(dentry,server);
  1479. if (result == 0) {
  1480. flag = 1;
  1481. req->rq_flags = 0;
  1482. goto retry;
  1483. }
  1484. }
  1485. #endif
  1486. goto out_free;
  1487. }
  1488. result = 0;
  1489. out_free:
  1490. smb_rput(req);
  1491. out:
  1492. return result;
  1493. }
  1494. int
  1495. smb_proc_flush(struct smb_sb_info *server, __u16 fileid)
  1496. {
  1497. int result;
  1498. struct smb_request *req;
  1499. result = -ENOMEM;
  1500. if (! (req = smb_alloc_request(server, 0)))
  1501. goto out;
  1502. smb_setup_header(req, SMBflush, 1, 0);
  1503. WSET(req->rq_header, smb_vwv0, fileid);
  1504. req->rq_flags |= SMB_REQ_NORETRY;
  1505. result = smb_request_ok(req, SMBflush, 0, 0);
  1506. smb_rput(req);
  1507. out:
  1508. return result;
  1509. }
  1510. static int
  1511. smb_proc_trunc32(struct inode *inode, loff_t length)
  1512. {
  1513. /*
  1514. * Writing 0bytes is old-SMB magic for truncating files.
  1515. * MAX_NON_LFS should prevent this from being called with a too
  1516. * large offset.
  1517. */
  1518. return smb_proc_write(inode, length, 0, NULL);
  1519. }
  1520. static int
  1521. smb_proc_trunc64(struct inode *inode, loff_t length)
  1522. {
  1523. struct smb_sb_info *server = server_from_inode(inode);
  1524. int result;
  1525. char *param;
  1526. char *data;
  1527. struct smb_request *req;
  1528. result = -ENOMEM;
  1529. if (! (req = smb_alloc_request(server, 14)))
  1530. goto out;
  1531. param = req->rq_buffer;
  1532. data = req->rq_buffer + 6;
  1533. /* FIXME: must we also set allocation size? winNT seems to do that */
  1534. WSET(param, 0, SMB_I(inode)->fileid);
  1535. WSET(param, 2, SMB_SET_FILE_END_OF_FILE_INFO);
  1536. WSET(param, 4, 0);
  1537. LSET(data, 0, length);
  1538. req->rq_trans2_command = TRANSACT2_SETFILEINFO;
  1539. req->rq_ldata = 8;
  1540. req->rq_data = data;
  1541. req->rq_lparm = 6;
  1542. req->rq_parm = param;
  1543. req->rq_flags |= SMB_REQ_NORETRY;
  1544. result = smb_add_request(req);
  1545. if (result < 0)
  1546. goto out_free;
  1547. result = 0;
  1548. if (req->rq_rcls != 0)
  1549. result = smb_errno(req);
  1550. out_free:
  1551. smb_rput(req);
  1552. out:
  1553. return result;
  1554. }
  1555. static int
  1556. smb_proc_trunc95(struct inode *inode, loff_t length)
  1557. {
  1558. struct smb_sb_info *server = server_from_inode(inode);
  1559. int result = smb_proc_trunc32(inode, length);
  1560. /*
  1561. * win9x doesn't appear to update the size immediately.
  1562. * It will return the old file size after the truncate,
  1563. * confusing smbfs. So we force an update.
  1564. *
  1565. * FIXME: is this still necessary?
  1566. */
  1567. smb_proc_flush(server, SMB_I(inode)->fileid);
  1568. return result;
  1569. }
  1570. static void
  1571. smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
  1572. {
  1573. memset(fattr, 0, sizeof(*fattr));
  1574. fattr->f_nlink = 1;
  1575. fattr->f_uid = server->mnt->uid;
  1576. fattr->f_gid = server->mnt->gid;
  1577. fattr->f_blksize = SMB_ST_BLKSIZE;
  1578. fattr->f_unix = 0;
  1579. }
  1580. static void
  1581. smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
  1582. {
  1583. if (fattr->f_unix)
  1584. return;
  1585. fattr->f_mode = server->mnt->file_mode;
  1586. if (fattr->attr & aDIR) {
  1587. fattr->f_mode = server->mnt->dir_mode;
  1588. fattr->f_size = SMB_ST_BLKSIZE;
  1589. }
  1590. /* Check the read-only flag */
  1591. if (fattr->attr & aRONLY)
  1592. fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  1593. /* How many 512 byte blocks do we need for this file? */
  1594. fattr->f_blocks = 0;
  1595. if (fattr->f_size != 0)
  1596. fattr->f_blocks = 1 + ((fattr->f_size-1) >> 9);
  1597. return;
  1598. }
  1599. void
  1600. smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr,
  1601. struct super_block *sb)
  1602. {
  1603. smb_init_dirent(server, fattr);
  1604. fattr->attr = aDIR;
  1605. fattr->f_ino = 2; /* traditional root inode number */
  1606. fattr->f_mtime = current_fs_time(sb);
  1607. smb_finish_dirent(server, fattr);
  1608. }
  1609. /*
  1610. * Decode a dirent for old protocols
  1611. *
  1612. * qname is filled with the decoded, and possibly translated, name.
  1613. * fattr receives decoded attributes
  1614. *
  1615. * Bugs Noted:
  1616. * (1) Pathworks servers may pad the name with extra spaces.
  1617. */
  1618. static char *
  1619. smb_decode_short_dirent(struct smb_sb_info *server, char *p,
  1620. struct qstr *qname, struct smb_fattr *fattr,
  1621. unsigned char *name_buf)
  1622. {
  1623. int len;
  1624. /*
  1625. * SMB doesn't have a concept of inode numbers ...
  1626. */
  1627. smb_init_dirent(server, fattr);
  1628. fattr->f_ino = 0; /* FIXME: do we need this? */
  1629. p += SMB_STATUS_SIZE; /* reserved (search_status) */
  1630. fattr->attr = *p;
  1631. fattr->f_mtime.tv_sec = date_dos2unix(server, WVAL(p, 3), WVAL(p, 1));
  1632. fattr->f_mtime.tv_nsec = 0;
  1633. fattr->f_size = DVAL(p, 5);
  1634. fattr->f_ctime = fattr->f_mtime;
  1635. fattr->f_atime = fattr->f_mtime;
  1636. qname->name = p + 9;
  1637. len = strnlen(qname->name, 12);
  1638. /*
  1639. * Trim trailing blanks for Pathworks servers
  1640. */
  1641. while (len > 2 && qname->name[len-1] == ' ')
  1642. len--;
  1643. smb_finish_dirent(server, fattr);
  1644. #if 0
  1645. /* FIXME: These only work for ascii chars, and recent smbmount doesn't
  1646. allow the flag to be set anyway. It kills const. Remove? */
  1647. switch (server->opt.case_handling) {
  1648. case SMB_CASE_UPPER:
  1649. str_upper(entry->name, len);
  1650. break;
  1651. case SMB_CASE_LOWER:
  1652. str_lower(entry->name, len);
  1653. break;
  1654. default:
  1655. break;
  1656. }
  1657. #endif
  1658. qname->len = 0;
  1659. len = server->ops->convert(name_buf, SMB_MAXNAMELEN,
  1660. qname->name, len,
  1661. server->remote_nls, server->local_nls);
  1662. if (len > 0) {
  1663. qname->len = len;
  1664. qname->name = name_buf;
  1665. DEBUG1("len=%d, name=%.*s\n",qname->len,qname->len,qname->name);
  1666. }
  1667. return p + 22;
  1668. }
  1669. /*
  1670. * This routine is used to read in directory entries from the network.
  1671. * Note that it is for short directory name seeks, i.e.: protocol <
  1672. * SMB_PROTOCOL_LANMAN2
  1673. */
  1674. static int
  1675. smb_proc_readdir_short(struct file *filp, void *dirent, filldir_t filldir,
  1676. struct smb_cache_control *ctl)
  1677. {
  1678. struct dentry *dir = filp->f_dentry;
  1679. struct smb_sb_info *server = server_from_dentry(dir);
  1680. struct qstr qname;
  1681. struct smb_fattr fattr;
  1682. char *p;
  1683. int result;
  1684. int i, first, entries_seen, entries;
  1685. int entries_asked = (server->opt.max_xmit - 100) / SMB_DIRINFO_SIZE;
  1686. __u16 bcc;
  1687. __u16 count;
  1688. char status[SMB_STATUS_SIZE];
  1689. static struct qstr mask = {
  1690. .name = "*.*",
  1691. .len = 3,
  1692. };
  1693. unsigned char *last_status;
  1694. struct smb_request *req;
  1695. unsigned char *name_buf;
  1696. VERBOSE("%s/%s\n", DENTRY_PATH(dir));
  1697. lock_kernel();
  1698. result = -ENOMEM;
  1699. if (! (name_buf = kmalloc(SMB_MAXNAMELEN, GFP_KERNEL)))
  1700. goto out;
  1701. first = 1;
  1702. entries = 0;
  1703. entries_seen = 2; /* implicit . and .. */
  1704. result = -ENOMEM;
  1705. if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
  1706. goto out_name;
  1707. while (1) {
  1708. p = smb_setup_header(req, SMBsearch, 2, 0);
  1709. WSET(req->rq_header, smb_vwv0, entries_asked);
  1710. WSET(req->rq_header, smb_vwv1, aDIR);
  1711. if (first == 1) {
  1712. result = smb_simple_encode_path(req, &p, dir, &mask);
  1713. if (result < 0)
  1714. goto out_free;
  1715. if (p + 3 > (char *)req->rq_buffer + req->rq_bufsize) {
  1716. result = -ENAMETOOLONG;
  1717. goto out_free;
  1718. }
  1719. *p++ = 5;
  1720. WSET(p, 0, 0);
  1721. p += 2;
  1722. first = 0;
  1723. } else {
  1724. if (p + 5 + SMB_STATUS_SIZE >
  1725. (char *)req->rq_buffer + req->rq_bufsize) {
  1726. result = -ENAMETOOLONG;
  1727. goto out_free;
  1728. }
  1729. *p++ = 4;
  1730. *p++ = 0;
  1731. *p++ = 5;
  1732. WSET(p, 0, SMB_STATUS_SIZE);
  1733. p += 2;
  1734. memcpy(p, status, SMB_STATUS_SIZE);
  1735. p += SMB_STATUS_SIZE;
  1736. }
  1737. smb_setup_bcc(req, p);
  1738. result = smb_request_ok(req, SMBsearch, 1, -1);
  1739. if (result < 0) {
  1740. if ((req->rq_rcls == ERRDOS) &&
  1741. (req->rq_err == ERRnofiles))
  1742. break;
  1743. goto out_free;
  1744. }
  1745. count = WVAL(req->rq_header, smb_vwv0);
  1746. if (count <= 0)
  1747. break;
  1748. result = -EIO;
  1749. bcc = smb_bcc(req->rq_header);
  1750. if (bcc != count * SMB_DIRINFO_SIZE + 3)
  1751. goto out_free;
  1752. p = req->rq_buffer + 3;
  1753. /* Make sure the response fits in the buffer. Fixed sized
  1754. entries means we don't have to check in the decode loop. */
  1755. last_status = req->rq_buffer + 3 + (count-1) * SMB_DIRINFO_SIZE;
  1756. if (last_status + SMB_DIRINFO_SIZE >=
  1757. req->rq_buffer + req->rq_bufsize) {
  1758. printk(KERN_ERR "smb_proc_readdir_short: "
  1759. "last dir entry outside buffer! "
  1760. "%d@%p %d@%p\n", SMB_DIRINFO_SIZE, last_status,
  1761. req->rq_bufsize, req->rq_buffer);
  1762. goto out_free;
  1763. }
  1764. /* Read the last entry into the status field. */
  1765. memcpy(status, last_status, SMB_STATUS_SIZE);
  1766. /* Now we are ready to parse smb directory entries. */
  1767. for (i = 0; i < count; i++) {
  1768. p = smb_decode_short_dirent(server, p,
  1769. &qname, &fattr, name_buf);
  1770. if (qname.len == 0)
  1771. continue;
  1772. if (entries_seen == 2 && qname.name[0] == '.') {
  1773. if (qname.len == 1)
  1774. continue;
  1775. if (qname.name[1] == '.' && qname.len == 2)
  1776. continue;
  1777. }
  1778. if (!smb_fill_cache(filp, dirent, filldir, ctl,
  1779. &qname, &fattr))
  1780. ; /* stop reading? */
  1781. entries_seen++;
  1782. }
  1783. }
  1784. result = entries;
  1785. out_free:
  1786. smb_rput(req);
  1787. out_name:
  1788. kfree(name_buf);
  1789. out:
  1790. unlock_kernel();
  1791. return result;
  1792. }
  1793. static void smb_decode_unix_basic(struct smb_fattr *fattr, struct smb_sb_info *server, char *p)
  1794. {
  1795. u64 size, disk_bytes;
  1796. /* FIXME: verify nls support. all is sent as utf8? */
  1797. fattr->f_unix = 1;
  1798. fattr->f_mode = 0;
  1799. /* FIXME: use the uniqueID from the remote instead? */
  1800. /* 0 L file size in bytes */
  1801. /* 8 L file size on disk in bytes (block count) */
  1802. /* 40 L uid */
  1803. /* 48 L gid */
  1804. /* 56 W file type */
  1805. /* 60 L devmajor */
  1806. /* 68 L devminor */
  1807. /* 76 L unique ID (inode) */
  1808. /* 84 L permissions */
  1809. /* 92 L link count */
  1810. size = LVAL(p, 0);
  1811. disk_bytes = LVAL(p, 8);
  1812. /*
  1813. * Some samba versions round up on-disk byte usage
  1814. * to 1MB boundaries, making it useless. When seeing
  1815. * that, use the size instead.
  1816. */
  1817. if (!(disk_bytes & 0xfffff))
  1818. disk_bytes = size+511;
  1819. fattr->f_size = size;
  1820. fattr->f_blocks = disk_bytes >> 9;
  1821. fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 16));
  1822. fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 24));
  1823. fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 32));
  1824. if (server->mnt->flags & SMB_MOUNT_UID)
  1825. fattr->f_uid = server->mnt->uid;
  1826. else
  1827. fattr->f_uid = LVAL(p, 40);
  1828. if (server->mnt->flags & SMB_MOUNT_GID)
  1829. fattr->f_gid = server->mnt->gid;
  1830. else
  1831. fattr->f_gid = LVAL(p, 48);
  1832. fattr->f_mode |= smb_filetype_to_mode(WVAL(p, 56));
  1833. if (S_ISBLK(fattr->f_mode) || S_ISCHR(fattr->f_mode)) {
  1834. __u64 major = LVAL(p, 60);
  1835. __u64 minor = LVAL(p, 68);
  1836. fattr->f_rdev = MKDEV(major & 0xffffffff, minor & 0xffffffff);
  1837. if (MAJOR(fattr->f_rdev) != (major & 0xffffffff) ||
  1838. MINOR(fattr->f_rdev) != (minor & 0xffffffff))
  1839. fattr->f_rdev = 0;
  1840. }
  1841. fattr->f_mode |= LVAL(p, 84);
  1842. if ( (server->mnt->flags & SMB_MOUNT_DMODE) &&
  1843. (S_ISDIR(fattr->f_mode)) )
  1844. fattr->f_mode = (server->mnt->dir_mode & S_IRWXUGO) | S_IFDIR;
  1845. else if ( (server->mnt->flags & SMB_MOUNT_FMODE) &&
  1846. !(S_ISDIR(fattr->f_mode)) )
  1847. fattr->f_mode = (server->mnt->file_mode & S_IRWXUGO) |
  1848. (fattr->f_mode & S_IFMT);
  1849. }
  1850. /*
  1851. * Interpret a long filename structure using the specified info level:
  1852. * level 1 for anything below NT1 protocol
  1853. * level 260 for NT1 protocol
  1854. *
  1855. * qname is filled with the decoded, and possibly translated, name
  1856. * fattr receives decoded attributes.
  1857. *
  1858. * Bugs Noted:
  1859. * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
  1860. */
  1861. static char *
  1862. smb_decode_long_dirent(struct smb_sb_info *server, char *p, int level,
  1863. struct qstr *qname, struct smb_fattr *fattr,
  1864. unsigned char *name_buf)
  1865. {
  1866. char *result;
  1867. unsigned int len = 0;
  1868. int n;
  1869. __u16 date, time;
  1870. int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
  1871. /*
  1872. * SMB doesn't have a concept of inode numbers ...
  1873. */
  1874. smb_init_dirent(server, fattr);
  1875. fattr->f_ino = 0; /* FIXME: do we need this? */
  1876. switch (level) {
  1877. case 1:
  1878. len = *((unsigned char *) p + 22);
  1879. qname->name = p + 23;
  1880. result = p + 24 + len;
  1881. date = WVAL(p, 0);
  1882. time = WVAL(p, 2);
  1883. fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
  1884. fattr->f_ctime.tv_nsec = 0;
  1885. date = WVAL(p, 4);
  1886. time = WVAL(p, 6);
  1887. fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
  1888. fattr->f_atime.tv_nsec = 0;
  1889. date = WVAL(p, 8);
  1890. time = WVAL(p, 10);
  1891. fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
  1892. fattr->f_mtime.tv_nsec = 0;
  1893. fattr->f_size = DVAL(p, 12);
  1894. /* ULONG allocation size */
  1895. fattr->attr = WVAL(p, 20);
  1896. VERBOSE("info 1 at %p, len=%d, name=%.*s\n",
  1897. p, len, len, qname->name);
  1898. break;
  1899. case 260:
  1900. result = p + WVAL(p, 0);
  1901. len = DVAL(p, 60);
  1902. if (len > 255) len = 255;
  1903. /* NT4 null terminates, unless we are using unicode ... */
  1904. qname->name = p + 94;
  1905. if (!unicode && len && qname->name[len-1] == '\0')
  1906. len--;
  1907. fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 8));
  1908. fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 16));
  1909. fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 24));
  1910. /* change time (32) */
  1911. fattr->f_size = LVAL(p, 40);
  1912. /* alloc size (48) */
  1913. fattr->attr = DVAL(p, 56);
  1914. VERBOSE("info 260 at %p, len=%d, name=%.*s\n",
  1915. p, len, len, qname->name);
  1916. break;
  1917. case SMB_FIND_FILE_UNIX:
  1918. result = p + WVAL(p, 0);
  1919. qname->name = p + 108;
  1920. len = strlen(qname->name);
  1921. /* FIXME: should we check the length?? */
  1922. p += 8;
  1923. smb_decode_unix_basic(fattr, server, p);
  1924. VERBOSE("info SMB_FIND_FILE_UNIX at %p, len=%d, name=%.*s\n",
  1925. p, len, len, qname->name);
  1926. break;
  1927. default:
  1928. PARANOIA("Unknown info level %d\n", level);
  1929. result = p + WVAL(p, 0);
  1930. goto out;
  1931. }
  1932. smb_finish_dirent(server, fattr);
  1933. #if 0
  1934. /* FIXME: These only work for ascii chars, and recent smbmount doesn't
  1935. allow the flag to be set anyway. Remove? */
  1936. switch (server->opt.case_handling) {
  1937. case SMB_CASE_UPPER:
  1938. str_upper(qname->name, len);
  1939. break;
  1940. case SMB_CASE_LOWER:
  1941. str_lower(qname->name, len);
  1942. break;
  1943. default:
  1944. break;
  1945. }
  1946. #endif
  1947. qname->len = 0;
  1948. n = server->ops->convert(name_buf, SMB_MAXNAMELEN,
  1949. qname->name, len,
  1950. server->remote_nls, server->local_nls);
  1951. if (n > 0) {
  1952. qname->len = n;
  1953. qname->name = name_buf;
  1954. }
  1955. out:
  1956. return result;
  1957. }
  1958. /* findfirst/findnext flags */
  1959. #define SMB_CLOSE_AFTER_FIRST (1<<0)
  1960. #define SMB_CLOSE_IF_END (1<<1)
  1961. #define SMB_REQUIRE_RESUME_KEY (1<<2)
  1962. #define SMB_CONTINUE_BIT (1<<3)
  1963. /*
  1964. * Note: samba-2.0.7 (at least) has a very similar routine, cli_list, in
  1965. * source/libsmb/clilist.c. When looking for smb bugs in the readdir code,
  1966. * go there for advise.
  1967. *
  1968. * Bugs Noted:
  1969. * (1) When using Info Level 1 Win NT 4.0 truncates directory listings
  1970. * for certain patterns of names and/or lengths. The breakage pattern
  1971. * is completely reproducible and can be toggled by the creation of a
  1972. * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
  1973. */
  1974. static int
  1975. smb_proc_readdir_long(struct file *filp, void *dirent, filldir_t filldir,
  1976. struct smb_cache_control *ctl)
  1977. {
  1978. struct dentry *dir = filp->f_dentry;
  1979. struct smb_sb_info *server = server_from_dentry(dir);
  1980. struct qstr qname;
  1981. struct smb_fattr fattr;
  1982. unsigned char *p, *lastname;
  1983. char *mask, *param;
  1984. __u16 command;
  1985. int first, entries_seen;
  1986. /* Both NT and OS/2 accept info level 1 (but see note below). */
  1987. int info_level = 260;
  1988. const int max_matches = 512;
  1989. unsigned int ff_searchcount = 0;
  1990. unsigned int ff_eos = 0;
  1991. unsigned int ff_lastname = 0;
  1992. unsigned int ff_dir_handle = 0;
  1993. unsigned int loop_count = 0;
  1994. unsigned int mask_len, i;
  1995. int result;
  1996. struct smb_request *req;
  1997. unsigned char *name_buf;
  1998. static struct qstr star = {
  1999. .name = "*",
  2000. .len = 1,
  2001. };
  2002. lock_kernel();
  2003. /*
  2004. * We always prefer unix style. Use info level 1 for older
  2005. * servers that don't do 260.
  2006. */
  2007. if (server->opt.capabilities & SMB_CAP_UNIX)
  2008. info_level = SMB_FIND_FILE_UNIX;
  2009. else if (server->opt.protocol < SMB_PROTOCOL_NT1)
  2010. info_level = 1;
  2011. result = -ENOMEM;
  2012. if (! (name_buf = kmalloc(SMB_MAXNAMELEN+2, GFP_KERNEL)))
  2013. goto out;
  2014. if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
  2015. goto out_name;
  2016. param = req->rq_buffer;
  2017. /*
  2018. * Encode the initial path
  2019. */
  2020. mask = param + 12;
  2021. result = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dir, &star);
  2022. if (result <= 0)
  2023. goto out_free;
  2024. mask_len = result - 1; /* mask_len is strlen, not #bytes */
  2025. result = 0;
  2026. first = 1;
  2027. VERBOSE("starting mask_len=%d, mask=%s\n", mask_len, mask);
  2028. entries_seen = 2;
  2029. ff_eos = 0;
  2030. while (ff_eos == 0) {
  2031. loop_count += 1;
  2032. if (loop_count > 10) {
  2033. printk(KERN_WARNING "smb_proc_readdir_long: "
  2034. "Looping in FIND_NEXT??\n");
  2035. result = -EIO;
  2036. break;
  2037. }
  2038. if (first != 0) {
  2039. command = TRANSACT2_FINDFIRST;
  2040. WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
  2041. WSET(param, 2, max_matches); /* max count */
  2042. WSET(param, 4, SMB_CLOSE_IF_END);
  2043. WSET(param, 6, info_level);
  2044. DSET(param, 8, 0);
  2045. } else {
  2046. command = TRANSACT2_FINDNEXT;
  2047. VERBOSE("handle=0x%X, lastname=%d, mask=%.*s\n",
  2048. ff_dir_handle, ff_lastname, mask_len, mask);
  2049. WSET(param, 0, ff_dir_handle); /* search handle */
  2050. WSET(param, 2, max_matches); /* max count */
  2051. WSET(param, 4, info_level);
  2052. DSET(param, 6, 0);
  2053. WSET(param, 10, SMB_CONTINUE_BIT|SMB_CLOSE_IF_END);
  2054. }
  2055. req->rq_trans2_command = command;
  2056. req->rq_ldata = 0;
  2057. req->rq_data = NULL;
  2058. req->rq_lparm = 12 + mask_len + 1;
  2059. req->rq_parm = param;
  2060. req->rq_flags = 0;
  2061. result = smb_add_request(req);
  2062. if (result < 0) {
  2063. PARANOIA("error=%d, breaking\n", result);
  2064. break;
  2065. }
  2066. if (req->rq_rcls == ERRSRV && req->rq_err == ERRerror) {
  2067. /* a damn Win95 bug - sometimes it clags if you
  2068. ask it too fast */
  2069. current->state = TASK_INTERRUPTIBLE;
  2070. schedule_timeout(HZ/5);
  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. LSET(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. }