inode.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2010
  5. * Author(s): Steve French ([email protected])
  6. *
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/stat.h>
  10. #include <linux/slab.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/freezer.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/wait_bit.h>
  15. #include <linux/fiemap.h>
  16. #include <asm/div64.h>
  17. #include "cifsfs.h"
  18. #include "cifspdu.h"
  19. #include "cifsglob.h"
  20. #include "cifsproto.h"
  21. #include "smb2proto.h"
  22. #include "cifs_debug.h"
  23. #include "cifs_fs_sb.h"
  24. #include "cifs_unicode.h"
  25. #include "fscache.h"
  26. #include "fs_context.h"
  27. #include "cifs_ioctl.h"
  28. #include "cached_dir.h"
  29. static void cifs_set_ops(struct inode *inode)
  30. {
  31. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  32. switch (inode->i_mode & S_IFMT) {
  33. case S_IFREG:
  34. inode->i_op = &cifs_file_inode_ops;
  35. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  36. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  37. inode->i_fop = &cifs_file_direct_nobrl_ops;
  38. else
  39. inode->i_fop = &cifs_file_direct_ops;
  40. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  41. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  42. inode->i_fop = &cifs_file_strict_nobrl_ops;
  43. else
  44. inode->i_fop = &cifs_file_strict_ops;
  45. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  46. inode->i_fop = &cifs_file_nobrl_ops;
  47. else { /* not direct, send byte range locks */
  48. inode->i_fop = &cifs_file_ops;
  49. }
  50. /* check if server can support readahead */
  51. if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
  52. PAGE_SIZE + MAX_CIFS_HDR_SIZE)
  53. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  54. else
  55. inode->i_data.a_ops = &cifs_addr_ops;
  56. break;
  57. case S_IFDIR:
  58. #ifdef CONFIG_CIFS_DFS_UPCALL
  59. if (IS_AUTOMOUNT(inode)) {
  60. inode->i_op = &cifs_dfs_referral_inode_operations;
  61. } else {
  62. #else /* NO DFS support, treat as a directory */
  63. {
  64. #endif
  65. inode->i_op = &cifs_dir_inode_ops;
  66. inode->i_fop = &cifs_dir_ops;
  67. }
  68. break;
  69. case S_IFLNK:
  70. inode->i_op = &cifs_symlink_inode_ops;
  71. break;
  72. default:
  73. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  74. break;
  75. }
  76. }
  77. /* check inode attributes against fattr. If they don't match, tag the
  78. * inode for cache invalidation
  79. */
  80. static void
  81. cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
  82. {
  83. struct cifs_fscache_inode_coherency_data cd;
  84. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  85. cifs_dbg(FYI, "%s: revalidating inode %llu\n",
  86. __func__, cifs_i->uniqueid);
  87. if (inode->i_state & I_NEW) {
  88. cifs_dbg(FYI, "%s: inode %llu is new\n",
  89. __func__, cifs_i->uniqueid);
  90. return;
  91. }
  92. /* don't bother with revalidation if we have an oplock */
  93. if (CIFS_CACHE_READ(cifs_i)) {
  94. cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
  95. __func__, cifs_i->uniqueid);
  96. return;
  97. }
  98. /* revalidate if mtime or size have changed */
  99. fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
  100. if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
  101. cifs_i->server_eof == fattr->cf_eof) {
  102. cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
  103. __func__, cifs_i->uniqueid);
  104. return;
  105. }
  106. cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
  107. __func__, cifs_i->uniqueid);
  108. set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
  109. /* Invalidate fscache cookie */
  110. cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
  111. fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
  112. }
  113. /*
  114. * copy nlink to the inode, unless it wasn't provided. Provide
  115. * sane values if we don't have an existing one and none was provided
  116. */
  117. static void
  118. cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  119. {
  120. /*
  121. * if we're in a situation where we can't trust what we
  122. * got from the server (readdir, some non-unix cases)
  123. * fake reasonable values
  124. */
  125. if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
  126. /* only provide fake values on a new inode */
  127. if (inode->i_state & I_NEW) {
  128. if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
  129. set_nlink(inode, 2);
  130. else
  131. set_nlink(inode, 1);
  132. }
  133. return;
  134. }
  135. /* we trust the server, so update it */
  136. set_nlink(inode, fattr->cf_nlink);
  137. }
  138. /* populate an inode with info from a cifs_fattr struct */
  139. int
  140. cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  141. {
  142. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  143. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  144. if (!(inode->i_state & I_NEW) &&
  145. unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
  146. CIFS_I(inode)->time = 0; /* force reval */
  147. return -ESTALE;
  148. }
  149. cifs_revalidate_cache(inode, fattr);
  150. spin_lock(&inode->i_lock);
  151. fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
  152. fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
  153. fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
  154. /* we do not want atime to be less than mtime, it broke some apps */
  155. if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
  156. inode->i_atime = fattr->cf_mtime;
  157. else
  158. inode->i_atime = fattr->cf_atime;
  159. inode->i_mtime = fattr->cf_mtime;
  160. inode->i_ctime = fattr->cf_ctime;
  161. inode->i_rdev = fattr->cf_rdev;
  162. cifs_nlink_fattr_to_inode(inode, fattr);
  163. inode->i_uid = fattr->cf_uid;
  164. inode->i_gid = fattr->cf_gid;
  165. /* if dynperm is set, don't clobber existing mode */
  166. if (inode->i_state & I_NEW ||
  167. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
  168. inode->i_mode = fattr->cf_mode;
  169. cifs_i->cifsAttrs = fattr->cf_cifsattrs;
  170. if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
  171. cifs_i->time = 0;
  172. else
  173. cifs_i->time = jiffies;
  174. if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
  175. set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
  176. else
  177. clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
  178. cifs_i->server_eof = fattr->cf_eof;
  179. /*
  180. * Can't safely change the file size here if the client is writing to
  181. * it due to potential races.
  182. */
  183. if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
  184. i_size_write(inode, fattr->cf_eof);
  185. /*
  186. * i_blocks is not related to (i_size / i_blksize),
  187. * but instead 512 byte (2**9) size is required for
  188. * calculating num blocks.
  189. */
  190. inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
  191. }
  192. if (S_ISLNK(fattr->cf_mode)) {
  193. kfree(cifs_i->symlink_target);
  194. cifs_i->symlink_target = fattr->cf_symlink_target;
  195. fattr->cf_symlink_target = NULL;
  196. }
  197. spin_unlock(&inode->i_lock);
  198. if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
  199. inode->i_flags |= S_AUTOMOUNT;
  200. if (inode->i_state & I_NEW)
  201. cifs_set_ops(inode);
  202. return 0;
  203. }
  204. void
  205. cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
  206. {
  207. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  208. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  209. return;
  210. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  211. }
  212. /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
  213. void
  214. cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
  215. struct cifs_sb_info *cifs_sb)
  216. {
  217. memset(fattr, 0, sizeof(*fattr));
  218. fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
  219. fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
  220. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  221. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  222. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
  223. fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
  224. /* old POSIX extensions don't get create time */
  225. fattr->cf_mode = le64_to_cpu(info->Permissions);
  226. /*
  227. * Since we set the inode type below we need to mask off
  228. * to avoid strange results if bits set above.
  229. */
  230. fattr->cf_mode &= ~S_IFMT;
  231. switch (le32_to_cpu(info->Type)) {
  232. case UNIX_FILE:
  233. fattr->cf_mode |= S_IFREG;
  234. fattr->cf_dtype = DT_REG;
  235. break;
  236. case UNIX_SYMLINK:
  237. fattr->cf_mode |= S_IFLNK;
  238. fattr->cf_dtype = DT_LNK;
  239. break;
  240. case UNIX_DIR:
  241. fattr->cf_mode |= S_IFDIR;
  242. fattr->cf_dtype = DT_DIR;
  243. break;
  244. case UNIX_CHARDEV:
  245. fattr->cf_mode |= S_IFCHR;
  246. fattr->cf_dtype = DT_CHR;
  247. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  248. le64_to_cpu(info->DevMinor) & MINORMASK);
  249. break;
  250. case UNIX_BLOCKDEV:
  251. fattr->cf_mode |= S_IFBLK;
  252. fattr->cf_dtype = DT_BLK;
  253. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  254. le64_to_cpu(info->DevMinor) & MINORMASK);
  255. break;
  256. case UNIX_FIFO:
  257. fattr->cf_mode |= S_IFIFO;
  258. fattr->cf_dtype = DT_FIFO;
  259. break;
  260. case UNIX_SOCKET:
  261. fattr->cf_mode |= S_IFSOCK;
  262. fattr->cf_dtype = DT_SOCK;
  263. break;
  264. default:
  265. /* safest to call it a file if we do not know */
  266. fattr->cf_mode |= S_IFREG;
  267. fattr->cf_dtype = DT_REG;
  268. cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
  269. break;
  270. }
  271. fattr->cf_uid = cifs_sb->ctx->linux_uid;
  272. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
  273. u64 id = le64_to_cpu(info->Uid);
  274. if (id < ((uid_t)-1)) {
  275. kuid_t uid = make_kuid(&init_user_ns, id);
  276. if (uid_valid(uid))
  277. fattr->cf_uid = uid;
  278. }
  279. }
  280. fattr->cf_gid = cifs_sb->ctx->linux_gid;
  281. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
  282. u64 id = le64_to_cpu(info->Gid);
  283. if (id < ((gid_t)-1)) {
  284. kgid_t gid = make_kgid(&init_user_ns, id);
  285. if (gid_valid(gid))
  286. fattr->cf_gid = gid;
  287. }
  288. }
  289. fattr->cf_nlink = le64_to_cpu(info->Nlinks);
  290. }
  291. /*
  292. * Fill a cifs_fattr struct with fake inode info.
  293. *
  294. * Needed to setup cifs_fattr data for the directory which is the
  295. * junction to the new submount (ie to setup the fake directory
  296. * which represents a DFS referral).
  297. */
  298. static void
  299. cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
  300. {
  301. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  302. cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
  303. memset(fattr, 0, sizeof(*fattr));
  304. fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
  305. fattr->cf_uid = cifs_sb->ctx->linux_uid;
  306. fattr->cf_gid = cifs_sb->ctx->linux_gid;
  307. ktime_get_coarse_real_ts64(&fattr->cf_mtime);
  308. fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
  309. fattr->cf_nlink = 2;
  310. fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
  311. }
  312. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  313. static int
  314. cifs_get_file_info_unix(struct file *filp)
  315. {
  316. int rc;
  317. unsigned int xid;
  318. FILE_UNIX_BASIC_INFO find_data;
  319. struct cifs_fattr fattr = {};
  320. struct inode *inode = file_inode(filp);
  321. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  322. struct cifsFileInfo *cfile = filp->private_data;
  323. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  324. xid = get_xid();
  325. if (cfile->symlink_target) {
  326. fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
  327. if (!fattr.cf_symlink_target) {
  328. rc = -ENOMEM;
  329. goto cifs_gfiunix_out;
  330. }
  331. }
  332. rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
  333. if (!rc) {
  334. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  335. } else if (rc == -EREMOTE) {
  336. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  337. rc = 0;
  338. } else
  339. goto cifs_gfiunix_out;
  340. rc = cifs_fattr_to_inode(inode, &fattr);
  341. cifs_gfiunix_out:
  342. free_xid(xid);
  343. return rc;
  344. }
  345. int cifs_get_inode_info_unix(struct inode **pinode,
  346. const unsigned char *full_path,
  347. struct super_block *sb, unsigned int xid)
  348. {
  349. int rc;
  350. FILE_UNIX_BASIC_INFO find_data;
  351. struct cifs_fattr fattr;
  352. struct cifs_tcon *tcon;
  353. struct TCP_Server_Info *server;
  354. struct tcon_link *tlink;
  355. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  356. cifs_dbg(FYI, "Getting info on %s\n", full_path);
  357. tlink = cifs_sb_tlink(cifs_sb);
  358. if (IS_ERR(tlink))
  359. return PTR_ERR(tlink);
  360. tcon = tlink_tcon(tlink);
  361. server = tcon->ses->server;
  362. /* could have done a find first instead but this returns more info */
  363. rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
  364. cifs_sb->local_nls, cifs_remap(cifs_sb));
  365. cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
  366. cifs_put_tlink(tlink);
  367. if (!rc) {
  368. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  369. } else if (rc == -EREMOTE) {
  370. cifs_create_dfs_fattr(&fattr, sb);
  371. rc = 0;
  372. } else {
  373. return rc;
  374. }
  375. /* check for Minshall+French symlinks */
  376. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  377. int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  378. full_path);
  379. if (tmprc)
  380. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  381. }
  382. if (S_ISLNK(fattr.cf_mode) && !fattr.cf_symlink_target) {
  383. if (!server->ops->query_symlink)
  384. return -EOPNOTSUPP;
  385. rc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
  386. &fattr.cf_symlink_target, false);
  387. if (rc) {
  388. cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
  389. goto cgiiu_exit;
  390. }
  391. }
  392. if (*pinode == NULL) {
  393. /* get new inode */
  394. cifs_fill_uniqueid(sb, &fattr);
  395. *pinode = cifs_iget(sb, &fattr);
  396. if (!*pinode)
  397. rc = -ENOMEM;
  398. } else {
  399. /* we already have inode, update it */
  400. /* if uniqueid is different, return error */
  401. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  402. CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
  403. CIFS_I(*pinode)->time = 0; /* force reval */
  404. rc = -ESTALE;
  405. goto cgiiu_exit;
  406. }
  407. /* if filetype is different, return error */
  408. rc = cifs_fattr_to_inode(*pinode, &fattr);
  409. }
  410. cgiiu_exit:
  411. kfree(fattr.cf_symlink_target);
  412. return rc;
  413. }
  414. #else
  415. int cifs_get_inode_info_unix(struct inode **pinode,
  416. const unsigned char *full_path,
  417. struct super_block *sb, unsigned int xid)
  418. {
  419. return -EOPNOTSUPP;
  420. }
  421. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  422. static int
  423. cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
  424. struct cifs_sb_info *cifs_sb, unsigned int xid)
  425. {
  426. int rc;
  427. __u32 oplock;
  428. struct tcon_link *tlink;
  429. struct cifs_tcon *tcon;
  430. struct cifs_fid fid;
  431. struct cifs_open_parms oparms;
  432. struct cifs_io_parms io_parms = {0};
  433. char buf[24];
  434. unsigned int bytes_read;
  435. char *pbuf;
  436. int buf_type = CIFS_NO_BUFFER;
  437. pbuf = buf;
  438. fattr->cf_mode &= ~S_IFMT;
  439. if (fattr->cf_eof == 0) {
  440. fattr->cf_mode |= S_IFIFO;
  441. fattr->cf_dtype = DT_FIFO;
  442. return 0;
  443. } else if (fattr->cf_eof < 8) {
  444. fattr->cf_mode |= S_IFREG;
  445. fattr->cf_dtype = DT_REG;
  446. return -EINVAL; /* EOPNOTSUPP? */
  447. }
  448. tlink = cifs_sb_tlink(cifs_sb);
  449. if (IS_ERR(tlink))
  450. return PTR_ERR(tlink);
  451. tcon = tlink_tcon(tlink);
  452. oparms = (struct cifs_open_parms) {
  453. .tcon = tcon,
  454. .cifs_sb = cifs_sb,
  455. .desired_access = GENERIC_READ,
  456. .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
  457. .disposition = FILE_OPEN,
  458. .path = path,
  459. .fid = &fid,
  460. };
  461. if (tcon->ses->server->oplocks)
  462. oplock = REQ_OPLOCK;
  463. else
  464. oplock = 0;
  465. rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
  466. if (rc) {
  467. cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
  468. cifs_put_tlink(tlink);
  469. return rc;
  470. }
  471. /* Read header */
  472. io_parms.netfid = fid.netfid;
  473. io_parms.pid = current->tgid;
  474. io_parms.tcon = tcon;
  475. io_parms.offset = 0;
  476. io_parms.length = 24;
  477. rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
  478. &bytes_read, &pbuf, &buf_type);
  479. if ((rc == 0) && (bytes_read >= 8)) {
  480. if (memcmp("IntxBLK", pbuf, 8) == 0) {
  481. cifs_dbg(FYI, "Block device\n");
  482. fattr->cf_mode |= S_IFBLK;
  483. fattr->cf_dtype = DT_BLK;
  484. if (bytes_read == 24) {
  485. /* we have enough to decode dev num */
  486. __u64 mjr; /* major */
  487. __u64 mnr; /* minor */
  488. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  489. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  490. fattr->cf_rdev = MKDEV(mjr, mnr);
  491. }
  492. } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
  493. cifs_dbg(FYI, "Char device\n");
  494. fattr->cf_mode |= S_IFCHR;
  495. fattr->cf_dtype = DT_CHR;
  496. if (bytes_read == 24) {
  497. /* we have enough to decode dev num */
  498. __u64 mjr; /* major */
  499. __u64 mnr; /* minor */
  500. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  501. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  502. fattr->cf_rdev = MKDEV(mjr, mnr);
  503. }
  504. } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
  505. cifs_dbg(FYI, "Symlink\n");
  506. fattr->cf_mode |= S_IFLNK;
  507. fattr->cf_dtype = DT_LNK;
  508. } else if (memcmp("LnxFIFO", pbuf, 8) == 0) {
  509. cifs_dbg(FYI, "FIFO\n");
  510. fattr->cf_mode |= S_IFIFO;
  511. fattr->cf_dtype = DT_FIFO;
  512. } else {
  513. fattr->cf_mode |= S_IFREG; /* file? */
  514. fattr->cf_dtype = DT_REG;
  515. rc = -EOPNOTSUPP;
  516. }
  517. } else {
  518. fattr->cf_mode |= S_IFREG; /* then it is a file */
  519. fattr->cf_dtype = DT_REG;
  520. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  521. }
  522. tcon->ses->server->ops->close(xid, tcon, &fid);
  523. cifs_put_tlink(tlink);
  524. return rc;
  525. }
  526. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  527. /*
  528. * Fetch mode bits as provided by SFU.
  529. *
  530. * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
  531. */
  532. static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
  533. struct cifs_sb_info *cifs_sb, unsigned int xid)
  534. {
  535. #ifdef CONFIG_CIFS_XATTR
  536. ssize_t rc;
  537. char ea_value[4];
  538. __u32 mode;
  539. struct tcon_link *tlink;
  540. struct cifs_tcon *tcon;
  541. tlink = cifs_sb_tlink(cifs_sb);
  542. if (IS_ERR(tlink))
  543. return PTR_ERR(tlink);
  544. tcon = tlink_tcon(tlink);
  545. if (tcon->ses->server->ops->query_all_EAs == NULL) {
  546. cifs_put_tlink(tlink);
  547. return -EOPNOTSUPP;
  548. }
  549. rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
  550. "SETFILEBITS", ea_value, 4 /* size of buf */,
  551. cifs_sb);
  552. cifs_put_tlink(tlink);
  553. if (rc < 0)
  554. return (int)rc;
  555. else if (rc > 3) {
  556. mode = le32_to_cpu(*((__le32 *)ea_value));
  557. fattr->cf_mode &= ~SFBITS_MASK;
  558. cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
  559. mode, fattr->cf_mode);
  560. fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
  561. cifs_dbg(FYI, "special mode bits 0%o\n", mode);
  562. }
  563. return 0;
  564. #else
  565. return -EOPNOTSUPP;
  566. #endif
  567. }
  568. /* Fill a cifs_fattr struct with info from POSIX info struct */
  569. static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct cifs_open_info_data *data,
  570. struct super_block *sb, bool adjust_tz, bool symlink)
  571. {
  572. struct smb311_posix_qinfo *info = &data->posix_fi;
  573. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  574. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  575. memset(fattr, 0, sizeof(*fattr));
  576. /* no fattr->flags to set */
  577. fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
  578. fattr->cf_uniqueid = le64_to_cpu(info->Inode);
  579. if (info->LastAccessTime)
  580. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  581. else
  582. ktime_get_coarse_real_ts64(&fattr->cf_atime);
  583. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  584. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  585. if (adjust_tz) {
  586. fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
  587. fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
  588. }
  589. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  590. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  591. fattr->cf_createtime = le64_to_cpu(info->CreationTime);
  592. fattr->cf_nlink = le32_to_cpu(info->HardLinks);
  593. fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
  594. /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
  595. /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
  596. if (symlink) {
  597. fattr->cf_mode |= S_IFLNK;
  598. fattr->cf_dtype = DT_LNK;
  599. fattr->cf_symlink_target = data->symlink_target;
  600. data->symlink_target = NULL;
  601. } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  602. fattr->cf_mode |= S_IFDIR;
  603. fattr->cf_dtype = DT_DIR;
  604. } else { /* file */
  605. fattr->cf_mode |= S_IFREG;
  606. fattr->cf_dtype = DT_REG;
  607. }
  608. /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
  609. fattr->cf_uid = cifs_sb->ctx->linux_uid; /* TODO: map uid and gid from SID */
  610. fattr->cf_gid = cifs_sb->ctx->linux_gid;
  611. cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
  612. fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
  613. }
  614. static void cifs_open_info_to_fattr(struct cifs_fattr *fattr, struct cifs_open_info_data *data,
  615. struct super_block *sb, bool adjust_tz, bool symlink,
  616. u32 reparse_tag)
  617. {
  618. struct smb2_file_all_info *info = &data->fi;
  619. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  620. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  621. memset(fattr, 0, sizeof(*fattr));
  622. fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
  623. if (info->DeletePending)
  624. fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
  625. if (info->LastAccessTime)
  626. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  627. else
  628. ktime_get_coarse_real_ts64(&fattr->cf_atime);
  629. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  630. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  631. if (adjust_tz) {
  632. fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
  633. fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
  634. }
  635. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  636. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  637. fattr->cf_createtime = le64_to_cpu(info->CreationTime);
  638. fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
  639. if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
  640. fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
  641. fattr->cf_dtype = DT_LNK;
  642. } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
  643. fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
  644. fattr->cf_dtype = DT_FIFO;
  645. } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
  646. fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
  647. fattr->cf_dtype = DT_SOCK;
  648. } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
  649. fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
  650. fattr->cf_dtype = DT_CHR;
  651. } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
  652. fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
  653. fattr->cf_dtype = DT_BLK;
  654. } else if (symlink || reparse_tag == IO_REPARSE_TAG_SYMLINK ||
  655. reparse_tag == IO_REPARSE_TAG_NFS) {
  656. fattr->cf_mode = S_IFLNK;
  657. fattr->cf_dtype = DT_LNK;
  658. } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  659. fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
  660. fattr->cf_dtype = DT_DIR;
  661. /*
  662. * Server can return wrong NumberOfLinks value for directories
  663. * when Unix extensions are disabled - fake it.
  664. */
  665. if (!tcon->unix_ext)
  666. fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
  667. } else {
  668. fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
  669. fattr->cf_dtype = DT_REG;
  670. /* clear write bits if ATTR_READONLY is set */
  671. if (fattr->cf_cifsattrs & ATTR_READONLY)
  672. fattr->cf_mode &= ~(S_IWUGO);
  673. /*
  674. * Don't accept zero nlink from non-unix servers unless
  675. * delete is pending. Instead mark it as unknown.
  676. */
  677. if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
  678. !info->DeletePending) {
  679. cifs_dbg(VFS, "bogus file nlink value %u\n",
  680. fattr->cf_nlink);
  681. fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
  682. }
  683. }
  684. if (S_ISLNK(fattr->cf_mode)) {
  685. if (likely(data->symlink_target))
  686. fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
  687. fattr->cf_symlink_target = data->symlink_target;
  688. data->symlink_target = NULL;
  689. }
  690. fattr->cf_uid = cifs_sb->ctx->linux_uid;
  691. fattr->cf_gid = cifs_sb->ctx->linux_gid;
  692. }
  693. static int
  694. cifs_get_file_info(struct file *filp)
  695. {
  696. int rc;
  697. unsigned int xid;
  698. struct cifs_open_info_data data = {};
  699. struct cifs_fattr fattr;
  700. struct inode *inode = file_inode(filp);
  701. struct cifsFileInfo *cfile = filp->private_data;
  702. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  703. struct TCP_Server_Info *server = tcon->ses->server;
  704. bool symlink = false;
  705. u32 tag = 0;
  706. if (!server->ops->query_file_info)
  707. return -ENOSYS;
  708. xid = get_xid();
  709. rc = server->ops->query_file_info(xid, tcon, cfile, &data);
  710. switch (rc) {
  711. case 0:
  712. /* TODO: add support to query reparse tag */
  713. if (data.symlink_target) {
  714. symlink = true;
  715. tag = IO_REPARSE_TAG_SYMLINK;
  716. }
  717. cifs_open_info_to_fattr(&fattr, &data, inode->i_sb, false, symlink, tag);
  718. break;
  719. case -EREMOTE:
  720. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  721. rc = 0;
  722. break;
  723. case -EOPNOTSUPP:
  724. case -EINVAL:
  725. /*
  726. * FIXME: legacy server -- fall back to path-based call?
  727. * for now, just skip revalidating and mark inode for
  728. * immediate reval.
  729. */
  730. rc = 0;
  731. CIFS_I(inode)->time = 0;
  732. goto cgfi_exit;
  733. default:
  734. goto cgfi_exit;
  735. }
  736. /*
  737. * don't bother with SFU junk here -- just mark inode as needing
  738. * revalidation.
  739. */
  740. fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
  741. fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
  742. /* if filetype is different, return error */
  743. rc = cifs_fattr_to_inode(inode, &fattr);
  744. cgfi_exit:
  745. cifs_free_open_info(&data);
  746. free_xid(xid);
  747. return rc;
  748. }
  749. /* Simple function to return a 64 bit hash of string. Rarely called */
  750. static __u64 simple_hashstr(const char *str)
  751. {
  752. const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
  753. __u64 hash = 0;
  754. while (*str)
  755. hash = (hash + (__u64) *str++) * hash_mult;
  756. return hash;
  757. }
  758. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  759. /**
  760. * cifs_backup_query_path_info - SMB1 fallback code to get ino
  761. *
  762. * Fallback code to get file metadata when we don't have access to
  763. * full_path (EACCES) and have backup creds.
  764. *
  765. * @xid: transaction id used to identify original request in logs
  766. * @tcon: information about the server share we have mounted
  767. * @sb: the superblock stores info such as disk space available
  768. * @full_path: name of the file we are getting the metadata for
  769. * @resp_buf: will be set to cifs resp buf and needs to be freed with
  770. * cifs_buf_release() when done with @data
  771. * @data: will be set to search info result buffer
  772. */
  773. static int
  774. cifs_backup_query_path_info(int xid,
  775. struct cifs_tcon *tcon,
  776. struct super_block *sb,
  777. const char *full_path,
  778. void **resp_buf,
  779. FILE_ALL_INFO **data)
  780. {
  781. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  782. struct cifs_search_info info = {0};
  783. u16 flags;
  784. int rc;
  785. *resp_buf = NULL;
  786. info.endOfSearch = false;
  787. if (tcon->unix_ext)
  788. info.info_level = SMB_FIND_FILE_UNIX;
  789. else if ((tcon->ses->capabilities &
  790. tcon->ses->server->vals->cap_nt_find) == 0)
  791. info.info_level = SMB_FIND_FILE_INFO_STANDARD;
  792. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  793. info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  794. else /* no srvino useful for fallback to some netapp */
  795. info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
  796. flags = CIFS_SEARCH_CLOSE_ALWAYS |
  797. CIFS_SEARCH_CLOSE_AT_END |
  798. CIFS_SEARCH_BACKUP_SEARCH;
  799. rc = CIFSFindFirst(xid, tcon, full_path,
  800. cifs_sb, NULL, flags, &info, false);
  801. if (rc)
  802. return rc;
  803. *resp_buf = (void *)info.ntwrk_buf_start;
  804. *data = (FILE_ALL_INFO *)info.srch_entries_start;
  805. return 0;
  806. }
  807. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  808. static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
  809. struct inode **inode, const char *full_path,
  810. struct cifs_open_info_data *data, struct cifs_fattr *fattr)
  811. {
  812. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  813. struct TCP_Server_Info *server = tcon->ses->server;
  814. int rc;
  815. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
  816. if (*inode)
  817. fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
  818. else
  819. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  820. return;
  821. }
  822. /*
  823. * If we have an inode pass a NULL tcon to ensure we don't
  824. * make a round trip to the server. This only works for SMB2+.
  825. */
  826. rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
  827. &fattr->cf_uniqueid, data);
  828. if (rc) {
  829. /*
  830. * If that fails reuse existing ino or generate one
  831. * and disable server ones
  832. */
  833. if (*inode)
  834. fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
  835. else {
  836. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  837. cifs_autodisable_serverino(cifs_sb);
  838. }
  839. return;
  840. }
  841. /* If no errors, check for zero root inode (invalid) */
  842. if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
  843. cifs_dbg(FYI, "Invalid (0) inodenum\n");
  844. if (*inode) {
  845. /* reuse */
  846. fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
  847. } else {
  848. /* make an ino by hashing the UNC */
  849. fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
  850. fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
  851. }
  852. }
  853. }
  854. static inline bool is_inode_cache_good(struct inode *ino)
  855. {
  856. return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
  857. }
  858. int cifs_get_inode_info(struct inode **inode, const char *full_path,
  859. struct cifs_open_info_data *data, struct super_block *sb, int xid,
  860. const struct cifs_fid *fid)
  861. {
  862. struct cifs_tcon *tcon;
  863. struct TCP_Server_Info *server;
  864. struct tcon_link *tlink;
  865. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  866. bool adjust_tz = false;
  867. struct cifs_fattr fattr = {0};
  868. bool is_reparse_point = false;
  869. struct cifs_open_info_data tmp_data = {};
  870. void *smb1_backup_rsp_buf = NULL;
  871. int rc = 0;
  872. int tmprc = 0;
  873. __u32 reparse_tag = 0;
  874. tlink = cifs_sb_tlink(cifs_sb);
  875. if (IS_ERR(tlink))
  876. return PTR_ERR(tlink);
  877. tcon = tlink_tcon(tlink);
  878. server = tcon->ses->server;
  879. /*
  880. * 1. Fetch file metadata if not provided (data)
  881. */
  882. if (!data) {
  883. if (is_inode_cache_good(*inode)) {
  884. cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
  885. goto out;
  886. }
  887. rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path, &tmp_data,
  888. &adjust_tz, &is_reparse_point);
  889. data = &tmp_data;
  890. }
  891. /*
  892. * 2. Convert it to internal cifs metadata (fattr)
  893. */
  894. switch (rc) {
  895. case 0:
  896. /*
  897. * If the file is a reparse point, it is more complicated
  898. * since we have to check if its reparse tag matches a known
  899. * special file type e.g. symlink or fifo or char etc.
  900. */
  901. if (is_reparse_point && data->symlink_target) {
  902. reparse_tag = IO_REPARSE_TAG_SYMLINK;
  903. } else if ((le32_to_cpu(data->fi.Attributes) & ATTR_REPARSE) &&
  904. server->ops->query_reparse_tag) {
  905. tmprc = server->ops->query_reparse_tag(xid, tcon, cifs_sb, full_path,
  906. &reparse_tag);
  907. if (tmprc)
  908. cifs_dbg(FYI, "%s: query_reparse_tag: rc = %d\n", __func__, tmprc);
  909. if (server->ops->query_symlink) {
  910. tmprc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
  911. &data->symlink_target,
  912. is_reparse_point);
  913. if (tmprc)
  914. cifs_dbg(FYI, "%s: query_symlink: rc = %d\n", __func__,
  915. tmprc);
  916. }
  917. }
  918. cifs_open_info_to_fattr(&fattr, data, sb, adjust_tz, is_reparse_point, reparse_tag);
  919. break;
  920. case -EREMOTE:
  921. /* DFS link, no metadata available on this server */
  922. cifs_create_dfs_fattr(&fattr, sb);
  923. rc = 0;
  924. break;
  925. case -EACCES:
  926. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  927. /*
  928. * perm errors, try again with backup flags if possible
  929. *
  930. * For SMB2 and later the backup intent flag
  931. * is already sent if needed on open and there
  932. * is no path based FindFirst operation to use
  933. * to retry with
  934. */
  935. if (backup_cred(cifs_sb) && is_smb1_server(server)) {
  936. /* for easier reading */
  937. FILE_ALL_INFO *fi;
  938. FILE_DIRECTORY_INFO *fdi;
  939. SEARCH_ID_FULL_DIR_INFO *si;
  940. rc = cifs_backup_query_path_info(xid, tcon, sb,
  941. full_path,
  942. &smb1_backup_rsp_buf,
  943. &fi);
  944. if (rc)
  945. goto out;
  946. move_cifs_info_to_smb2(&data->fi, fi);
  947. fdi = (FILE_DIRECTORY_INFO *)fi;
  948. si = (SEARCH_ID_FULL_DIR_INFO *)fi;
  949. cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
  950. fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
  951. /* uniqueid set, skip get inum step */
  952. goto handle_mnt_opt;
  953. } else {
  954. /* nothing we can do, bail out */
  955. goto out;
  956. }
  957. #else
  958. goto out;
  959. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  960. break;
  961. default:
  962. cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
  963. goto out;
  964. }
  965. /*
  966. * 3. Get or update inode number (fattr.cf_uniqueid)
  967. */
  968. cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
  969. /*
  970. * 4. Tweak fattr based on mount options
  971. */
  972. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  973. handle_mnt_opt:
  974. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  975. /* query for SFU type info if supported and needed */
  976. if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
  977. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  978. tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
  979. if (tmprc)
  980. cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
  981. }
  982. /* fill in 0777 bits from ACL */
  983. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
  984. rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
  985. full_path, fid);
  986. if (rc == -EREMOTE)
  987. rc = 0;
  988. if (rc) {
  989. cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
  990. __func__, rc);
  991. goto out;
  992. }
  993. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  994. rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
  995. full_path, fid);
  996. if (rc == -EREMOTE)
  997. rc = 0;
  998. if (rc) {
  999. cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
  1000. __func__, rc);
  1001. goto out;
  1002. }
  1003. }
  1004. /* fill in remaining high mode bits e.g. SUID, VTX */
  1005. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  1006. cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
  1007. /* check for Minshall+French symlinks */
  1008. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  1009. tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  1010. full_path);
  1011. if (tmprc)
  1012. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  1013. }
  1014. /*
  1015. * 5. Update inode with final fattr data
  1016. */
  1017. if (!*inode) {
  1018. *inode = cifs_iget(sb, &fattr);
  1019. if (!*inode)
  1020. rc = -ENOMEM;
  1021. } else {
  1022. /* we already have inode, update it */
  1023. /* if uniqueid is different, return error */
  1024. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  1025. CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
  1026. CIFS_I(*inode)->time = 0; /* force reval */
  1027. rc = -ESTALE;
  1028. goto out;
  1029. }
  1030. /* if filetype is different, return error */
  1031. rc = cifs_fattr_to_inode(*inode, &fattr);
  1032. }
  1033. out:
  1034. cifs_buf_release(smb1_backup_rsp_buf);
  1035. cifs_put_tlink(tlink);
  1036. cifs_free_open_info(&tmp_data);
  1037. kfree(fattr.cf_symlink_target);
  1038. return rc;
  1039. }
  1040. int
  1041. smb311_posix_get_inode_info(struct inode **inode,
  1042. const char *full_path,
  1043. struct super_block *sb, unsigned int xid)
  1044. {
  1045. struct cifs_tcon *tcon;
  1046. struct tcon_link *tlink;
  1047. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  1048. bool adjust_tz = false;
  1049. struct cifs_fattr fattr = {0};
  1050. bool symlink = false;
  1051. struct cifs_open_info_data data = {};
  1052. int rc = 0;
  1053. int tmprc = 0;
  1054. tlink = cifs_sb_tlink(cifs_sb);
  1055. if (IS_ERR(tlink))
  1056. return PTR_ERR(tlink);
  1057. tcon = tlink_tcon(tlink);
  1058. /*
  1059. * 1. Fetch file metadata
  1060. */
  1061. if (is_inode_cache_good(*inode)) {
  1062. cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
  1063. goto out;
  1064. }
  1065. rc = smb311_posix_query_path_info(xid, tcon, cifs_sb, full_path, &data, &adjust_tz,
  1066. &symlink);
  1067. /*
  1068. * 2. Convert it to internal cifs metadata (fattr)
  1069. */
  1070. switch (rc) {
  1071. case 0:
  1072. smb311_posix_info_to_fattr(&fattr, &data, sb, adjust_tz, symlink);
  1073. break;
  1074. case -EREMOTE:
  1075. /* DFS link, no metadata available on this server */
  1076. cifs_create_dfs_fattr(&fattr, sb);
  1077. rc = 0;
  1078. break;
  1079. case -EACCES:
  1080. /*
  1081. * For SMB2 and later the backup intent flag
  1082. * is already sent if needed on open and there
  1083. * is no path based FindFirst operation to use
  1084. * to retry with so nothing we can do, bail out
  1085. */
  1086. goto out;
  1087. default:
  1088. cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
  1089. goto out;
  1090. }
  1091. /*
  1092. * 3. Tweak fattr based on mount options
  1093. */
  1094. /* check for Minshall+French symlinks */
  1095. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  1096. tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  1097. full_path);
  1098. if (tmprc)
  1099. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  1100. }
  1101. /*
  1102. * 4. Update inode with final fattr data
  1103. */
  1104. if (!*inode) {
  1105. *inode = cifs_iget(sb, &fattr);
  1106. if (!*inode)
  1107. rc = -ENOMEM;
  1108. } else {
  1109. /* we already have inode, update it */
  1110. /* if uniqueid is different, return error */
  1111. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  1112. CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
  1113. CIFS_I(*inode)->time = 0; /* force reval */
  1114. rc = -ESTALE;
  1115. goto out;
  1116. }
  1117. /* if filetype is different, return error */
  1118. rc = cifs_fattr_to_inode(*inode, &fattr);
  1119. }
  1120. out:
  1121. cifs_put_tlink(tlink);
  1122. cifs_free_open_info(&data);
  1123. kfree(fattr.cf_symlink_target);
  1124. return rc;
  1125. }
  1126. static const struct inode_operations cifs_ipc_inode_ops = {
  1127. .lookup = cifs_lookup,
  1128. };
  1129. static int
  1130. cifs_find_inode(struct inode *inode, void *opaque)
  1131. {
  1132. struct cifs_fattr *fattr = opaque;
  1133. /* don't match inode with different uniqueid */
  1134. if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
  1135. return 0;
  1136. /* use createtime like an i_generation field */
  1137. if (CIFS_I(inode)->createtime != fattr->cf_createtime)
  1138. return 0;
  1139. /* don't match inode of different type */
  1140. if (inode_wrong_type(inode, fattr->cf_mode))
  1141. return 0;
  1142. /* if it's not a directory or has no dentries, then flag it */
  1143. if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
  1144. fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
  1145. return 1;
  1146. }
  1147. static int
  1148. cifs_init_inode(struct inode *inode, void *opaque)
  1149. {
  1150. struct cifs_fattr *fattr = opaque;
  1151. CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
  1152. CIFS_I(inode)->createtime = fattr->cf_createtime;
  1153. return 0;
  1154. }
  1155. /*
  1156. * walk dentry list for an inode and report whether it has aliases that
  1157. * are hashed. We use this to determine if a directory inode can actually
  1158. * be used.
  1159. */
  1160. static bool
  1161. inode_has_hashed_dentries(struct inode *inode)
  1162. {
  1163. struct dentry *dentry;
  1164. spin_lock(&inode->i_lock);
  1165. hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
  1166. if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
  1167. spin_unlock(&inode->i_lock);
  1168. return true;
  1169. }
  1170. }
  1171. spin_unlock(&inode->i_lock);
  1172. return false;
  1173. }
  1174. /* Given fattrs, get a corresponding inode */
  1175. struct inode *
  1176. cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
  1177. {
  1178. unsigned long hash;
  1179. struct inode *inode;
  1180. retry_iget5_locked:
  1181. cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
  1182. /* hash down to 32-bits on 32-bit arch */
  1183. hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
  1184. inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
  1185. if (inode) {
  1186. /* was there a potentially problematic inode collision? */
  1187. if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
  1188. fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
  1189. if (inode_has_hashed_dentries(inode)) {
  1190. cifs_autodisable_serverino(CIFS_SB(sb));
  1191. iput(inode);
  1192. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  1193. goto retry_iget5_locked;
  1194. }
  1195. }
  1196. /* can't fail - see cifs_find_inode() */
  1197. cifs_fattr_to_inode(inode, fattr);
  1198. if (sb->s_flags & SB_NOATIME)
  1199. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  1200. if (inode->i_state & I_NEW) {
  1201. inode->i_ino = hash;
  1202. cifs_fscache_get_inode_cookie(inode);
  1203. unlock_new_inode(inode);
  1204. }
  1205. }
  1206. return inode;
  1207. }
  1208. /* gets root inode */
  1209. struct inode *cifs_root_iget(struct super_block *sb)
  1210. {
  1211. unsigned int xid;
  1212. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  1213. struct inode *inode = NULL;
  1214. long rc;
  1215. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  1216. char *path = NULL;
  1217. int len;
  1218. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
  1219. && cifs_sb->prepath) {
  1220. len = strlen(cifs_sb->prepath);
  1221. path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
  1222. if (path == NULL)
  1223. return ERR_PTR(-ENOMEM);
  1224. path[0] = '/';
  1225. memcpy(path+1, cifs_sb->prepath, len);
  1226. } else {
  1227. path = kstrdup("", GFP_KERNEL);
  1228. if (path == NULL)
  1229. return ERR_PTR(-ENOMEM);
  1230. }
  1231. xid = get_xid();
  1232. if (tcon->unix_ext) {
  1233. rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
  1234. /* some servers mistakenly claim POSIX support */
  1235. if (rc != -EOPNOTSUPP)
  1236. goto iget_no_retry;
  1237. cifs_dbg(VFS, "server does not support POSIX extensions\n");
  1238. tcon->unix_ext = false;
  1239. }
  1240. convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
  1241. if (tcon->posix_extensions)
  1242. rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
  1243. else
  1244. rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
  1245. iget_no_retry:
  1246. if (!inode) {
  1247. inode = ERR_PTR(rc);
  1248. goto out;
  1249. }
  1250. if (rc && tcon->pipe) {
  1251. cifs_dbg(FYI, "ipc connection - fake read inode\n");
  1252. spin_lock(&inode->i_lock);
  1253. inode->i_mode |= S_IFDIR;
  1254. set_nlink(inode, 2);
  1255. inode->i_op = &cifs_ipc_inode_ops;
  1256. inode->i_fop = &simple_dir_operations;
  1257. inode->i_uid = cifs_sb->ctx->linux_uid;
  1258. inode->i_gid = cifs_sb->ctx->linux_gid;
  1259. spin_unlock(&inode->i_lock);
  1260. } else if (rc) {
  1261. iget_failed(inode);
  1262. inode = ERR_PTR(rc);
  1263. }
  1264. out:
  1265. kfree(path);
  1266. free_xid(xid);
  1267. return inode;
  1268. }
  1269. int
  1270. cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
  1271. const char *full_path, __u32 dosattr)
  1272. {
  1273. bool set_time = false;
  1274. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1275. struct TCP_Server_Info *server;
  1276. FILE_BASIC_INFO info_buf;
  1277. if (attrs == NULL)
  1278. return -EINVAL;
  1279. server = cifs_sb_master_tcon(cifs_sb)->ses->server;
  1280. if (!server->ops->set_file_info)
  1281. return -ENOSYS;
  1282. info_buf.Pad = 0;
  1283. if (attrs->ia_valid & ATTR_ATIME) {
  1284. set_time = true;
  1285. info_buf.LastAccessTime =
  1286. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  1287. } else
  1288. info_buf.LastAccessTime = 0;
  1289. if (attrs->ia_valid & ATTR_MTIME) {
  1290. set_time = true;
  1291. info_buf.LastWriteTime =
  1292. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  1293. } else
  1294. info_buf.LastWriteTime = 0;
  1295. /*
  1296. * Samba throws this field away, but windows may actually use it.
  1297. * Do not set ctime unless other time stamps are changed explicitly
  1298. * (i.e. by utimes()) since we would then have a mix of client and
  1299. * server times.
  1300. */
  1301. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  1302. cifs_dbg(FYI, "CIFS - CTIME changed\n");
  1303. info_buf.ChangeTime =
  1304. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  1305. } else
  1306. info_buf.ChangeTime = 0;
  1307. info_buf.CreationTime = 0; /* don't change */
  1308. info_buf.Attributes = cpu_to_le32(dosattr);
  1309. return server->ops->set_file_info(inode, full_path, &info_buf, xid);
  1310. }
  1311. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1312. /*
  1313. * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
  1314. * and rename it to a random name that hopefully won't conflict with
  1315. * anything else.
  1316. */
  1317. int
  1318. cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
  1319. const unsigned int xid)
  1320. {
  1321. int oplock = 0;
  1322. int rc;
  1323. struct cifs_fid fid;
  1324. struct cifs_open_parms oparms;
  1325. struct inode *inode = d_inode(dentry);
  1326. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1327. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1328. struct tcon_link *tlink;
  1329. struct cifs_tcon *tcon;
  1330. __u32 dosattr, origattr;
  1331. FILE_BASIC_INFO *info_buf = NULL;
  1332. tlink = cifs_sb_tlink(cifs_sb);
  1333. if (IS_ERR(tlink))
  1334. return PTR_ERR(tlink);
  1335. tcon = tlink_tcon(tlink);
  1336. /*
  1337. * We cannot rename the file if the server doesn't support
  1338. * CAP_INFOLEVEL_PASSTHRU
  1339. */
  1340. if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
  1341. rc = -EBUSY;
  1342. goto out;
  1343. }
  1344. oparms = (struct cifs_open_parms) {
  1345. .tcon = tcon,
  1346. .cifs_sb = cifs_sb,
  1347. .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
  1348. .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
  1349. .disposition = FILE_OPEN,
  1350. .path = full_path,
  1351. .fid = &fid,
  1352. };
  1353. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1354. if (rc != 0)
  1355. goto out;
  1356. origattr = cifsInode->cifsAttrs;
  1357. if (origattr == 0)
  1358. origattr |= ATTR_NORMAL;
  1359. dosattr = origattr & ~ATTR_READONLY;
  1360. if (dosattr == 0)
  1361. dosattr |= ATTR_NORMAL;
  1362. dosattr |= ATTR_HIDDEN;
  1363. /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
  1364. if (dosattr != origattr) {
  1365. info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
  1366. if (info_buf == NULL) {
  1367. rc = -ENOMEM;
  1368. goto out_close;
  1369. }
  1370. info_buf->Attributes = cpu_to_le32(dosattr);
  1371. rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
  1372. current->tgid);
  1373. /* although we would like to mark the file hidden
  1374. if that fails we will still try to rename it */
  1375. if (!rc)
  1376. cifsInode->cifsAttrs = dosattr;
  1377. else
  1378. dosattr = origattr; /* since not able to change them */
  1379. }
  1380. /* rename the file */
  1381. rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
  1382. cifs_sb->local_nls,
  1383. cifs_remap(cifs_sb));
  1384. if (rc != 0) {
  1385. rc = -EBUSY;
  1386. goto undo_setattr;
  1387. }
  1388. /* try to set DELETE_ON_CLOSE */
  1389. if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
  1390. rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
  1391. current->tgid);
  1392. /*
  1393. * some samba versions return -ENOENT when we try to set the
  1394. * file disposition here. Likely a samba bug, but work around
  1395. * it for now. This means that some cifsXXX files may hang
  1396. * around after they shouldn't.
  1397. *
  1398. * BB: remove this hack after more servers have the fix
  1399. */
  1400. if (rc == -ENOENT)
  1401. rc = 0;
  1402. else if (rc != 0) {
  1403. rc = -EBUSY;
  1404. goto undo_rename;
  1405. }
  1406. set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
  1407. }
  1408. out_close:
  1409. CIFSSMBClose(xid, tcon, fid.netfid);
  1410. out:
  1411. kfree(info_buf);
  1412. cifs_put_tlink(tlink);
  1413. return rc;
  1414. /*
  1415. * reset everything back to the original state. Don't bother
  1416. * dealing with errors here since we can't do anything about
  1417. * them anyway.
  1418. */
  1419. undo_rename:
  1420. CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
  1421. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1422. undo_setattr:
  1423. if (dosattr != origattr) {
  1424. info_buf->Attributes = cpu_to_le32(origattr);
  1425. if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
  1426. current->tgid))
  1427. cifsInode->cifsAttrs = origattr;
  1428. }
  1429. goto out_close;
  1430. }
  1431. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1432. /* copied from fs/nfs/dir.c with small changes */
  1433. static void
  1434. cifs_drop_nlink(struct inode *inode)
  1435. {
  1436. spin_lock(&inode->i_lock);
  1437. if (inode->i_nlink > 0)
  1438. drop_nlink(inode);
  1439. spin_unlock(&inode->i_lock);
  1440. }
  1441. /*
  1442. * If d_inode(dentry) is null (usually meaning the cached dentry
  1443. * is a negative dentry) then we would attempt a standard SMB delete, but
  1444. * if that fails we can not attempt the fall back mechanisms on EACCES
  1445. * but will return the EACCES to the caller. Note that the VFS does not call
  1446. * unlink on negative dentries currently.
  1447. */
  1448. int cifs_unlink(struct inode *dir, struct dentry *dentry)
  1449. {
  1450. int rc = 0;
  1451. unsigned int xid;
  1452. const char *full_path;
  1453. void *page;
  1454. struct inode *inode = d_inode(dentry);
  1455. struct cifsInodeInfo *cifs_inode;
  1456. struct super_block *sb = dir->i_sb;
  1457. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  1458. struct tcon_link *tlink;
  1459. struct cifs_tcon *tcon;
  1460. struct TCP_Server_Info *server;
  1461. struct iattr *attrs = NULL;
  1462. __u32 dosattr = 0, origattr = 0;
  1463. cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
  1464. if (unlikely(cifs_forced_shutdown(cifs_sb)))
  1465. return -EIO;
  1466. tlink = cifs_sb_tlink(cifs_sb);
  1467. if (IS_ERR(tlink))
  1468. return PTR_ERR(tlink);
  1469. tcon = tlink_tcon(tlink);
  1470. server = tcon->ses->server;
  1471. xid = get_xid();
  1472. page = alloc_dentry_path();
  1473. if (tcon->nodelete) {
  1474. rc = -EACCES;
  1475. goto unlink_out;
  1476. }
  1477. /* Unlink can be called from rename so we can not take the
  1478. * sb->s_vfs_rename_mutex here */
  1479. full_path = build_path_from_dentry(dentry, page);
  1480. if (IS_ERR(full_path)) {
  1481. rc = PTR_ERR(full_path);
  1482. goto unlink_out;
  1483. }
  1484. cifs_close_deferred_file_under_dentry(tcon, full_path);
  1485. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1486. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1487. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1488. rc = CIFSPOSIXDelFile(xid, tcon, full_path,
  1489. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  1490. cifs_remap(cifs_sb));
  1491. cifs_dbg(FYI, "posix del rc %d\n", rc);
  1492. if ((rc == 0) || (rc == -ENOENT))
  1493. goto psx_del_no_retry;
  1494. }
  1495. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1496. retry_std_delete:
  1497. if (!server->ops->unlink) {
  1498. rc = -ENOSYS;
  1499. goto psx_del_no_retry;
  1500. }
  1501. rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
  1502. psx_del_no_retry:
  1503. if (!rc) {
  1504. if (inode)
  1505. cifs_drop_nlink(inode);
  1506. } else if (rc == -ENOENT) {
  1507. d_drop(dentry);
  1508. } else if (rc == -EBUSY) {
  1509. if (server->ops->rename_pending_delete) {
  1510. rc = server->ops->rename_pending_delete(full_path,
  1511. dentry, xid);
  1512. if (rc == 0)
  1513. cifs_drop_nlink(inode);
  1514. }
  1515. } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
  1516. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  1517. if (attrs == NULL) {
  1518. rc = -ENOMEM;
  1519. goto out_reval;
  1520. }
  1521. /* try to reset dos attributes */
  1522. cifs_inode = CIFS_I(inode);
  1523. origattr = cifs_inode->cifsAttrs;
  1524. if (origattr == 0)
  1525. origattr |= ATTR_NORMAL;
  1526. dosattr = origattr & ~ATTR_READONLY;
  1527. if (dosattr == 0)
  1528. dosattr |= ATTR_NORMAL;
  1529. dosattr |= ATTR_HIDDEN;
  1530. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1531. if (rc != 0)
  1532. goto out_reval;
  1533. goto retry_std_delete;
  1534. }
  1535. /* undo the setattr if we errored out and it's needed */
  1536. if (rc != 0 && dosattr != 0)
  1537. cifs_set_file_info(inode, attrs, xid, full_path, origattr);
  1538. out_reval:
  1539. if (inode) {
  1540. cifs_inode = CIFS_I(inode);
  1541. cifs_inode->time = 0; /* will force revalidate to get info
  1542. when needed */
  1543. inode->i_ctime = current_time(inode);
  1544. }
  1545. dir->i_ctime = dir->i_mtime = current_time(dir);
  1546. cifs_inode = CIFS_I(dir);
  1547. CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
  1548. unlink_out:
  1549. free_dentry_path(page);
  1550. kfree(attrs);
  1551. free_xid(xid);
  1552. cifs_put_tlink(tlink);
  1553. return rc;
  1554. }
  1555. static int
  1556. cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
  1557. const char *full_path, struct cifs_sb_info *cifs_sb,
  1558. struct cifs_tcon *tcon, const unsigned int xid)
  1559. {
  1560. int rc = 0;
  1561. struct inode *inode = NULL;
  1562. if (tcon->posix_extensions)
  1563. rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
  1564. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1565. else if (tcon->unix_ext)
  1566. rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
  1567. xid);
  1568. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1569. else
  1570. rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
  1571. xid, NULL);
  1572. if (rc)
  1573. return rc;
  1574. if (!S_ISDIR(inode->i_mode)) {
  1575. /*
  1576. * mkdir succeeded, but another client has managed to remove the
  1577. * sucker and replace it with non-directory. Return success,
  1578. * but don't leave the child in dcache.
  1579. */
  1580. iput(inode);
  1581. d_drop(dentry);
  1582. return 0;
  1583. }
  1584. /*
  1585. * setting nlink not necessary except in cases where we failed to get it
  1586. * from the server or was set bogus. Also, since this is a brand new
  1587. * inode, no need to grab the i_lock before setting the i_nlink.
  1588. */
  1589. if (inode->i_nlink < 2)
  1590. set_nlink(inode, 2);
  1591. mode &= ~current_umask();
  1592. /* must turn on setgid bit if parent dir has it */
  1593. if (parent->i_mode & S_ISGID)
  1594. mode |= S_ISGID;
  1595. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1596. if (tcon->unix_ext) {
  1597. struct cifs_unix_set_info_args args = {
  1598. .mode = mode,
  1599. .ctime = NO_CHANGE_64,
  1600. .atime = NO_CHANGE_64,
  1601. .mtime = NO_CHANGE_64,
  1602. .device = 0,
  1603. };
  1604. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1605. args.uid = current_fsuid();
  1606. if (parent->i_mode & S_ISGID)
  1607. args.gid = parent->i_gid;
  1608. else
  1609. args.gid = current_fsgid();
  1610. } else {
  1611. args.uid = INVALID_UID; /* no change */
  1612. args.gid = INVALID_GID; /* no change */
  1613. }
  1614. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  1615. cifs_sb->local_nls,
  1616. cifs_remap(cifs_sb));
  1617. } else {
  1618. #else
  1619. {
  1620. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1621. struct TCP_Server_Info *server = tcon->ses->server;
  1622. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1623. (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
  1624. server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
  1625. tcon, xid);
  1626. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  1627. inode->i_mode = (mode | S_IFDIR);
  1628. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1629. inode->i_uid = current_fsuid();
  1630. if (inode->i_mode & S_ISGID)
  1631. inode->i_gid = parent->i_gid;
  1632. else
  1633. inode->i_gid = current_fsgid();
  1634. }
  1635. }
  1636. d_instantiate(dentry, inode);
  1637. return 0;
  1638. }
  1639. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1640. static int
  1641. cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
  1642. const char *full_path, struct cifs_sb_info *cifs_sb,
  1643. struct cifs_tcon *tcon, const unsigned int xid)
  1644. {
  1645. int rc = 0;
  1646. u32 oplock = 0;
  1647. FILE_UNIX_BASIC_INFO *info = NULL;
  1648. struct inode *newinode = NULL;
  1649. struct cifs_fattr fattr;
  1650. info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  1651. if (info == NULL) {
  1652. rc = -ENOMEM;
  1653. goto posix_mkdir_out;
  1654. }
  1655. mode &= ~current_umask();
  1656. rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
  1657. NULL /* netfid */, info, &oplock, full_path,
  1658. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1659. if (rc == -EOPNOTSUPP)
  1660. goto posix_mkdir_out;
  1661. else if (rc) {
  1662. cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
  1663. d_drop(dentry);
  1664. goto posix_mkdir_out;
  1665. }
  1666. if (info->Type == cpu_to_le32(-1))
  1667. /* no return info, go query for it */
  1668. goto posix_mkdir_get_info;
  1669. /*
  1670. * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
  1671. * need to set uid/gid.
  1672. */
  1673. cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
  1674. cifs_fill_uniqueid(inode->i_sb, &fattr);
  1675. newinode = cifs_iget(inode->i_sb, &fattr);
  1676. if (!newinode)
  1677. goto posix_mkdir_get_info;
  1678. d_instantiate(dentry, newinode);
  1679. #ifdef CONFIG_CIFS_DEBUG2
  1680. cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
  1681. dentry, dentry, newinode);
  1682. if (newinode->i_nlink != 2)
  1683. cifs_dbg(FYI, "unexpected number of links %d\n",
  1684. newinode->i_nlink);
  1685. #endif
  1686. posix_mkdir_out:
  1687. kfree(info);
  1688. return rc;
  1689. posix_mkdir_get_info:
  1690. rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
  1691. xid);
  1692. goto posix_mkdir_out;
  1693. }
  1694. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1695. int cifs_mkdir(struct user_namespace *mnt_userns, struct inode *inode,
  1696. struct dentry *direntry, umode_t mode)
  1697. {
  1698. int rc = 0;
  1699. unsigned int xid;
  1700. struct cifs_sb_info *cifs_sb;
  1701. struct tcon_link *tlink;
  1702. struct cifs_tcon *tcon;
  1703. struct TCP_Server_Info *server;
  1704. const char *full_path;
  1705. void *page;
  1706. cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
  1707. mode, inode);
  1708. cifs_sb = CIFS_SB(inode->i_sb);
  1709. if (unlikely(cifs_forced_shutdown(cifs_sb)))
  1710. return -EIO;
  1711. tlink = cifs_sb_tlink(cifs_sb);
  1712. if (IS_ERR(tlink))
  1713. return PTR_ERR(tlink);
  1714. tcon = tlink_tcon(tlink);
  1715. xid = get_xid();
  1716. page = alloc_dentry_path();
  1717. full_path = build_path_from_dentry(direntry, page);
  1718. if (IS_ERR(full_path)) {
  1719. rc = PTR_ERR(full_path);
  1720. goto mkdir_out;
  1721. }
  1722. server = tcon->ses->server;
  1723. if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
  1724. rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
  1725. cifs_sb);
  1726. d_drop(direntry); /* for time being always refresh inode info */
  1727. goto mkdir_out;
  1728. }
  1729. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1730. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1731. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1732. rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
  1733. tcon, xid);
  1734. if (rc != -EOPNOTSUPP)
  1735. goto mkdir_out;
  1736. }
  1737. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1738. if (!server->ops->mkdir) {
  1739. rc = -ENOSYS;
  1740. goto mkdir_out;
  1741. }
  1742. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  1743. rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
  1744. if (rc) {
  1745. cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
  1746. d_drop(direntry);
  1747. goto mkdir_out;
  1748. }
  1749. /* TODO: skip this for smb2/smb3 */
  1750. rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
  1751. xid);
  1752. mkdir_out:
  1753. /*
  1754. * Force revalidate to get parent dir info when needed since cached
  1755. * attributes are invalid now.
  1756. */
  1757. CIFS_I(inode)->time = 0;
  1758. free_dentry_path(page);
  1759. free_xid(xid);
  1760. cifs_put_tlink(tlink);
  1761. return rc;
  1762. }
  1763. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  1764. {
  1765. int rc = 0;
  1766. unsigned int xid;
  1767. struct cifs_sb_info *cifs_sb;
  1768. struct tcon_link *tlink;
  1769. struct cifs_tcon *tcon;
  1770. struct TCP_Server_Info *server;
  1771. const char *full_path;
  1772. void *page = alloc_dentry_path();
  1773. struct cifsInodeInfo *cifsInode;
  1774. cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
  1775. xid = get_xid();
  1776. full_path = build_path_from_dentry(direntry, page);
  1777. if (IS_ERR(full_path)) {
  1778. rc = PTR_ERR(full_path);
  1779. goto rmdir_exit;
  1780. }
  1781. cifs_sb = CIFS_SB(inode->i_sb);
  1782. if (unlikely(cifs_forced_shutdown(cifs_sb))) {
  1783. rc = -EIO;
  1784. goto rmdir_exit;
  1785. }
  1786. tlink = cifs_sb_tlink(cifs_sb);
  1787. if (IS_ERR(tlink)) {
  1788. rc = PTR_ERR(tlink);
  1789. goto rmdir_exit;
  1790. }
  1791. tcon = tlink_tcon(tlink);
  1792. server = tcon->ses->server;
  1793. if (!server->ops->rmdir) {
  1794. rc = -ENOSYS;
  1795. cifs_put_tlink(tlink);
  1796. goto rmdir_exit;
  1797. }
  1798. if (tcon->nodelete) {
  1799. rc = -EACCES;
  1800. cifs_put_tlink(tlink);
  1801. goto rmdir_exit;
  1802. }
  1803. rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
  1804. cifs_put_tlink(tlink);
  1805. if (!rc) {
  1806. spin_lock(&d_inode(direntry)->i_lock);
  1807. i_size_write(d_inode(direntry), 0);
  1808. clear_nlink(d_inode(direntry));
  1809. spin_unlock(&d_inode(direntry)->i_lock);
  1810. }
  1811. cifsInode = CIFS_I(d_inode(direntry));
  1812. /* force revalidate to go get info when needed */
  1813. cifsInode->time = 0;
  1814. cifsInode = CIFS_I(inode);
  1815. /*
  1816. * Force revalidate to get parent dir info when needed since cached
  1817. * attributes are invalid now.
  1818. */
  1819. cifsInode->time = 0;
  1820. d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
  1821. current_time(inode);
  1822. rmdir_exit:
  1823. free_dentry_path(page);
  1824. free_xid(xid);
  1825. return rc;
  1826. }
  1827. static int
  1828. cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
  1829. const char *from_path, struct dentry *to_dentry,
  1830. const char *to_path)
  1831. {
  1832. struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
  1833. struct tcon_link *tlink;
  1834. struct cifs_tcon *tcon;
  1835. struct TCP_Server_Info *server;
  1836. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1837. struct cifs_fid fid;
  1838. struct cifs_open_parms oparms;
  1839. int oplock;
  1840. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1841. int rc;
  1842. tlink = cifs_sb_tlink(cifs_sb);
  1843. if (IS_ERR(tlink))
  1844. return PTR_ERR(tlink);
  1845. tcon = tlink_tcon(tlink);
  1846. server = tcon->ses->server;
  1847. if (!server->ops->rename)
  1848. return -ENOSYS;
  1849. /* try path-based rename first */
  1850. rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
  1851. /*
  1852. * Don't bother with rename by filehandle unless file is busy and
  1853. * source. Note that cross directory moves do not work with
  1854. * rename by filehandle to various Windows servers.
  1855. */
  1856. if (rc == 0 || rc != -EBUSY)
  1857. goto do_rename_exit;
  1858. /* Don't fall back to using SMB on SMB 2+ mount */
  1859. if (server->vals->protocol_id != 0)
  1860. goto do_rename_exit;
  1861. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1862. /* open-file renames don't work across directories */
  1863. if (to_dentry->d_parent != from_dentry->d_parent)
  1864. goto do_rename_exit;
  1865. oparms = (struct cifs_open_parms) {
  1866. .tcon = tcon,
  1867. .cifs_sb = cifs_sb,
  1868. /* open the file to be renamed -- we need DELETE perms */
  1869. .desired_access = DELETE,
  1870. .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
  1871. .disposition = FILE_OPEN,
  1872. .path = from_path,
  1873. .fid = &fid,
  1874. };
  1875. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1876. if (rc == 0) {
  1877. rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
  1878. (const char *) to_dentry->d_name.name,
  1879. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1880. CIFSSMBClose(xid, tcon, fid.netfid);
  1881. }
  1882. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1883. do_rename_exit:
  1884. if (rc == 0)
  1885. d_move(from_dentry, to_dentry);
  1886. cifs_put_tlink(tlink);
  1887. return rc;
  1888. }
  1889. int
  1890. cifs_rename2(struct user_namespace *mnt_userns, struct inode *source_dir,
  1891. struct dentry *source_dentry, struct inode *target_dir,
  1892. struct dentry *target_dentry, unsigned int flags)
  1893. {
  1894. const char *from_name, *to_name;
  1895. void *page1, *page2;
  1896. struct cifs_sb_info *cifs_sb;
  1897. struct tcon_link *tlink;
  1898. struct cifs_tcon *tcon;
  1899. unsigned int xid;
  1900. int rc, tmprc;
  1901. int retry_count = 0;
  1902. FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
  1903. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1904. FILE_UNIX_BASIC_INFO *info_buf_target;
  1905. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1906. if (flags & ~RENAME_NOREPLACE)
  1907. return -EINVAL;
  1908. cifs_sb = CIFS_SB(source_dir->i_sb);
  1909. if (unlikely(cifs_forced_shutdown(cifs_sb)))
  1910. return -EIO;
  1911. tlink = cifs_sb_tlink(cifs_sb);
  1912. if (IS_ERR(tlink))
  1913. return PTR_ERR(tlink);
  1914. tcon = tlink_tcon(tlink);
  1915. page1 = alloc_dentry_path();
  1916. page2 = alloc_dentry_path();
  1917. xid = get_xid();
  1918. from_name = build_path_from_dentry(source_dentry, page1);
  1919. if (IS_ERR(from_name)) {
  1920. rc = PTR_ERR(from_name);
  1921. goto cifs_rename_exit;
  1922. }
  1923. to_name = build_path_from_dentry(target_dentry, page2);
  1924. if (IS_ERR(to_name)) {
  1925. rc = PTR_ERR(to_name);
  1926. goto cifs_rename_exit;
  1927. }
  1928. cifs_close_deferred_file_under_dentry(tcon, from_name);
  1929. if (d_inode(target_dentry) != NULL)
  1930. cifs_close_deferred_file_under_dentry(tcon, to_name);
  1931. rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
  1932. to_name);
  1933. if (rc == -EACCES) {
  1934. while (retry_count < 3) {
  1935. cifs_close_all_deferred_files(tcon);
  1936. rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
  1937. to_name);
  1938. if (rc != -EACCES)
  1939. break;
  1940. retry_count++;
  1941. }
  1942. }
  1943. /*
  1944. * No-replace is the natural behavior for CIFS, so skip unlink hacks.
  1945. */
  1946. if (flags & RENAME_NOREPLACE)
  1947. goto cifs_rename_exit;
  1948. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1949. if (rc == -EEXIST && tcon->unix_ext) {
  1950. /*
  1951. * Are src and dst hardlinks of same inode? We can only tell
  1952. * with unix extensions enabled.
  1953. */
  1954. info_buf_source =
  1955. kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
  1956. GFP_KERNEL);
  1957. if (info_buf_source == NULL) {
  1958. rc = -ENOMEM;
  1959. goto cifs_rename_exit;
  1960. }
  1961. info_buf_target = info_buf_source + 1;
  1962. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
  1963. info_buf_source,
  1964. cifs_sb->local_nls,
  1965. cifs_remap(cifs_sb));
  1966. if (tmprc != 0)
  1967. goto unlink_target;
  1968. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
  1969. info_buf_target,
  1970. cifs_sb->local_nls,
  1971. cifs_remap(cifs_sb));
  1972. if (tmprc == 0 && (info_buf_source->UniqueId ==
  1973. info_buf_target->UniqueId)) {
  1974. /* same file, POSIX says that this is a noop */
  1975. rc = 0;
  1976. goto cifs_rename_exit;
  1977. }
  1978. }
  1979. /*
  1980. * else ... BB we could add the same check for Windows by
  1981. * checking the UniqueId via FILE_INTERNAL_INFO
  1982. */
  1983. unlink_target:
  1984. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1985. /* Try unlinking the target dentry if it's not negative */
  1986. if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
  1987. if (d_is_dir(target_dentry))
  1988. tmprc = cifs_rmdir(target_dir, target_dentry);
  1989. else
  1990. tmprc = cifs_unlink(target_dir, target_dentry);
  1991. if (tmprc)
  1992. goto cifs_rename_exit;
  1993. rc = cifs_do_rename(xid, source_dentry, from_name,
  1994. target_dentry, to_name);
  1995. }
  1996. /* force revalidate to go get info when needed */
  1997. CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
  1998. source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
  1999. target_dir->i_mtime = current_time(source_dir);
  2000. cifs_rename_exit:
  2001. kfree(info_buf_source);
  2002. free_dentry_path(page2);
  2003. free_dentry_path(page1);
  2004. free_xid(xid);
  2005. cifs_put_tlink(tlink);
  2006. return rc;
  2007. }
  2008. static bool
  2009. cifs_dentry_needs_reval(struct dentry *dentry)
  2010. {
  2011. struct inode *inode = d_inode(dentry);
  2012. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  2013. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2014. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  2015. struct cached_fid *cfid = NULL;
  2016. if (cifs_i->time == 0)
  2017. return true;
  2018. if (CIFS_CACHE_READ(cifs_i))
  2019. return false;
  2020. if (!lookupCacheEnabled)
  2021. return true;
  2022. if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
  2023. spin_lock(&cfid->fid_lock);
  2024. if (cfid->time && cifs_i->time > cfid->time) {
  2025. spin_unlock(&cfid->fid_lock);
  2026. close_cached_dir(cfid);
  2027. return false;
  2028. }
  2029. spin_unlock(&cfid->fid_lock);
  2030. close_cached_dir(cfid);
  2031. }
  2032. /*
  2033. * depending on inode type, check if attribute caching disabled for
  2034. * files or directories
  2035. */
  2036. if (S_ISDIR(inode->i_mode)) {
  2037. if (!cifs_sb->ctx->acdirmax)
  2038. return true;
  2039. if (!time_in_range(jiffies, cifs_i->time,
  2040. cifs_i->time + cifs_sb->ctx->acdirmax))
  2041. return true;
  2042. } else { /* file */
  2043. if (!cifs_sb->ctx->acregmax)
  2044. return true;
  2045. if (!time_in_range(jiffies, cifs_i->time,
  2046. cifs_i->time + cifs_sb->ctx->acregmax))
  2047. return true;
  2048. }
  2049. /* hardlinked files w/ noserverino get "special" treatment */
  2050. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
  2051. S_ISREG(inode->i_mode) && inode->i_nlink != 1)
  2052. return true;
  2053. return false;
  2054. }
  2055. /*
  2056. * Zap the cache. Called when invalid_mapping flag is set.
  2057. */
  2058. int
  2059. cifs_invalidate_mapping(struct inode *inode)
  2060. {
  2061. int rc = 0;
  2062. if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
  2063. rc = invalidate_inode_pages2(inode->i_mapping);
  2064. if (rc)
  2065. cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
  2066. __func__, inode);
  2067. }
  2068. return rc;
  2069. }
  2070. /**
  2071. * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
  2072. *
  2073. * @key: currently unused
  2074. * @mode: the task state to sleep in
  2075. */
  2076. static int
  2077. cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
  2078. {
  2079. schedule();
  2080. if (signal_pending_state(mode, current))
  2081. return -ERESTARTSYS;
  2082. return 0;
  2083. }
  2084. int
  2085. cifs_revalidate_mapping(struct inode *inode)
  2086. {
  2087. int rc;
  2088. unsigned long *flags = &CIFS_I(inode)->flags;
  2089. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2090. /* swapfiles are not supposed to be shared */
  2091. if (IS_SWAPFILE(inode))
  2092. return 0;
  2093. rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
  2094. TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
  2095. if (rc)
  2096. return rc;
  2097. if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
  2098. /* for cache=singleclient, do not invalidate */
  2099. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
  2100. goto skip_invalidate;
  2101. rc = cifs_invalidate_mapping(inode);
  2102. if (rc)
  2103. set_bit(CIFS_INO_INVALID_MAPPING, flags);
  2104. }
  2105. skip_invalidate:
  2106. clear_bit_unlock(CIFS_INO_LOCK, flags);
  2107. smp_mb__after_atomic();
  2108. wake_up_bit(flags, CIFS_INO_LOCK);
  2109. return rc;
  2110. }
  2111. int
  2112. cifs_zap_mapping(struct inode *inode)
  2113. {
  2114. set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
  2115. return cifs_revalidate_mapping(inode);
  2116. }
  2117. int cifs_revalidate_file_attr(struct file *filp)
  2118. {
  2119. int rc = 0;
  2120. struct dentry *dentry = file_dentry(filp);
  2121. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  2122. struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
  2123. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  2124. if (!cifs_dentry_needs_reval(dentry))
  2125. return rc;
  2126. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  2127. if (tlink_tcon(cfile->tlink)->unix_ext)
  2128. rc = cifs_get_file_info_unix(filp);
  2129. else
  2130. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  2131. rc = cifs_get_file_info(filp);
  2132. return rc;
  2133. }
  2134. int cifs_revalidate_dentry_attr(struct dentry *dentry)
  2135. {
  2136. unsigned int xid;
  2137. int rc = 0;
  2138. struct inode *inode = d_inode(dentry);
  2139. struct super_block *sb = dentry->d_sb;
  2140. const char *full_path;
  2141. void *page;
  2142. int count = 0;
  2143. if (inode == NULL)
  2144. return -ENOENT;
  2145. if (!cifs_dentry_needs_reval(dentry))
  2146. return rc;
  2147. xid = get_xid();
  2148. page = alloc_dentry_path();
  2149. full_path = build_path_from_dentry(dentry, page);
  2150. if (IS_ERR(full_path)) {
  2151. rc = PTR_ERR(full_path);
  2152. goto out;
  2153. }
  2154. cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
  2155. full_path, inode, inode->i_count.counter,
  2156. dentry, cifs_get_time(dentry), jiffies);
  2157. again:
  2158. if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
  2159. rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
  2160. else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
  2161. rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
  2162. else
  2163. rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
  2164. xid, NULL);
  2165. if (rc == -EAGAIN && count++ < 10)
  2166. goto again;
  2167. out:
  2168. free_dentry_path(page);
  2169. free_xid(xid);
  2170. return rc;
  2171. }
  2172. int cifs_revalidate_file(struct file *filp)
  2173. {
  2174. int rc;
  2175. struct inode *inode = file_inode(filp);
  2176. rc = cifs_revalidate_file_attr(filp);
  2177. if (rc)
  2178. return rc;
  2179. return cifs_revalidate_mapping(inode);
  2180. }
  2181. /* revalidate a dentry's inode attributes */
  2182. int cifs_revalidate_dentry(struct dentry *dentry)
  2183. {
  2184. int rc;
  2185. struct inode *inode = d_inode(dentry);
  2186. rc = cifs_revalidate_dentry_attr(dentry);
  2187. if (rc)
  2188. return rc;
  2189. return cifs_revalidate_mapping(inode);
  2190. }
  2191. int cifs_getattr(struct user_namespace *mnt_userns, const struct path *path,
  2192. struct kstat *stat, u32 request_mask, unsigned int flags)
  2193. {
  2194. struct dentry *dentry = path->dentry;
  2195. struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
  2196. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  2197. struct inode *inode = d_inode(dentry);
  2198. int rc;
  2199. if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
  2200. return -EIO;
  2201. /*
  2202. * We need to be sure that all dirty pages are written and the server
  2203. * has actual ctime, mtime and file length.
  2204. */
  2205. if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
  2206. !CIFS_CACHE_READ(CIFS_I(inode)) &&
  2207. inode->i_mapping && inode->i_mapping->nrpages != 0) {
  2208. rc = filemap_fdatawait(inode->i_mapping);
  2209. if (rc) {
  2210. mapping_set_error(inode->i_mapping, rc);
  2211. return rc;
  2212. }
  2213. }
  2214. if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
  2215. CIFS_I(inode)->time = 0; /* force revalidate */
  2216. /*
  2217. * If the caller doesn't require syncing, only sync if
  2218. * necessary (e.g. due to earlier truncate or setattr
  2219. * invalidating the cached metadata)
  2220. */
  2221. if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
  2222. (CIFS_I(inode)->time == 0)) {
  2223. rc = cifs_revalidate_dentry_attr(dentry);
  2224. if (rc)
  2225. return rc;
  2226. }
  2227. generic_fillattr(&init_user_ns, inode, stat);
  2228. stat->blksize = cifs_sb->ctx->bsize;
  2229. stat->ino = CIFS_I(inode)->uniqueid;
  2230. /* old CIFS Unix Extensions doesn't return create time */
  2231. if (CIFS_I(inode)->createtime) {
  2232. stat->result_mask |= STATX_BTIME;
  2233. stat->btime =
  2234. cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
  2235. }
  2236. stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
  2237. if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
  2238. stat->attributes |= STATX_ATTR_COMPRESSED;
  2239. if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
  2240. stat->attributes |= STATX_ATTR_ENCRYPTED;
  2241. /*
  2242. * If on a multiuser mount without unix extensions or cifsacl being
  2243. * enabled, and the admin hasn't overridden them, set the ownership
  2244. * to the fsuid/fsgid of the current process.
  2245. */
  2246. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
  2247. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  2248. !tcon->unix_ext) {
  2249. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
  2250. stat->uid = current_fsuid();
  2251. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
  2252. stat->gid = current_fsgid();
  2253. }
  2254. return 0;
  2255. }
  2256. int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
  2257. u64 len)
  2258. {
  2259. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  2260. struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
  2261. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  2262. struct TCP_Server_Info *server = tcon->ses->server;
  2263. struct cifsFileInfo *cfile;
  2264. int rc;
  2265. if (unlikely(cifs_forced_shutdown(cifs_sb)))
  2266. return -EIO;
  2267. /*
  2268. * We need to be sure that all dirty pages are written as they
  2269. * might fill holes on the server.
  2270. */
  2271. if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
  2272. inode->i_mapping->nrpages != 0) {
  2273. rc = filemap_fdatawait(inode->i_mapping);
  2274. if (rc) {
  2275. mapping_set_error(inode->i_mapping, rc);
  2276. return rc;
  2277. }
  2278. }
  2279. cfile = find_readable_file(cifs_i, false);
  2280. if (cfile == NULL)
  2281. return -EINVAL;
  2282. if (server->ops->fiemap) {
  2283. rc = server->ops->fiemap(tcon, cfile, fei, start, len);
  2284. cifsFileInfo_put(cfile);
  2285. return rc;
  2286. }
  2287. cifsFileInfo_put(cfile);
  2288. return -EOPNOTSUPP;
  2289. }
  2290. int cifs_truncate_page(struct address_space *mapping, loff_t from)
  2291. {
  2292. pgoff_t index = from >> PAGE_SHIFT;
  2293. unsigned offset = from & (PAGE_SIZE - 1);
  2294. struct page *page;
  2295. int rc = 0;
  2296. page = grab_cache_page(mapping, index);
  2297. if (!page)
  2298. return -ENOMEM;
  2299. zero_user_segment(page, offset, PAGE_SIZE);
  2300. unlock_page(page);
  2301. put_page(page);
  2302. return rc;
  2303. }
  2304. void cifs_setsize(struct inode *inode, loff_t offset)
  2305. {
  2306. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  2307. spin_lock(&inode->i_lock);
  2308. i_size_write(inode, offset);
  2309. spin_unlock(&inode->i_lock);
  2310. /* Cached inode must be refreshed on truncate */
  2311. cifs_i->time = 0;
  2312. truncate_pagecache(inode, offset);
  2313. }
  2314. static int
  2315. cifs_set_file_size(struct inode *inode, struct iattr *attrs,
  2316. unsigned int xid, const char *full_path)
  2317. {
  2318. int rc;
  2319. struct cifsFileInfo *open_file;
  2320. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  2321. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2322. struct tcon_link *tlink = NULL;
  2323. struct cifs_tcon *tcon = NULL;
  2324. struct TCP_Server_Info *server;
  2325. /*
  2326. * To avoid spurious oplock breaks from server, in the case of
  2327. * inodes that we already have open, avoid doing path based
  2328. * setting of file size if we can do it by handle.
  2329. * This keeps our caching token (oplock) and avoids timeouts
  2330. * when the local oplock break takes longer to flush
  2331. * writebehind data than the SMB timeout for the SetPathInfo
  2332. * request would allow
  2333. */
  2334. open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
  2335. if (open_file) {
  2336. tcon = tlink_tcon(open_file->tlink);
  2337. server = tcon->ses->server;
  2338. if (server->ops->set_file_size)
  2339. rc = server->ops->set_file_size(xid, tcon, open_file,
  2340. attrs->ia_size, false);
  2341. else
  2342. rc = -ENOSYS;
  2343. cifsFileInfo_put(open_file);
  2344. cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
  2345. } else
  2346. rc = -EINVAL;
  2347. if (!rc)
  2348. goto set_size_out;
  2349. if (tcon == NULL) {
  2350. tlink = cifs_sb_tlink(cifs_sb);
  2351. if (IS_ERR(tlink))
  2352. return PTR_ERR(tlink);
  2353. tcon = tlink_tcon(tlink);
  2354. server = tcon->ses->server;
  2355. }
  2356. /*
  2357. * Set file size by pathname rather than by handle either because no
  2358. * valid, writeable file handle for it was found or because there was
  2359. * an error setting it by handle.
  2360. */
  2361. if (server->ops->set_path_size)
  2362. rc = server->ops->set_path_size(xid, tcon, full_path,
  2363. attrs->ia_size, cifs_sb, false);
  2364. else
  2365. rc = -ENOSYS;
  2366. cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
  2367. if (tlink)
  2368. cifs_put_tlink(tlink);
  2369. set_size_out:
  2370. if (rc == 0) {
  2371. cifsInode->server_eof = attrs->ia_size;
  2372. cifs_setsize(inode, attrs->ia_size);
  2373. /*
  2374. * i_blocks is not related to (i_size / i_blksize), but instead
  2375. * 512 byte (2**9) size is required for calculating num blocks.
  2376. * Until we can query the server for actual allocation size,
  2377. * this is best estimate we have for blocks allocated for a file
  2378. * Number of blocks must be rounded up so size 1 is not 0 blocks
  2379. */
  2380. inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
  2381. /*
  2382. * The man page of truncate says if the size changed,
  2383. * then the st_ctime and st_mtime fields for the file
  2384. * are updated.
  2385. */
  2386. attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
  2387. attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  2388. cifs_truncate_page(inode->i_mapping, inode->i_size);
  2389. }
  2390. return rc;
  2391. }
  2392. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  2393. static int
  2394. cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
  2395. {
  2396. int rc;
  2397. unsigned int xid;
  2398. const char *full_path;
  2399. void *page = alloc_dentry_path();
  2400. struct inode *inode = d_inode(direntry);
  2401. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  2402. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2403. struct tcon_link *tlink;
  2404. struct cifs_tcon *pTcon;
  2405. struct cifs_unix_set_info_args *args = NULL;
  2406. struct cifsFileInfo *open_file;
  2407. cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
  2408. direntry, attrs->ia_valid);
  2409. xid = get_xid();
  2410. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  2411. attrs->ia_valid |= ATTR_FORCE;
  2412. rc = setattr_prepare(&init_user_ns, direntry, attrs);
  2413. if (rc < 0)
  2414. goto out;
  2415. full_path = build_path_from_dentry(direntry, page);
  2416. if (IS_ERR(full_path)) {
  2417. rc = PTR_ERR(full_path);
  2418. goto out;
  2419. }
  2420. /*
  2421. * Attempt to flush data before changing attributes. We need to do
  2422. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  2423. * ownership or mode then we may also need to do this. Here, we take
  2424. * the safe way out and just do the flush on all setattr requests. If
  2425. * the flush returns error, store it to report later and continue.
  2426. *
  2427. * BB: This should be smarter. Why bother flushing pages that
  2428. * will be truncated anyway? Also, should we error out here if
  2429. * the flush returns error?
  2430. */
  2431. rc = filemap_write_and_wait(inode->i_mapping);
  2432. if (is_interrupt_error(rc)) {
  2433. rc = -ERESTARTSYS;
  2434. goto out;
  2435. }
  2436. mapping_set_error(inode->i_mapping, rc);
  2437. rc = 0;
  2438. if (attrs->ia_valid & ATTR_SIZE) {
  2439. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  2440. if (rc != 0)
  2441. goto out;
  2442. }
  2443. /* skip mode change if it's just for clearing setuid/setgid */
  2444. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  2445. attrs->ia_valid &= ~ATTR_MODE;
  2446. args = kmalloc(sizeof(*args), GFP_KERNEL);
  2447. if (args == NULL) {
  2448. rc = -ENOMEM;
  2449. goto out;
  2450. }
  2451. /* set up the struct */
  2452. if (attrs->ia_valid & ATTR_MODE)
  2453. args->mode = attrs->ia_mode;
  2454. else
  2455. args->mode = NO_CHANGE_64;
  2456. if (attrs->ia_valid & ATTR_UID)
  2457. args->uid = attrs->ia_uid;
  2458. else
  2459. args->uid = INVALID_UID; /* no change */
  2460. if (attrs->ia_valid & ATTR_GID)
  2461. args->gid = attrs->ia_gid;
  2462. else
  2463. args->gid = INVALID_GID; /* no change */
  2464. if (attrs->ia_valid & ATTR_ATIME)
  2465. args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
  2466. else
  2467. args->atime = NO_CHANGE_64;
  2468. if (attrs->ia_valid & ATTR_MTIME)
  2469. args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
  2470. else
  2471. args->mtime = NO_CHANGE_64;
  2472. if (attrs->ia_valid & ATTR_CTIME)
  2473. args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
  2474. else
  2475. args->ctime = NO_CHANGE_64;
  2476. args->device = 0;
  2477. open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
  2478. if (open_file) {
  2479. u16 nfid = open_file->fid.netfid;
  2480. u32 npid = open_file->pid;
  2481. pTcon = tlink_tcon(open_file->tlink);
  2482. rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
  2483. cifsFileInfo_put(open_file);
  2484. } else {
  2485. tlink = cifs_sb_tlink(cifs_sb);
  2486. if (IS_ERR(tlink)) {
  2487. rc = PTR_ERR(tlink);
  2488. goto out;
  2489. }
  2490. pTcon = tlink_tcon(tlink);
  2491. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
  2492. cifs_sb->local_nls,
  2493. cifs_remap(cifs_sb));
  2494. cifs_put_tlink(tlink);
  2495. }
  2496. if (rc)
  2497. goto out;
  2498. if ((attrs->ia_valid & ATTR_SIZE) &&
  2499. attrs->ia_size != i_size_read(inode)) {
  2500. truncate_setsize(inode, attrs->ia_size);
  2501. fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
  2502. }
  2503. setattr_copy(&init_user_ns, inode, attrs);
  2504. mark_inode_dirty(inode);
  2505. /* force revalidate when any of these times are set since some
  2506. of the fs types (eg ext3, fat) do not have fine enough
  2507. time granularity to match protocol, and we do not have a
  2508. a way (yet) to query the server fs's time granularity (and
  2509. whether it rounds times down).
  2510. */
  2511. if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
  2512. cifsInode->time = 0;
  2513. out:
  2514. kfree(args);
  2515. free_dentry_path(page);
  2516. free_xid(xid);
  2517. return rc;
  2518. }
  2519. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  2520. static int
  2521. cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
  2522. {
  2523. unsigned int xid;
  2524. kuid_t uid = INVALID_UID;
  2525. kgid_t gid = INVALID_GID;
  2526. struct inode *inode = d_inode(direntry);
  2527. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2528. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  2529. struct cifsFileInfo *wfile;
  2530. struct cifs_tcon *tcon;
  2531. const char *full_path;
  2532. void *page = alloc_dentry_path();
  2533. int rc = -EACCES;
  2534. __u32 dosattr = 0;
  2535. __u64 mode = NO_CHANGE_64;
  2536. xid = get_xid();
  2537. cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
  2538. direntry, attrs->ia_valid);
  2539. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  2540. attrs->ia_valid |= ATTR_FORCE;
  2541. rc = setattr_prepare(&init_user_ns, direntry, attrs);
  2542. if (rc < 0)
  2543. goto cifs_setattr_exit;
  2544. full_path = build_path_from_dentry(direntry, page);
  2545. if (IS_ERR(full_path)) {
  2546. rc = PTR_ERR(full_path);
  2547. goto cifs_setattr_exit;
  2548. }
  2549. /*
  2550. * Attempt to flush data before changing attributes. We need to do
  2551. * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
  2552. * returns error, store it to report later and continue.
  2553. *
  2554. * BB: This should be smarter. Why bother flushing pages that
  2555. * will be truncated anyway? Also, should we error out here if
  2556. * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
  2557. */
  2558. if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
  2559. rc = filemap_write_and_wait(inode->i_mapping);
  2560. if (is_interrupt_error(rc)) {
  2561. rc = -ERESTARTSYS;
  2562. goto cifs_setattr_exit;
  2563. }
  2564. mapping_set_error(inode->i_mapping, rc);
  2565. }
  2566. rc = 0;
  2567. if ((attrs->ia_valid & ATTR_MTIME) &&
  2568. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  2569. rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
  2570. if (!rc) {
  2571. tcon = tlink_tcon(wfile->tlink);
  2572. rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
  2573. cifsFileInfo_put(wfile);
  2574. if (rc)
  2575. goto cifs_setattr_exit;
  2576. } else if (rc != -EBADF)
  2577. goto cifs_setattr_exit;
  2578. else
  2579. rc = 0;
  2580. }
  2581. if (attrs->ia_valid & ATTR_SIZE) {
  2582. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  2583. if (rc != 0)
  2584. goto cifs_setattr_exit;
  2585. }
  2586. if (attrs->ia_valid & ATTR_UID)
  2587. uid = attrs->ia_uid;
  2588. if (attrs->ia_valid & ATTR_GID)
  2589. gid = attrs->ia_gid;
  2590. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
  2591. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
  2592. if (uid_valid(uid) || gid_valid(gid)) {
  2593. mode = NO_CHANGE_64;
  2594. rc = id_mode_to_cifs_acl(inode, full_path, &mode,
  2595. uid, gid);
  2596. if (rc) {
  2597. cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
  2598. __func__, rc);
  2599. goto cifs_setattr_exit;
  2600. }
  2601. }
  2602. } else
  2603. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
  2604. attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
  2605. /* skip mode change if it's just for clearing setuid/setgid */
  2606. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  2607. attrs->ia_valid &= ~ATTR_MODE;
  2608. if (attrs->ia_valid & ATTR_MODE) {
  2609. mode = attrs->ia_mode;
  2610. rc = 0;
  2611. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
  2612. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
  2613. rc = id_mode_to_cifs_acl(inode, full_path, &mode,
  2614. INVALID_UID, INVALID_GID);
  2615. if (rc) {
  2616. cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
  2617. __func__, rc);
  2618. goto cifs_setattr_exit;
  2619. }
  2620. /*
  2621. * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
  2622. * Pick up the actual mode bits that were set.
  2623. */
  2624. if (mode != attrs->ia_mode)
  2625. attrs->ia_mode = mode;
  2626. } else
  2627. if (((mode & S_IWUGO) == 0) &&
  2628. (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  2629. dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
  2630. /* fix up mode if we're not using dynperm */
  2631. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  2632. attrs->ia_mode = inode->i_mode & ~S_IWUGO;
  2633. } else if ((mode & S_IWUGO) &&
  2634. (cifsInode->cifsAttrs & ATTR_READONLY)) {
  2635. dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
  2636. /* Attributes of 0 are ignored */
  2637. if (dosattr == 0)
  2638. dosattr |= ATTR_NORMAL;
  2639. /* reset local inode permissions to normal */
  2640. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  2641. attrs->ia_mode &= ~(S_IALLUGO);
  2642. if (S_ISDIR(inode->i_mode))
  2643. attrs->ia_mode |=
  2644. cifs_sb->ctx->dir_mode;
  2645. else
  2646. attrs->ia_mode |=
  2647. cifs_sb->ctx->file_mode;
  2648. }
  2649. } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  2650. /* ignore mode change - ATTR_READONLY hasn't changed */
  2651. attrs->ia_valid &= ~ATTR_MODE;
  2652. }
  2653. }
  2654. if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
  2655. ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
  2656. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  2657. /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
  2658. /* Even if error on time set, no sense failing the call if
  2659. the server would set the time to a reasonable value anyway,
  2660. and this check ensures that we are not being called from
  2661. sys_utimes in which case we ought to fail the call back to
  2662. the user when the server rejects the call */
  2663. if ((rc) && (attrs->ia_valid &
  2664. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  2665. rc = 0;
  2666. }
  2667. /* do not need local check to inode_check_ok since the server does
  2668. that */
  2669. if (rc)
  2670. goto cifs_setattr_exit;
  2671. if ((attrs->ia_valid & ATTR_SIZE) &&
  2672. attrs->ia_size != i_size_read(inode)) {
  2673. truncate_setsize(inode, attrs->ia_size);
  2674. fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
  2675. }
  2676. setattr_copy(&init_user_ns, inode, attrs);
  2677. mark_inode_dirty(inode);
  2678. cifs_setattr_exit:
  2679. free_xid(xid);
  2680. free_dentry_path(page);
  2681. return rc;
  2682. }
  2683. int
  2684. cifs_setattr(struct user_namespace *mnt_userns, struct dentry *direntry,
  2685. struct iattr *attrs)
  2686. {
  2687. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  2688. int rc, retries = 0;
  2689. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  2690. struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
  2691. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  2692. if (unlikely(cifs_forced_shutdown(cifs_sb)))
  2693. return -EIO;
  2694. do {
  2695. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  2696. if (pTcon->unix_ext)
  2697. rc = cifs_setattr_unix(direntry, attrs);
  2698. else
  2699. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  2700. rc = cifs_setattr_nounix(direntry, attrs);
  2701. retries++;
  2702. } while (is_retryable_error(rc) && retries < 2);
  2703. /* BB: add cifs_setattr_legacy for really old servers */
  2704. return rc;
  2705. }