inode.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. /*
  2. * fs/cifs/inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2010
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/slab.h>
  24. #include <linux/pagemap.h>
  25. #include <asm/div64.h>
  26. #include "cifsfs.h"
  27. #include "cifspdu.h"
  28. #include "cifsglob.h"
  29. #include "cifsproto.h"
  30. #include "cifs_debug.h"
  31. #include "cifs_fs_sb.h"
  32. #include "fscache.h"
  33. static void cifs_set_ops(struct inode *inode)
  34. {
  35. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  36. switch (inode->i_mode & S_IFMT) {
  37. case S_IFREG:
  38. inode->i_op = &cifs_file_inode_ops;
  39. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  40. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  41. inode->i_fop = &cifs_file_direct_nobrl_ops;
  42. else
  43. inode->i_fop = &cifs_file_direct_ops;
  44. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  45. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  46. inode->i_fop = &cifs_file_strict_nobrl_ops;
  47. else
  48. inode->i_fop = &cifs_file_strict_ops;
  49. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  50. inode->i_fop = &cifs_file_nobrl_ops;
  51. else { /* not direct, send byte range locks */
  52. inode->i_fop = &cifs_file_ops;
  53. }
  54. /* check if server can support readpages */
  55. if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
  56. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
  57. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  58. else
  59. inode->i_data.a_ops = &cifs_addr_ops;
  60. break;
  61. case S_IFDIR:
  62. #ifdef CONFIG_CIFS_DFS_UPCALL
  63. if (IS_AUTOMOUNT(inode)) {
  64. inode->i_op = &cifs_dfs_referral_inode_operations;
  65. } else {
  66. #else /* NO DFS support, treat as a directory */
  67. {
  68. #endif
  69. inode->i_op = &cifs_dir_inode_ops;
  70. inode->i_fop = &cifs_dir_ops;
  71. }
  72. break;
  73. case S_IFLNK:
  74. inode->i_op = &cifs_symlink_inode_ops;
  75. break;
  76. default:
  77. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  78. break;
  79. }
  80. }
  81. /* check inode attributes against fattr. If they don't match, tag the
  82. * inode for cache invalidation
  83. */
  84. static void
  85. cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
  86. {
  87. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  88. cifs_dbg(FYI, "%s: revalidating inode %llu\n",
  89. __func__, cifs_i->uniqueid);
  90. if (inode->i_state & I_NEW) {
  91. cifs_dbg(FYI, "%s: inode %llu is new\n",
  92. __func__, cifs_i->uniqueid);
  93. return;
  94. }
  95. /* don't bother with revalidation if we have an oplock */
  96. if (cifs_i->clientCanCacheRead) {
  97. cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
  98. __func__, cifs_i->uniqueid);
  99. return;
  100. }
  101. /* revalidate if mtime or size have changed */
  102. if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
  103. cifs_i->server_eof == fattr->cf_eof) {
  104. cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
  105. __func__, cifs_i->uniqueid);
  106. return;
  107. }
  108. cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
  109. __func__, cifs_i->uniqueid);
  110. cifs_i->invalid_mapping = true;
  111. }
  112. /* populate an inode with info from a cifs_fattr struct */
  113. void
  114. cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  115. {
  116. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  117. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  118. cifs_revalidate_cache(inode, fattr);
  119. spin_lock(&inode->i_lock);
  120. inode->i_atime = fattr->cf_atime;
  121. inode->i_mtime = fattr->cf_mtime;
  122. inode->i_ctime = fattr->cf_ctime;
  123. inode->i_rdev = fattr->cf_rdev;
  124. set_nlink(inode, fattr->cf_nlink);
  125. inode->i_uid = fattr->cf_uid;
  126. inode->i_gid = fattr->cf_gid;
  127. /* if dynperm is set, don't clobber existing mode */
  128. if (inode->i_state & I_NEW ||
  129. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
  130. inode->i_mode = fattr->cf_mode;
  131. cifs_i->cifsAttrs = fattr->cf_cifsattrs;
  132. if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
  133. cifs_i->time = 0;
  134. else
  135. cifs_i->time = jiffies;
  136. cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING;
  137. cifs_i->server_eof = fattr->cf_eof;
  138. /*
  139. * Can't safely change the file size here if the client is writing to
  140. * it due to potential races.
  141. */
  142. if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
  143. i_size_write(inode, fattr->cf_eof);
  144. /*
  145. * i_blocks is not related to (i_size / i_blksize),
  146. * but instead 512 byte (2**9) size is required for
  147. * calculating num blocks.
  148. */
  149. inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
  150. }
  151. spin_unlock(&inode->i_lock);
  152. if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
  153. inode->i_flags |= S_AUTOMOUNT;
  154. if (inode->i_state & I_NEW)
  155. cifs_set_ops(inode);
  156. }
  157. void
  158. cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
  159. {
  160. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  161. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  162. return;
  163. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  164. }
  165. /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
  166. void
  167. cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
  168. struct cifs_sb_info *cifs_sb)
  169. {
  170. memset(fattr, 0, sizeof(*fattr));
  171. fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
  172. fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
  173. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  174. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  175. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
  176. fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
  177. fattr->cf_mode = le64_to_cpu(info->Permissions);
  178. /*
  179. * Since we set the inode type below we need to mask off
  180. * to avoid strange results if bits set above.
  181. */
  182. fattr->cf_mode &= ~S_IFMT;
  183. switch (le32_to_cpu(info->Type)) {
  184. case UNIX_FILE:
  185. fattr->cf_mode |= S_IFREG;
  186. fattr->cf_dtype = DT_REG;
  187. break;
  188. case UNIX_SYMLINK:
  189. fattr->cf_mode |= S_IFLNK;
  190. fattr->cf_dtype = DT_LNK;
  191. break;
  192. case UNIX_DIR:
  193. fattr->cf_mode |= S_IFDIR;
  194. fattr->cf_dtype = DT_DIR;
  195. break;
  196. case UNIX_CHARDEV:
  197. fattr->cf_mode |= S_IFCHR;
  198. fattr->cf_dtype = DT_CHR;
  199. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  200. le64_to_cpu(info->DevMinor) & MINORMASK);
  201. break;
  202. case UNIX_BLOCKDEV:
  203. fattr->cf_mode |= S_IFBLK;
  204. fattr->cf_dtype = DT_BLK;
  205. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  206. le64_to_cpu(info->DevMinor) & MINORMASK);
  207. break;
  208. case UNIX_FIFO:
  209. fattr->cf_mode |= S_IFIFO;
  210. fattr->cf_dtype = DT_FIFO;
  211. break;
  212. case UNIX_SOCKET:
  213. fattr->cf_mode |= S_IFSOCK;
  214. fattr->cf_dtype = DT_SOCK;
  215. break;
  216. default:
  217. /* safest to call it a file if we do not know */
  218. fattr->cf_mode |= S_IFREG;
  219. fattr->cf_dtype = DT_REG;
  220. cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
  221. break;
  222. }
  223. fattr->cf_uid = cifs_sb->mnt_uid;
  224. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
  225. u64 id = le64_to_cpu(info->Uid);
  226. if (id < ((uid_t)-1)) {
  227. kuid_t uid = make_kuid(&init_user_ns, id);
  228. if (uid_valid(uid))
  229. fattr->cf_uid = uid;
  230. }
  231. }
  232. fattr->cf_gid = cifs_sb->mnt_gid;
  233. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
  234. u64 id = le64_to_cpu(info->Gid);
  235. if (id < ((gid_t)-1)) {
  236. kgid_t gid = make_kgid(&init_user_ns, id);
  237. if (gid_valid(gid))
  238. fattr->cf_gid = gid;
  239. }
  240. }
  241. fattr->cf_nlink = le64_to_cpu(info->Nlinks);
  242. }
  243. /*
  244. * Fill a cifs_fattr struct with fake inode info.
  245. *
  246. * Needed to setup cifs_fattr data for the directory which is the
  247. * junction to the new submount (ie to setup the fake directory
  248. * which represents a DFS referral).
  249. */
  250. static void
  251. cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
  252. {
  253. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  254. cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
  255. memset(fattr, 0, sizeof(*fattr));
  256. fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
  257. fattr->cf_uid = cifs_sb->mnt_uid;
  258. fattr->cf_gid = cifs_sb->mnt_gid;
  259. fattr->cf_atime = CURRENT_TIME;
  260. fattr->cf_ctime = CURRENT_TIME;
  261. fattr->cf_mtime = CURRENT_TIME;
  262. fattr->cf_nlink = 2;
  263. fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
  264. }
  265. static int
  266. cifs_get_file_info_unix(struct file *filp)
  267. {
  268. int rc;
  269. unsigned int xid;
  270. FILE_UNIX_BASIC_INFO find_data;
  271. struct cifs_fattr fattr;
  272. struct inode *inode = file_inode(filp);
  273. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  274. struct cifsFileInfo *cfile = filp->private_data;
  275. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  276. xid = get_xid();
  277. rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
  278. if (!rc) {
  279. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  280. } else if (rc == -EREMOTE) {
  281. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  282. rc = 0;
  283. }
  284. cifs_fattr_to_inode(inode, &fattr);
  285. free_xid(xid);
  286. return rc;
  287. }
  288. int cifs_get_inode_info_unix(struct inode **pinode,
  289. const unsigned char *full_path,
  290. struct super_block *sb, unsigned int xid)
  291. {
  292. int rc;
  293. FILE_UNIX_BASIC_INFO find_data;
  294. struct cifs_fattr fattr;
  295. struct cifs_tcon *tcon;
  296. struct tcon_link *tlink;
  297. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  298. cifs_dbg(FYI, "Getting info on %s\n", full_path);
  299. tlink = cifs_sb_tlink(cifs_sb);
  300. if (IS_ERR(tlink))
  301. return PTR_ERR(tlink);
  302. tcon = tlink_tcon(tlink);
  303. /* could have done a find first instead but this returns more info */
  304. rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
  305. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  306. CIFS_MOUNT_MAP_SPECIAL_CHR);
  307. cifs_put_tlink(tlink);
  308. if (!rc) {
  309. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  310. } else if (rc == -EREMOTE) {
  311. cifs_create_dfs_fattr(&fattr, sb);
  312. rc = 0;
  313. } else {
  314. return rc;
  315. }
  316. /* check for Minshall+French symlinks */
  317. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  318. int tmprc = CIFSCheckMFSymlink(&fattr, full_path, cifs_sb, xid);
  319. if (tmprc)
  320. cifs_dbg(FYI, "CIFSCheckMFSymlink: %d\n", tmprc);
  321. }
  322. if (*pinode == NULL) {
  323. /* get new inode */
  324. cifs_fill_uniqueid(sb, &fattr);
  325. *pinode = cifs_iget(sb, &fattr);
  326. if (!*pinode)
  327. rc = -ENOMEM;
  328. } else {
  329. /* we already have inode, update it */
  330. cifs_fattr_to_inode(*pinode, &fattr);
  331. }
  332. return rc;
  333. }
  334. static int
  335. cifs_sfu_type(struct cifs_fattr *fattr, const unsigned char *path,
  336. struct cifs_sb_info *cifs_sb, unsigned int xid)
  337. {
  338. int rc;
  339. int oplock = 0;
  340. __u16 netfid;
  341. struct tcon_link *tlink;
  342. struct cifs_tcon *tcon;
  343. struct cifs_io_parms io_parms;
  344. char buf[24];
  345. unsigned int bytes_read;
  346. char *pbuf;
  347. pbuf = buf;
  348. fattr->cf_mode &= ~S_IFMT;
  349. if (fattr->cf_eof == 0) {
  350. fattr->cf_mode |= S_IFIFO;
  351. fattr->cf_dtype = DT_FIFO;
  352. return 0;
  353. } else if (fattr->cf_eof < 8) {
  354. fattr->cf_mode |= S_IFREG;
  355. fattr->cf_dtype = DT_REG;
  356. return -EINVAL; /* EOPNOTSUPP? */
  357. }
  358. tlink = cifs_sb_tlink(cifs_sb);
  359. if (IS_ERR(tlink))
  360. return PTR_ERR(tlink);
  361. tcon = tlink_tcon(tlink);
  362. rc = CIFSSMBOpen(xid, tcon, path, FILE_OPEN, GENERIC_READ,
  363. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  364. cifs_sb->local_nls,
  365. cifs_sb->mnt_cifs_flags &
  366. CIFS_MOUNT_MAP_SPECIAL_CHR);
  367. if (rc == 0) {
  368. int buf_type = CIFS_NO_BUFFER;
  369. /* Read header */
  370. io_parms.netfid = netfid;
  371. io_parms.pid = current->tgid;
  372. io_parms.tcon = tcon;
  373. io_parms.offset = 0;
  374. io_parms.length = 24;
  375. rc = CIFSSMBRead(xid, &io_parms, &bytes_read, &pbuf,
  376. &buf_type);
  377. if ((rc == 0) && (bytes_read >= 8)) {
  378. if (memcmp("IntxBLK", pbuf, 8) == 0) {
  379. cifs_dbg(FYI, "Block device\n");
  380. fattr->cf_mode |= S_IFBLK;
  381. fattr->cf_dtype = DT_BLK;
  382. if (bytes_read == 24) {
  383. /* we have enough to decode dev num */
  384. __u64 mjr; /* major */
  385. __u64 mnr; /* minor */
  386. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  387. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  388. fattr->cf_rdev = MKDEV(mjr, mnr);
  389. }
  390. } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
  391. cifs_dbg(FYI, "Char device\n");
  392. fattr->cf_mode |= S_IFCHR;
  393. fattr->cf_dtype = DT_CHR;
  394. if (bytes_read == 24) {
  395. /* we have enough to decode dev num */
  396. __u64 mjr; /* major */
  397. __u64 mnr; /* minor */
  398. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  399. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  400. fattr->cf_rdev = MKDEV(mjr, mnr);
  401. }
  402. } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
  403. cifs_dbg(FYI, "Symlink\n");
  404. fattr->cf_mode |= S_IFLNK;
  405. fattr->cf_dtype = DT_LNK;
  406. } else {
  407. fattr->cf_mode |= S_IFREG; /* file? */
  408. fattr->cf_dtype = DT_REG;
  409. rc = -EOPNOTSUPP;
  410. }
  411. } else {
  412. fattr->cf_mode |= S_IFREG; /* then it is a file */
  413. fattr->cf_dtype = DT_REG;
  414. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  415. }
  416. CIFSSMBClose(xid, tcon, netfid);
  417. }
  418. cifs_put_tlink(tlink);
  419. return rc;
  420. }
  421. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  422. /*
  423. * Fetch mode bits as provided by SFU.
  424. *
  425. * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
  426. */
  427. static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
  428. struct cifs_sb_info *cifs_sb, unsigned int xid)
  429. {
  430. #ifdef CONFIG_CIFS_XATTR
  431. ssize_t rc;
  432. char ea_value[4];
  433. __u32 mode;
  434. struct tcon_link *tlink;
  435. struct cifs_tcon *tcon;
  436. tlink = cifs_sb_tlink(cifs_sb);
  437. if (IS_ERR(tlink))
  438. return PTR_ERR(tlink);
  439. tcon = tlink_tcon(tlink);
  440. rc = CIFSSMBQAllEAs(xid, tcon, path, "SETFILEBITS",
  441. ea_value, 4 /* size of buf */, cifs_sb->local_nls,
  442. cifs_sb->mnt_cifs_flags &
  443. CIFS_MOUNT_MAP_SPECIAL_CHR);
  444. cifs_put_tlink(tlink);
  445. if (rc < 0)
  446. return (int)rc;
  447. else if (rc > 3) {
  448. mode = le32_to_cpu(*((__le32 *)ea_value));
  449. fattr->cf_mode &= ~SFBITS_MASK;
  450. cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
  451. mode, fattr->cf_mode);
  452. fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
  453. cifs_dbg(FYI, "special mode bits 0%o\n", mode);
  454. }
  455. return 0;
  456. #else
  457. return -EOPNOTSUPP;
  458. #endif
  459. }
  460. /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
  461. static void
  462. cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
  463. struct cifs_sb_info *cifs_sb, bool adjust_tz)
  464. {
  465. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  466. memset(fattr, 0, sizeof(*fattr));
  467. fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
  468. if (info->DeletePending)
  469. fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
  470. if (info->LastAccessTime)
  471. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  472. else
  473. fattr->cf_atime = CURRENT_TIME;
  474. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  475. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  476. if (adjust_tz) {
  477. fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
  478. fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
  479. }
  480. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  481. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  482. fattr->cf_createtime = le64_to_cpu(info->CreationTime);
  483. if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  484. fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
  485. fattr->cf_dtype = DT_DIR;
  486. /*
  487. * Server can return wrong NumberOfLinks value for directories
  488. * when Unix extensions are disabled - fake it.
  489. */
  490. fattr->cf_nlink = 2;
  491. } else {
  492. fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
  493. fattr->cf_dtype = DT_REG;
  494. /* clear write bits if ATTR_READONLY is set */
  495. if (fattr->cf_cifsattrs & ATTR_READONLY)
  496. fattr->cf_mode &= ~(S_IWUGO);
  497. fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
  498. if (fattr->cf_nlink < 1) {
  499. cifs_dbg(1, "replacing bogus file nlink value %u\n",
  500. fattr->cf_nlink);
  501. fattr->cf_nlink = 1;
  502. }
  503. }
  504. fattr->cf_uid = cifs_sb->mnt_uid;
  505. fattr->cf_gid = cifs_sb->mnt_gid;
  506. }
  507. static int
  508. cifs_get_file_info(struct file *filp)
  509. {
  510. int rc;
  511. unsigned int xid;
  512. FILE_ALL_INFO find_data;
  513. struct cifs_fattr fattr;
  514. struct inode *inode = file_inode(filp);
  515. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  516. struct cifsFileInfo *cfile = filp->private_data;
  517. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  518. struct TCP_Server_Info *server = tcon->ses->server;
  519. if (!server->ops->query_file_info)
  520. return -ENOSYS;
  521. xid = get_xid();
  522. rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
  523. switch (rc) {
  524. case 0:
  525. cifs_all_info_to_fattr(&fattr, &find_data, cifs_sb, false);
  526. break;
  527. case -EREMOTE:
  528. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  529. rc = 0;
  530. break;
  531. case -EOPNOTSUPP:
  532. case -EINVAL:
  533. /*
  534. * FIXME: legacy server -- fall back to path-based call?
  535. * for now, just skip revalidating and mark inode for
  536. * immediate reval.
  537. */
  538. rc = 0;
  539. CIFS_I(inode)->time = 0;
  540. default:
  541. goto cgfi_exit;
  542. }
  543. /*
  544. * don't bother with SFU junk here -- just mark inode as needing
  545. * revalidation.
  546. */
  547. fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
  548. fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
  549. cifs_fattr_to_inode(inode, &fattr);
  550. cgfi_exit:
  551. free_xid(xid);
  552. return rc;
  553. }
  554. int
  555. cifs_get_inode_info(struct inode **inode, const char *full_path,
  556. FILE_ALL_INFO *data, struct super_block *sb, int xid,
  557. const __u16 *fid)
  558. {
  559. bool validinum = false;
  560. __u16 srchflgs;
  561. int rc = 0, tmprc = ENOSYS;
  562. struct cifs_tcon *tcon;
  563. struct TCP_Server_Info *server;
  564. struct tcon_link *tlink;
  565. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  566. char *buf = NULL;
  567. bool adjust_tz = false;
  568. struct cifs_fattr fattr;
  569. struct cifs_search_info *srchinf = NULL;
  570. tlink = cifs_sb_tlink(cifs_sb);
  571. if (IS_ERR(tlink))
  572. return PTR_ERR(tlink);
  573. tcon = tlink_tcon(tlink);
  574. server = tcon->ses->server;
  575. cifs_dbg(FYI, "Getting info on %s\n", full_path);
  576. if ((data == NULL) && (*inode != NULL)) {
  577. if (CIFS_I(*inode)->clientCanCacheRead) {
  578. cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
  579. goto cgii_exit;
  580. }
  581. }
  582. /* if inode info is not passed, get it from server */
  583. if (data == NULL) {
  584. if (!server->ops->query_path_info) {
  585. rc = -ENOSYS;
  586. goto cgii_exit;
  587. }
  588. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  589. if (buf == NULL) {
  590. rc = -ENOMEM;
  591. goto cgii_exit;
  592. }
  593. data = (FILE_ALL_INFO *)buf;
  594. rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
  595. data, &adjust_tz);
  596. }
  597. if (!rc) {
  598. cifs_all_info_to_fattr(&fattr, (FILE_ALL_INFO *)data, cifs_sb,
  599. adjust_tz);
  600. } else if (rc == -EREMOTE) {
  601. cifs_create_dfs_fattr(&fattr, sb);
  602. rc = 0;
  603. } else if (rc == -EACCES && backup_cred(cifs_sb)) {
  604. srchinf = kzalloc(sizeof(struct cifs_search_info),
  605. GFP_KERNEL);
  606. if (srchinf == NULL) {
  607. rc = -ENOMEM;
  608. goto cgii_exit;
  609. }
  610. srchinf->endOfSearch = false;
  611. srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  612. srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
  613. CIFS_SEARCH_CLOSE_AT_END |
  614. CIFS_SEARCH_BACKUP_SEARCH;
  615. rc = CIFSFindFirst(xid, tcon, full_path,
  616. cifs_sb, NULL, srchflgs, srchinf, false);
  617. if (!rc) {
  618. data =
  619. (FILE_ALL_INFO *)srchinf->srch_entries_start;
  620. cifs_dir_info_to_fattr(&fattr,
  621. (FILE_DIRECTORY_INFO *)data, cifs_sb);
  622. fattr.cf_uniqueid = le64_to_cpu(
  623. ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
  624. validinum = true;
  625. cifs_buf_release(srchinf->ntwrk_buf_start);
  626. }
  627. kfree(srchinf);
  628. } else
  629. goto cgii_exit;
  630. /*
  631. * If an inode wasn't passed in, then get the inode number
  632. *
  633. * Is an i_ino of zero legal? Can we use that to check if the server
  634. * supports returning inode numbers? Are there other sanity checks we
  635. * can use to ensure that the server is really filling in that field?
  636. */
  637. if (*inode == NULL) {
  638. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  639. if (validinum == false) {
  640. if (server->ops->get_srv_inum)
  641. tmprc = server->ops->get_srv_inum(xid,
  642. tcon, cifs_sb, full_path,
  643. &fattr.cf_uniqueid, data);
  644. if (tmprc) {
  645. cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
  646. tmprc);
  647. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  648. cifs_autodisable_serverino(cifs_sb);
  649. }
  650. }
  651. } else
  652. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  653. } else
  654. fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
  655. /* query for SFU type info if supported and needed */
  656. if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
  657. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  658. tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
  659. if (tmprc)
  660. cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
  661. }
  662. #ifdef CONFIG_CIFS_ACL
  663. /* fill in 0777 bits from ACL */
  664. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  665. rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
  666. if (rc) {
  667. cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
  668. __func__, rc);
  669. goto cgii_exit;
  670. }
  671. }
  672. #endif /* CONFIG_CIFS_ACL */
  673. /* fill in remaining high mode bits e.g. SUID, VTX */
  674. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  675. cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
  676. /* check for Minshall+French symlinks */
  677. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  678. tmprc = CIFSCheckMFSymlink(&fattr, full_path, cifs_sb, xid);
  679. if (tmprc)
  680. cifs_dbg(FYI, "CIFSCheckMFSymlink: %d\n", tmprc);
  681. }
  682. if (!*inode) {
  683. *inode = cifs_iget(sb, &fattr);
  684. if (!*inode)
  685. rc = -ENOMEM;
  686. } else {
  687. cifs_fattr_to_inode(*inode, &fattr);
  688. }
  689. cgii_exit:
  690. kfree(buf);
  691. cifs_put_tlink(tlink);
  692. return rc;
  693. }
  694. static const struct inode_operations cifs_ipc_inode_ops = {
  695. .lookup = cifs_lookup,
  696. };
  697. static int
  698. cifs_find_inode(struct inode *inode, void *opaque)
  699. {
  700. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  701. /* don't match inode with different uniqueid */
  702. if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
  703. return 0;
  704. /* use createtime like an i_generation field */
  705. if (CIFS_I(inode)->createtime != fattr->cf_createtime)
  706. return 0;
  707. /* don't match inode of different type */
  708. if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
  709. return 0;
  710. /* if it's not a directory or has no dentries, then flag it */
  711. if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
  712. fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
  713. return 1;
  714. }
  715. static int
  716. cifs_init_inode(struct inode *inode, void *opaque)
  717. {
  718. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  719. CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
  720. CIFS_I(inode)->createtime = fattr->cf_createtime;
  721. return 0;
  722. }
  723. /*
  724. * walk dentry list for an inode and report whether it has aliases that
  725. * are hashed. We use this to determine if a directory inode can actually
  726. * be used.
  727. */
  728. static bool
  729. inode_has_hashed_dentries(struct inode *inode)
  730. {
  731. struct dentry *dentry;
  732. spin_lock(&inode->i_lock);
  733. hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  734. if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
  735. spin_unlock(&inode->i_lock);
  736. return true;
  737. }
  738. }
  739. spin_unlock(&inode->i_lock);
  740. return false;
  741. }
  742. /* Given fattrs, get a corresponding inode */
  743. struct inode *
  744. cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
  745. {
  746. unsigned long hash;
  747. struct inode *inode;
  748. retry_iget5_locked:
  749. cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
  750. /* hash down to 32-bits on 32-bit arch */
  751. hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
  752. inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
  753. if (inode) {
  754. /* was there a potentially problematic inode collision? */
  755. if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
  756. fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
  757. if (inode_has_hashed_dentries(inode)) {
  758. cifs_autodisable_serverino(CIFS_SB(sb));
  759. iput(inode);
  760. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  761. goto retry_iget5_locked;
  762. }
  763. }
  764. cifs_fattr_to_inode(inode, fattr);
  765. if (sb->s_flags & MS_NOATIME)
  766. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  767. if (inode->i_state & I_NEW) {
  768. inode->i_ino = hash;
  769. if (S_ISREG(inode->i_mode))
  770. inode->i_data.backing_dev_info = sb->s_bdi;
  771. #ifdef CONFIG_CIFS_FSCACHE
  772. /* initialize per-inode cache cookie pointer */
  773. CIFS_I(inode)->fscache = NULL;
  774. #endif
  775. unlock_new_inode(inode);
  776. }
  777. }
  778. return inode;
  779. }
  780. /* gets root inode */
  781. struct inode *cifs_root_iget(struct super_block *sb)
  782. {
  783. unsigned int xid;
  784. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  785. struct inode *inode = NULL;
  786. long rc;
  787. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  788. xid = get_xid();
  789. if (tcon->unix_ext)
  790. rc = cifs_get_inode_info_unix(&inode, "", sb, xid);
  791. else
  792. rc = cifs_get_inode_info(&inode, "", NULL, sb, xid, NULL);
  793. if (!inode) {
  794. inode = ERR_PTR(rc);
  795. goto out;
  796. }
  797. #ifdef CONFIG_CIFS_FSCACHE
  798. /* populate tcon->resource_id */
  799. tcon->resource_id = CIFS_I(inode)->uniqueid;
  800. #endif
  801. if (rc && tcon->ipc) {
  802. cifs_dbg(FYI, "ipc connection - fake read inode\n");
  803. spin_lock(&inode->i_lock);
  804. inode->i_mode |= S_IFDIR;
  805. set_nlink(inode, 2);
  806. inode->i_op = &cifs_ipc_inode_ops;
  807. inode->i_fop = &simple_dir_operations;
  808. inode->i_uid = cifs_sb->mnt_uid;
  809. inode->i_gid = cifs_sb->mnt_gid;
  810. spin_unlock(&inode->i_lock);
  811. } else if (rc) {
  812. iget_failed(inode);
  813. inode = ERR_PTR(rc);
  814. }
  815. out:
  816. /* can not call macro free_xid here since in a void func
  817. * TODO: This is no longer true
  818. */
  819. _free_xid(xid);
  820. return inode;
  821. }
  822. int
  823. cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
  824. char *full_path, __u32 dosattr)
  825. {
  826. bool set_time = false;
  827. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  828. struct TCP_Server_Info *server;
  829. FILE_BASIC_INFO info_buf;
  830. if (attrs == NULL)
  831. return -EINVAL;
  832. server = cifs_sb_master_tcon(cifs_sb)->ses->server;
  833. if (!server->ops->set_file_info)
  834. return -ENOSYS;
  835. if (attrs->ia_valid & ATTR_ATIME) {
  836. set_time = true;
  837. info_buf.LastAccessTime =
  838. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  839. } else
  840. info_buf.LastAccessTime = 0;
  841. if (attrs->ia_valid & ATTR_MTIME) {
  842. set_time = true;
  843. info_buf.LastWriteTime =
  844. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  845. } else
  846. info_buf.LastWriteTime = 0;
  847. /*
  848. * Samba throws this field away, but windows may actually use it.
  849. * Do not set ctime unless other time stamps are changed explicitly
  850. * (i.e. by utimes()) since we would then have a mix of client and
  851. * server times.
  852. */
  853. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  854. cifs_dbg(FYI, "CIFS - CTIME changed\n");
  855. info_buf.ChangeTime =
  856. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  857. } else
  858. info_buf.ChangeTime = 0;
  859. info_buf.CreationTime = 0; /* don't change */
  860. info_buf.Attributes = cpu_to_le32(dosattr);
  861. return server->ops->set_file_info(inode, full_path, &info_buf, xid);
  862. }
  863. /*
  864. * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
  865. * and rename it to a random name that hopefully won't conflict with
  866. * anything else.
  867. */
  868. int
  869. cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
  870. const unsigned int xid)
  871. {
  872. int oplock = 0;
  873. int rc;
  874. __u16 netfid;
  875. struct inode *inode = dentry->d_inode;
  876. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  877. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  878. struct tcon_link *tlink;
  879. struct cifs_tcon *tcon;
  880. __u32 dosattr, origattr;
  881. FILE_BASIC_INFO *info_buf = NULL;
  882. tlink = cifs_sb_tlink(cifs_sb);
  883. if (IS_ERR(tlink))
  884. return PTR_ERR(tlink);
  885. tcon = tlink_tcon(tlink);
  886. /*
  887. * We cannot rename the file if the server doesn't support
  888. * CAP_INFOLEVEL_PASSTHRU
  889. */
  890. if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
  891. rc = -EBUSY;
  892. goto out;
  893. }
  894. rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
  895. DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
  896. &netfid, &oplock, NULL, cifs_sb->local_nls,
  897. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  898. if (rc != 0)
  899. goto out;
  900. origattr = cifsInode->cifsAttrs;
  901. if (origattr == 0)
  902. origattr |= ATTR_NORMAL;
  903. dosattr = origattr & ~ATTR_READONLY;
  904. if (dosattr == 0)
  905. dosattr |= ATTR_NORMAL;
  906. dosattr |= ATTR_HIDDEN;
  907. /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
  908. if (dosattr != origattr) {
  909. info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
  910. if (info_buf == NULL) {
  911. rc = -ENOMEM;
  912. goto out_close;
  913. }
  914. info_buf->Attributes = cpu_to_le32(dosattr);
  915. rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
  916. current->tgid);
  917. /* although we would like to mark the file hidden
  918. if that fails we will still try to rename it */
  919. if (!rc)
  920. cifsInode->cifsAttrs = dosattr;
  921. else
  922. dosattr = origattr; /* since not able to change them */
  923. }
  924. /* rename the file */
  925. rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
  926. cifs_sb->mnt_cifs_flags &
  927. CIFS_MOUNT_MAP_SPECIAL_CHR);
  928. if (rc != 0) {
  929. rc = -EBUSY;
  930. goto undo_setattr;
  931. }
  932. /* try to set DELETE_ON_CLOSE */
  933. if (!cifsInode->delete_pending) {
  934. rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
  935. current->tgid);
  936. /*
  937. * some samba versions return -ENOENT when we try to set the
  938. * file disposition here. Likely a samba bug, but work around
  939. * it for now. This means that some cifsXXX files may hang
  940. * around after they shouldn't.
  941. *
  942. * BB: remove this hack after more servers have the fix
  943. */
  944. if (rc == -ENOENT)
  945. rc = 0;
  946. else if (rc != 0) {
  947. rc = -EBUSY;
  948. goto undo_rename;
  949. }
  950. cifsInode->delete_pending = true;
  951. }
  952. out_close:
  953. CIFSSMBClose(xid, tcon, netfid);
  954. out:
  955. kfree(info_buf);
  956. cifs_put_tlink(tlink);
  957. return rc;
  958. /*
  959. * reset everything back to the original state. Don't bother
  960. * dealing with errors here since we can't do anything about
  961. * them anyway.
  962. */
  963. undo_rename:
  964. CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
  965. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  966. CIFS_MOUNT_MAP_SPECIAL_CHR);
  967. undo_setattr:
  968. if (dosattr != origattr) {
  969. info_buf->Attributes = cpu_to_le32(origattr);
  970. if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
  971. current->tgid))
  972. cifsInode->cifsAttrs = origattr;
  973. }
  974. goto out_close;
  975. }
  976. /* copied from fs/nfs/dir.c with small changes */
  977. static void
  978. cifs_drop_nlink(struct inode *inode)
  979. {
  980. spin_lock(&inode->i_lock);
  981. if (inode->i_nlink > 0)
  982. drop_nlink(inode);
  983. spin_unlock(&inode->i_lock);
  984. }
  985. /*
  986. * If dentry->d_inode is null (usually meaning the cached dentry
  987. * is a negative dentry) then we would attempt a standard SMB delete, but
  988. * if that fails we can not attempt the fall back mechanisms on EACCESS
  989. * but will return the EACCESS to the caller. Note that the VFS does not call
  990. * unlink on negative dentries currently.
  991. */
  992. int cifs_unlink(struct inode *dir, struct dentry *dentry)
  993. {
  994. int rc = 0;
  995. unsigned int xid;
  996. char *full_path = NULL;
  997. struct inode *inode = dentry->d_inode;
  998. struct cifsInodeInfo *cifs_inode;
  999. struct super_block *sb = dir->i_sb;
  1000. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  1001. struct tcon_link *tlink;
  1002. struct cifs_tcon *tcon;
  1003. struct TCP_Server_Info *server;
  1004. struct iattr *attrs = NULL;
  1005. __u32 dosattr = 0, origattr = 0;
  1006. cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
  1007. tlink = cifs_sb_tlink(cifs_sb);
  1008. if (IS_ERR(tlink))
  1009. return PTR_ERR(tlink);
  1010. tcon = tlink_tcon(tlink);
  1011. server = tcon->ses->server;
  1012. xid = get_xid();
  1013. /* Unlink can be called from rename so we can not take the
  1014. * sb->s_vfs_rename_mutex here */
  1015. full_path = build_path_from_dentry(dentry);
  1016. if (full_path == NULL) {
  1017. rc = -ENOMEM;
  1018. goto unlink_out;
  1019. }
  1020. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1021. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1022. rc = CIFSPOSIXDelFile(xid, tcon, full_path,
  1023. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  1024. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  1025. cifs_dbg(FYI, "posix del rc %d\n", rc);
  1026. if ((rc == 0) || (rc == -ENOENT))
  1027. goto psx_del_no_retry;
  1028. }
  1029. retry_std_delete:
  1030. if (!server->ops->unlink) {
  1031. rc = -ENOSYS;
  1032. goto psx_del_no_retry;
  1033. }
  1034. rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
  1035. psx_del_no_retry:
  1036. if (!rc) {
  1037. if (inode)
  1038. cifs_drop_nlink(inode);
  1039. } else if (rc == -ENOENT) {
  1040. d_drop(dentry);
  1041. } else if (rc == -EBUSY) {
  1042. if (server->ops->rename_pending_delete) {
  1043. rc = server->ops->rename_pending_delete(full_path,
  1044. dentry, xid);
  1045. if (rc == 0)
  1046. cifs_drop_nlink(inode);
  1047. }
  1048. } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
  1049. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  1050. if (attrs == NULL) {
  1051. rc = -ENOMEM;
  1052. goto out_reval;
  1053. }
  1054. /* try to reset dos attributes */
  1055. cifs_inode = CIFS_I(inode);
  1056. origattr = cifs_inode->cifsAttrs;
  1057. if (origattr == 0)
  1058. origattr |= ATTR_NORMAL;
  1059. dosattr = origattr & ~ATTR_READONLY;
  1060. if (dosattr == 0)
  1061. dosattr |= ATTR_NORMAL;
  1062. dosattr |= ATTR_HIDDEN;
  1063. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1064. if (rc != 0)
  1065. goto out_reval;
  1066. goto retry_std_delete;
  1067. }
  1068. /* undo the setattr if we errored out and it's needed */
  1069. if (rc != 0 && dosattr != 0)
  1070. cifs_set_file_info(inode, attrs, xid, full_path, origattr);
  1071. out_reval:
  1072. if (inode) {
  1073. cifs_inode = CIFS_I(inode);
  1074. cifs_inode->time = 0; /* will force revalidate to get info
  1075. when needed */
  1076. inode->i_ctime = current_fs_time(sb);
  1077. }
  1078. dir->i_ctime = dir->i_mtime = current_fs_time(sb);
  1079. cifs_inode = CIFS_I(dir);
  1080. CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
  1081. unlink_out:
  1082. kfree(full_path);
  1083. kfree(attrs);
  1084. free_xid(xid);
  1085. cifs_put_tlink(tlink);
  1086. return rc;
  1087. }
  1088. static int
  1089. cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
  1090. const char *full_path, struct cifs_sb_info *cifs_sb,
  1091. struct cifs_tcon *tcon, const unsigned int xid)
  1092. {
  1093. int rc = 0;
  1094. struct inode *inode = NULL;
  1095. if (tcon->unix_ext)
  1096. rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
  1097. xid);
  1098. else
  1099. rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
  1100. xid, NULL);
  1101. if (rc)
  1102. return rc;
  1103. /*
  1104. * setting nlink not necessary except in cases where we failed to get it
  1105. * from the server or was set bogus. Also, since this is a brand new
  1106. * inode, no need to grab the i_lock before setting the i_nlink.
  1107. */
  1108. if (inode->i_nlink < 2)
  1109. set_nlink(inode, 2);
  1110. mode &= ~current_umask();
  1111. /* must turn on setgid bit if parent dir has it */
  1112. if (parent->i_mode & S_ISGID)
  1113. mode |= S_ISGID;
  1114. if (tcon->unix_ext) {
  1115. struct cifs_unix_set_info_args args = {
  1116. .mode = mode,
  1117. .ctime = NO_CHANGE_64,
  1118. .atime = NO_CHANGE_64,
  1119. .mtime = NO_CHANGE_64,
  1120. .device = 0,
  1121. };
  1122. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1123. args.uid = current_fsuid();
  1124. if (parent->i_mode & S_ISGID)
  1125. args.gid = parent->i_gid;
  1126. else
  1127. args.gid = current_fsgid();
  1128. } else {
  1129. args.uid = INVALID_UID; /* no change */
  1130. args.gid = INVALID_GID; /* no change */
  1131. }
  1132. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  1133. cifs_sb->local_nls,
  1134. cifs_sb->mnt_cifs_flags &
  1135. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1136. } else {
  1137. struct TCP_Server_Info *server = tcon->ses->server;
  1138. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1139. (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
  1140. server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
  1141. tcon, xid);
  1142. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  1143. inode->i_mode = (mode | S_IFDIR);
  1144. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1145. inode->i_uid = current_fsuid();
  1146. if (inode->i_mode & S_ISGID)
  1147. inode->i_gid = parent->i_gid;
  1148. else
  1149. inode->i_gid = current_fsgid();
  1150. }
  1151. }
  1152. d_instantiate(dentry, inode);
  1153. return rc;
  1154. }
  1155. static int
  1156. cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
  1157. const char *full_path, struct cifs_sb_info *cifs_sb,
  1158. struct cifs_tcon *tcon, const unsigned int xid)
  1159. {
  1160. int rc = 0;
  1161. u32 oplock = 0;
  1162. FILE_UNIX_BASIC_INFO *info = NULL;
  1163. struct inode *newinode = NULL;
  1164. struct cifs_fattr fattr;
  1165. info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  1166. if (info == NULL) {
  1167. rc = -ENOMEM;
  1168. goto posix_mkdir_out;
  1169. }
  1170. mode &= ~current_umask();
  1171. rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
  1172. NULL /* netfid */, info, &oplock, full_path,
  1173. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  1174. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1175. if (rc == -EOPNOTSUPP)
  1176. goto posix_mkdir_out;
  1177. else if (rc) {
  1178. cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
  1179. d_drop(dentry);
  1180. goto posix_mkdir_out;
  1181. }
  1182. if (info->Type == cpu_to_le32(-1))
  1183. /* no return info, go query for it */
  1184. goto posix_mkdir_get_info;
  1185. /*
  1186. * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
  1187. * need to set uid/gid.
  1188. */
  1189. cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
  1190. cifs_fill_uniqueid(inode->i_sb, &fattr);
  1191. newinode = cifs_iget(inode->i_sb, &fattr);
  1192. if (!newinode)
  1193. goto posix_mkdir_get_info;
  1194. d_instantiate(dentry, newinode);
  1195. #ifdef CONFIG_CIFS_DEBUG2
  1196. cifs_dbg(FYI, "instantiated dentry %p %s to inode %p\n",
  1197. dentry, dentry->d_name.name, newinode);
  1198. if (newinode->i_nlink != 2)
  1199. cifs_dbg(FYI, "unexpected number of links %d\n",
  1200. newinode->i_nlink);
  1201. #endif
  1202. posix_mkdir_out:
  1203. kfree(info);
  1204. return rc;
  1205. posix_mkdir_get_info:
  1206. rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
  1207. xid);
  1208. goto posix_mkdir_out;
  1209. }
  1210. int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
  1211. {
  1212. int rc = 0;
  1213. unsigned int xid;
  1214. struct cifs_sb_info *cifs_sb;
  1215. struct tcon_link *tlink;
  1216. struct cifs_tcon *tcon;
  1217. struct TCP_Server_Info *server;
  1218. char *full_path;
  1219. cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
  1220. mode, inode);
  1221. cifs_sb = CIFS_SB(inode->i_sb);
  1222. tlink = cifs_sb_tlink(cifs_sb);
  1223. if (IS_ERR(tlink))
  1224. return PTR_ERR(tlink);
  1225. tcon = tlink_tcon(tlink);
  1226. xid = get_xid();
  1227. full_path = build_path_from_dentry(direntry);
  1228. if (full_path == NULL) {
  1229. rc = -ENOMEM;
  1230. goto mkdir_out;
  1231. }
  1232. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1233. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1234. rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
  1235. tcon, xid);
  1236. if (rc != -EOPNOTSUPP)
  1237. goto mkdir_out;
  1238. }
  1239. server = tcon->ses->server;
  1240. if (!server->ops->mkdir) {
  1241. rc = -ENOSYS;
  1242. goto mkdir_out;
  1243. }
  1244. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  1245. rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
  1246. if (rc) {
  1247. cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
  1248. d_drop(direntry);
  1249. goto mkdir_out;
  1250. }
  1251. rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
  1252. xid);
  1253. mkdir_out:
  1254. /*
  1255. * Force revalidate to get parent dir info when needed since cached
  1256. * attributes are invalid now.
  1257. */
  1258. CIFS_I(inode)->time = 0;
  1259. kfree(full_path);
  1260. free_xid(xid);
  1261. cifs_put_tlink(tlink);
  1262. return rc;
  1263. }
  1264. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  1265. {
  1266. int rc = 0;
  1267. unsigned int xid;
  1268. struct cifs_sb_info *cifs_sb;
  1269. struct tcon_link *tlink;
  1270. struct cifs_tcon *tcon;
  1271. struct TCP_Server_Info *server;
  1272. char *full_path = NULL;
  1273. struct cifsInodeInfo *cifsInode;
  1274. cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
  1275. xid = get_xid();
  1276. full_path = build_path_from_dentry(direntry);
  1277. if (full_path == NULL) {
  1278. rc = -ENOMEM;
  1279. goto rmdir_exit;
  1280. }
  1281. cifs_sb = CIFS_SB(inode->i_sb);
  1282. tlink = cifs_sb_tlink(cifs_sb);
  1283. if (IS_ERR(tlink)) {
  1284. rc = PTR_ERR(tlink);
  1285. goto rmdir_exit;
  1286. }
  1287. tcon = tlink_tcon(tlink);
  1288. server = tcon->ses->server;
  1289. if (!server->ops->rmdir) {
  1290. rc = -ENOSYS;
  1291. cifs_put_tlink(tlink);
  1292. goto rmdir_exit;
  1293. }
  1294. rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
  1295. cifs_put_tlink(tlink);
  1296. if (!rc) {
  1297. spin_lock(&direntry->d_inode->i_lock);
  1298. i_size_write(direntry->d_inode, 0);
  1299. clear_nlink(direntry->d_inode);
  1300. spin_unlock(&direntry->d_inode->i_lock);
  1301. }
  1302. cifsInode = CIFS_I(direntry->d_inode);
  1303. /* force revalidate to go get info when needed */
  1304. cifsInode->time = 0;
  1305. cifsInode = CIFS_I(inode);
  1306. /*
  1307. * Force revalidate to get parent dir info when needed since cached
  1308. * attributes are invalid now.
  1309. */
  1310. cifsInode->time = 0;
  1311. direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
  1312. current_fs_time(inode->i_sb);
  1313. rmdir_exit:
  1314. kfree(full_path);
  1315. free_xid(xid);
  1316. return rc;
  1317. }
  1318. static int
  1319. cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
  1320. const char *from_path, struct dentry *to_dentry,
  1321. const char *to_path)
  1322. {
  1323. struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
  1324. struct tcon_link *tlink;
  1325. struct cifs_tcon *tcon;
  1326. struct TCP_Server_Info *server;
  1327. __u16 srcfid;
  1328. int oplock, rc;
  1329. tlink = cifs_sb_tlink(cifs_sb);
  1330. if (IS_ERR(tlink))
  1331. return PTR_ERR(tlink);
  1332. tcon = tlink_tcon(tlink);
  1333. server = tcon->ses->server;
  1334. if (!server->ops->rename)
  1335. return -ENOSYS;
  1336. /* try path-based rename first */
  1337. rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
  1338. /*
  1339. * Don't bother with rename by filehandle unless file is busy and
  1340. * source. Note that cross directory moves do not work with
  1341. * rename by filehandle to various Windows servers.
  1342. */
  1343. if (rc == 0 || rc != -EBUSY)
  1344. goto do_rename_exit;
  1345. /* open-file renames don't work across directories */
  1346. if (to_dentry->d_parent != from_dentry->d_parent)
  1347. goto do_rename_exit;
  1348. /* open the file to be renamed -- we need DELETE perms */
  1349. rc = CIFSSMBOpen(xid, tcon, from_path, FILE_OPEN, DELETE,
  1350. CREATE_NOT_DIR, &srcfid, &oplock, NULL,
  1351. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  1352. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1353. if (rc == 0) {
  1354. rc = CIFSSMBRenameOpenFile(xid, tcon, srcfid,
  1355. (const char *) to_dentry->d_name.name,
  1356. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  1357. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1358. CIFSSMBClose(xid, tcon, srcfid);
  1359. }
  1360. do_rename_exit:
  1361. cifs_put_tlink(tlink);
  1362. return rc;
  1363. }
  1364. int
  1365. cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
  1366. struct inode *target_dir, struct dentry *target_dentry)
  1367. {
  1368. char *from_name = NULL;
  1369. char *to_name = NULL;
  1370. struct cifs_sb_info *cifs_sb;
  1371. struct tcon_link *tlink;
  1372. struct cifs_tcon *tcon;
  1373. FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
  1374. FILE_UNIX_BASIC_INFO *info_buf_target;
  1375. unsigned int xid;
  1376. int rc, tmprc;
  1377. cifs_sb = CIFS_SB(source_dir->i_sb);
  1378. tlink = cifs_sb_tlink(cifs_sb);
  1379. if (IS_ERR(tlink))
  1380. return PTR_ERR(tlink);
  1381. tcon = tlink_tcon(tlink);
  1382. xid = get_xid();
  1383. /*
  1384. * we already have the rename sem so we do not need to
  1385. * grab it again here to protect the path integrity
  1386. */
  1387. from_name = build_path_from_dentry(source_dentry);
  1388. if (from_name == NULL) {
  1389. rc = -ENOMEM;
  1390. goto cifs_rename_exit;
  1391. }
  1392. to_name = build_path_from_dentry(target_dentry);
  1393. if (to_name == NULL) {
  1394. rc = -ENOMEM;
  1395. goto cifs_rename_exit;
  1396. }
  1397. rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
  1398. to_name);
  1399. if (rc == -EEXIST && tcon->unix_ext) {
  1400. /*
  1401. * Are src and dst hardlinks of same inode? We can only tell
  1402. * with unix extensions enabled.
  1403. */
  1404. info_buf_source =
  1405. kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
  1406. GFP_KERNEL);
  1407. if (info_buf_source == NULL) {
  1408. rc = -ENOMEM;
  1409. goto cifs_rename_exit;
  1410. }
  1411. info_buf_target = info_buf_source + 1;
  1412. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
  1413. info_buf_source,
  1414. cifs_sb->local_nls,
  1415. cifs_sb->mnt_cifs_flags &
  1416. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1417. if (tmprc != 0)
  1418. goto unlink_target;
  1419. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
  1420. info_buf_target,
  1421. cifs_sb->local_nls,
  1422. cifs_sb->mnt_cifs_flags &
  1423. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1424. if (tmprc == 0 && (info_buf_source->UniqueId ==
  1425. info_buf_target->UniqueId)) {
  1426. /* same file, POSIX says that this is a noop */
  1427. rc = 0;
  1428. goto cifs_rename_exit;
  1429. }
  1430. }
  1431. /*
  1432. * else ... BB we could add the same check for Windows by
  1433. * checking the UniqueId via FILE_INTERNAL_INFO
  1434. */
  1435. unlink_target:
  1436. /* Try unlinking the target dentry if it's not negative */
  1437. if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) {
  1438. tmprc = cifs_unlink(target_dir, target_dentry);
  1439. if (tmprc)
  1440. goto cifs_rename_exit;
  1441. rc = cifs_do_rename(xid, source_dentry, from_name,
  1442. target_dentry, to_name);
  1443. }
  1444. cifs_rename_exit:
  1445. kfree(info_buf_source);
  1446. kfree(from_name);
  1447. kfree(to_name);
  1448. free_xid(xid);
  1449. cifs_put_tlink(tlink);
  1450. return rc;
  1451. }
  1452. static bool
  1453. cifs_inode_needs_reval(struct inode *inode)
  1454. {
  1455. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  1456. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1457. if (cifs_i->clientCanCacheRead)
  1458. return false;
  1459. if (!lookupCacheEnabled)
  1460. return true;
  1461. if (cifs_i->time == 0)
  1462. return true;
  1463. if (!time_in_range(jiffies, cifs_i->time,
  1464. cifs_i->time + cifs_sb->actimeo))
  1465. return true;
  1466. /* hardlinked files w/ noserverino get "special" treatment */
  1467. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
  1468. S_ISREG(inode->i_mode) && inode->i_nlink != 1)
  1469. return true;
  1470. return false;
  1471. }
  1472. /*
  1473. * Zap the cache. Called when invalid_mapping flag is set.
  1474. */
  1475. int
  1476. cifs_invalidate_mapping(struct inode *inode)
  1477. {
  1478. int rc = 0;
  1479. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  1480. cifs_i->invalid_mapping = false;
  1481. if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
  1482. rc = invalidate_inode_pages2(inode->i_mapping);
  1483. if (rc) {
  1484. cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
  1485. __func__, inode);
  1486. cifs_i->invalid_mapping = true;
  1487. }
  1488. }
  1489. cifs_fscache_reset_inode_cookie(inode);
  1490. return rc;
  1491. }
  1492. int cifs_revalidate_file_attr(struct file *filp)
  1493. {
  1494. int rc = 0;
  1495. struct inode *inode = file_inode(filp);
  1496. struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
  1497. if (!cifs_inode_needs_reval(inode))
  1498. return rc;
  1499. if (tlink_tcon(cfile->tlink)->unix_ext)
  1500. rc = cifs_get_file_info_unix(filp);
  1501. else
  1502. rc = cifs_get_file_info(filp);
  1503. return rc;
  1504. }
  1505. int cifs_revalidate_dentry_attr(struct dentry *dentry)
  1506. {
  1507. unsigned int xid;
  1508. int rc = 0;
  1509. struct inode *inode = dentry->d_inode;
  1510. struct super_block *sb = dentry->d_sb;
  1511. char *full_path = NULL;
  1512. if (inode == NULL)
  1513. return -ENOENT;
  1514. if (!cifs_inode_needs_reval(inode))
  1515. return rc;
  1516. xid = get_xid();
  1517. /* can not safely grab the rename sem here if rename calls revalidate
  1518. since that would deadlock */
  1519. full_path = build_path_from_dentry(dentry);
  1520. if (full_path == NULL) {
  1521. rc = -ENOMEM;
  1522. goto out;
  1523. }
  1524. cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
  1525. full_path, inode, inode->i_count.counter,
  1526. dentry, dentry->d_time, jiffies);
  1527. if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
  1528. rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
  1529. else
  1530. rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
  1531. xid, NULL);
  1532. out:
  1533. kfree(full_path);
  1534. free_xid(xid);
  1535. return rc;
  1536. }
  1537. int cifs_revalidate_file(struct file *filp)
  1538. {
  1539. int rc;
  1540. struct inode *inode = file_inode(filp);
  1541. rc = cifs_revalidate_file_attr(filp);
  1542. if (rc)
  1543. return rc;
  1544. if (CIFS_I(inode)->invalid_mapping)
  1545. rc = cifs_invalidate_mapping(inode);
  1546. return rc;
  1547. }
  1548. /* revalidate a dentry's inode attributes */
  1549. int cifs_revalidate_dentry(struct dentry *dentry)
  1550. {
  1551. int rc;
  1552. struct inode *inode = dentry->d_inode;
  1553. rc = cifs_revalidate_dentry_attr(dentry);
  1554. if (rc)
  1555. return rc;
  1556. if (CIFS_I(inode)->invalid_mapping)
  1557. rc = cifs_invalidate_mapping(inode);
  1558. return rc;
  1559. }
  1560. int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1561. struct kstat *stat)
  1562. {
  1563. struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
  1564. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  1565. struct inode *inode = dentry->d_inode;
  1566. int rc;
  1567. /*
  1568. * We need to be sure that all dirty pages are written and the server
  1569. * has actual ctime, mtime and file length.
  1570. */
  1571. if (!CIFS_I(inode)->clientCanCacheRead && inode->i_mapping &&
  1572. inode->i_mapping->nrpages != 0) {
  1573. rc = filemap_fdatawait(inode->i_mapping);
  1574. if (rc) {
  1575. mapping_set_error(inode->i_mapping, rc);
  1576. return rc;
  1577. }
  1578. }
  1579. rc = cifs_revalidate_dentry_attr(dentry);
  1580. if (rc)
  1581. return rc;
  1582. generic_fillattr(inode, stat);
  1583. stat->blksize = CIFS_MAX_MSGSIZE;
  1584. stat->ino = CIFS_I(inode)->uniqueid;
  1585. /*
  1586. * If on a multiuser mount without unix extensions or cifsacl being
  1587. * enabled, and the admin hasn't overridden them, set the ownership
  1588. * to the fsuid/fsgid of the current process.
  1589. */
  1590. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
  1591. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1592. !tcon->unix_ext) {
  1593. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
  1594. stat->uid = current_fsuid();
  1595. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
  1596. stat->gid = current_fsgid();
  1597. }
  1598. return rc;
  1599. }
  1600. static int cifs_truncate_page(struct address_space *mapping, loff_t from)
  1601. {
  1602. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  1603. unsigned offset = from & (PAGE_CACHE_SIZE - 1);
  1604. struct page *page;
  1605. int rc = 0;
  1606. page = grab_cache_page(mapping, index);
  1607. if (!page)
  1608. return -ENOMEM;
  1609. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  1610. unlock_page(page);
  1611. page_cache_release(page);
  1612. return rc;
  1613. }
  1614. static void cifs_setsize(struct inode *inode, loff_t offset)
  1615. {
  1616. loff_t oldsize;
  1617. spin_lock(&inode->i_lock);
  1618. oldsize = inode->i_size;
  1619. i_size_write(inode, offset);
  1620. spin_unlock(&inode->i_lock);
  1621. truncate_pagecache(inode, oldsize, offset);
  1622. }
  1623. static int
  1624. cifs_set_file_size(struct inode *inode, struct iattr *attrs,
  1625. unsigned int xid, char *full_path)
  1626. {
  1627. int rc;
  1628. struct cifsFileInfo *open_file;
  1629. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1630. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1631. struct tcon_link *tlink = NULL;
  1632. struct cifs_tcon *tcon = NULL;
  1633. struct TCP_Server_Info *server;
  1634. struct cifs_io_parms io_parms;
  1635. /*
  1636. * To avoid spurious oplock breaks from server, in the case of
  1637. * inodes that we already have open, avoid doing path based
  1638. * setting of file size if we can do it by handle.
  1639. * This keeps our caching token (oplock) and avoids timeouts
  1640. * when the local oplock break takes longer to flush
  1641. * writebehind data than the SMB timeout for the SetPathInfo
  1642. * request would allow
  1643. */
  1644. open_file = find_writable_file(cifsInode, true);
  1645. if (open_file) {
  1646. tcon = tlink_tcon(open_file->tlink);
  1647. server = tcon->ses->server;
  1648. if (server->ops->set_file_size)
  1649. rc = server->ops->set_file_size(xid, tcon, open_file,
  1650. attrs->ia_size, false);
  1651. else
  1652. rc = -ENOSYS;
  1653. cifsFileInfo_put(open_file);
  1654. cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
  1655. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1656. unsigned int bytes_written;
  1657. io_parms.netfid = open_file->fid.netfid;
  1658. io_parms.pid = open_file->pid;
  1659. io_parms.tcon = tcon;
  1660. io_parms.offset = 0;
  1661. io_parms.length = attrs->ia_size;
  1662. rc = CIFSSMBWrite(xid, &io_parms, &bytes_written,
  1663. NULL, NULL, 1);
  1664. cifs_dbg(FYI, "Wrt seteof rc %d\n", rc);
  1665. }
  1666. } else
  1667. rc = -EINVAL;
  1668. if (!rc)
  1669. goto set_size_out;
  1670. if (tcon == NULL) {
  1671. tlink = cifs_sb_tlink(cifs_sb);
  1672. if (IS_ERR(tlink))
  1673. return PTR_ERR(tlink);
  1674. tcon = tlink_tcon(tlink);
  1675. server = tcon->ses->server;
  1676. }
  1677. /*
  1678. * Set file size by pathname rather than by handle either because no
  1679. * valid, writeable file handle for it was found or because there was
  1680. * an error setting it by handle.
  1681. */
  1682. if (server->ops->set_path_size)
  1683. rc = server->ops->set_path_size(xid, tcon, full_path,
  1684. attrs->ia_size, cifs_sb, false);
  1685. else
  1686. rc = -ENOSYS;
  1687. cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
  1688. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1689. __u16 netfid;
  1690. int oplock = 0;
  1691. rc = SMBLegacyOpen(xid, tcon, full_path, FILE_OPEN,
  1692. GENERIC_WRITE, CREATE_NOT_DIR, &netfid,
  1693. &oplock, NULL, cifs_sb->local_nls,
  1694. cifs_sb->mnt_cifs_flags &
  1695. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1696. if (rc == 0) {
  1697. unsigned int bytes_written;
  1698. io_parms.netfid = netfid;
  1699. io_parms.pid = current->tgid;
  1700. io_parms.tcon = tcon;
  1701. io_parms.offset = 0;
  1702. io_parms.length = attrs->ia_size;
  1703. rc = CIFSSMBWrite(xid, &io_parms, &bytes_written, NULL,
  1704. NULL, 1);
  1705. cifs_dbg(FYI, "wrt seteof rc %d\n", rc);
  1706. CIFSSMBClose(xid, tcon, netfid);
  1707. }
  1708. }
  1709. if (tlink)
  1710. cifs_put_tlink(tlink);
  1711. set_size_out:
  1712. if (rc == 0) {
  1713. cifsInode->server_eof = attrs->ia_size;
  1714. cifs_setsize(inode, attrs->ia_size);
  1715. cifs_truncate_page(inode->i_mapping, inode->i_size);
  1716. }
  1717. return rc;
  1718. }
  1719. static int
  1720. cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
  1721. {
  1722. int rc;
  1723. unsigned int xid;
  1724. char *full_path = NULL;
  1725. struct inode *inode = direntry->d_inode;
  1726. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1727. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1728. struct tcon_link *tlink;
  1729. struct cifs_tcon *pTcon;
  1730. struct cifs_unix_set_info_args *args = NULL;
  1731. struct cifsFileInfo *open_file;
  1732. cifs_dbg(FYI, "setattr_unix on file %s attrs->ia_valid=0x%x\n",
  1733. direntry->d_name.name, attrs->ia_valid);
  1734. xid = get_xid();
  1735. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  1736. attrs->ia_valid |= ATTR_FORCE;
  1737. rc = inode_change_ok(inode, attrs);
  1738. if (rc < 0)
  1739. goto out;
  1740. full_path = build_path_from_dentry(direntry);
  1741. if (full_path == NULL) {
  1742. rc = -ENOMEM;
  1743. goto out;
  1744. }
  1745. /*
  1746. * Attempt to flush data before changing attributes. We need to do
  1747. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  1748. * ownership or mode then we may also need to do this. Here, we take
  1749. * the safe way out and just do the flush on all setattr requests. If
  1750. * the flush returns error, store it to report later and continue.
  1751. *
  1752. * BB: This should be smarter. Why bother flushing pages that
  1753. * will be truncated anyway? Also, should we error out here if
  1754. * the flush returns error?
  1755. */
  1756. rc = filemap_write_and_wait(inode->i_mapping);
  1757. mapping_set_error(inode->i_mapping, rc);
  1758. rc = 0;
  1759. if (attrs->ia_valid & ATTR_SIZE) {
  1760. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1761. if (rc != 0)
  1762. goto out;
  1763. }
  1764. /* skip mode change if it's just for clearing setuid/setgid */
  1765. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1766. attrs->ia_valid &= ~ATTR_MODE;
  1767. args = kmalloc(sizeof(*args), GFP_KERNEL);
  1768. if (args == NULL) {
  1769. rc = -ENOMEM;
  1770. goto out;
  1771. }
  1772. /* set up the struct */
  1773. if (attrs->ia_valid & ATTR_MODE)
  1774. args->mode = attrs->ia_mode;
  1775. else
  1776. args->mode = NO_CHANGE_64;
  1777. if (attrs->ia_valid & ATTR_UID)
  1778. args->uid = attrs->ia_uid;
  1779. else
  1780. args->uid = INVALID_UID; /* no change */
  1781. if (attrs->ia_valid & ATTR_GID)
  1782. args->gid = attrs->ia_gid;
  1783. else
  1784. args->gid = INVALID_GID; /* no change */
  1785. if (attrs->ia_valid & ATTR_ATIME)
  1786. args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
  1787. else
  1788. args->atime = NO_CHANGE_64;
  1789. if (attrs->ia_valid & ATTR_MTIME)
  1790. args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
  1791. else
  1792. args->mtime = NO_CHANGE_64;
  1793. if (attrs->ia_valid & ATTR_CTIME)
  1794. args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
  1795. else
  1796. args->ctime = NO_CHANGE_64;
  1797. args->device = 0;
  1798. open_file = find_writable_file(cifsInode, true);
  1799. if (open_file) {
  1800. u16 nfid = open_file->fid.netfid;
  1801. u32 npid = open_file->pid;
  1802. pTcon = tlink_tcon(open_file->tlink);
  1803. rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
  1804. cifsFileInfo_put(open_file);
  1805. } else {
  1806. tlink = cifs_sb_tlink(cifs_sb);
  1807. if (IS_ERR(tlink)) {
  1808. rc = PTR_ERR(tlink);
  1809. goto out;
  1810. }
  1811. pTcon = tlink_tcon(tlink);
  1812. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
  1813. cifs_sb->local_nls,
  1814. cifs_sb->mnt_cifs_flags &
  1815. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1816. cifs_put_tlink(tlink);
  1817. }
  1818. if (rc)
  1819. goto out;
  1820. if ((attrs->ia_valid & ATTR_SIZE) &&
  1821. attrs->ia_size != i_size_read(inode))
  1822. truncate_setsize(inode, attrs->ia_size);
  1823. setattr_copy(inode, attrs);
  1824. mark_inode_dirty(inode);
  1825. /* force revalidate when any of these times are set since some
  1826. of the fs types (eg ext3, fat) do not have fine enough
  1827. time granularity to match protocol, and we do not have a
  1828. a way (yet) to query the server fs's time granularity (and
  1829. whether it rounds times down).
  1830. */
  1831. if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
  1832. cifsInode->time = 0;
  1833. out:
  1834. kfree(args);
  1835. kfree(full_path);
  1836. free_xid(xid);
  1837. return rc;
  1838. }
  1839. static int
  1840. cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
  1841. {
  1842. unsigned int xid;
  1843. kuid_t uid = INVALID_UID;
  1844. kgid_t gid = INVALID_GID;
  1845. struct inode *inode = direntry->d_inode;
  1846. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1847. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1848. char *full_path = NULL;
  1849. int rc = -EACCES;
  1850. __u32 dosattr = 0;
  1851. __u64 mode = NO_CHANGE_64;
  1852. xid = get_xid();
  1853. cifs_dbg(FYI, "setattr on file %s attrs->iavalid 0x%x\n",
  1854. direntry->d_name.name, attrs->ia_valid);
  1855. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  1856. attrs->ia_valid |= ATTR_FORCE;
  1857. rc = inode_change_ok(inode, attrs);
  1858. if (rc < 0) {
  1859. free_xid(xid);
  1860. return rc;
  1861. }
  1862. full_path = build_path_from_dentry(direntry);
  1863. if (full_path == NULL) {
  1864. rc = -ENOMEM;
  1865. free_xid(xid);
  1866. return rc;
  1867. }
  1868. /*
  1869. * Attempt to flush data before changing attributes. We need to do
  1870. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  1871. * ownership or mode then we may also need to do this. Here, we take
  1872. * the safe way out and just do the flush on all setattr requests. If
  1873. * the flush returns error, store it to report later and continue.
  1874. *
  1875. * BB: This should be smarter. Why bother flushing pages that
  1876. * will be truncated anyway? Also, should we error out here if
  1877. * the flush returns error?
  1878. */
  1879. rc = filemap_write_and_wait(inode->i_mapping);
  1880. mapping_set_error(inode->i_mapping, rc);
  1881. rc = 0;
  1882. if (attrs->ia_valid & ATTR_SIZE) {
  1883. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1884. if (rc != 0)
  1885. goto cifs_setattr_exit;
  1886. }
  1887. if (attrs->ia_valid & ATTR_UID)
  1888. uid = attrs->ia_uid;
  1889. if (attrs->ia_valid & ATTR_GID)
  1890. gid = attrs->ia_gid;
  1891. #ifdef CONFIG_CIFS_ACL
  1892. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  1893. if (uid_valid(uid) || gid_valid(gid)) {
  1894. rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
  1895. uid, gid);
  1896. if (rc) {
  1897. cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
  1898. __func__, rc);
  1899. goto cifs_setattr_exit;
  1900. }
  1901. }
  1902. } else
  1903. #endif /* CONFIG_CIFS_ACL */
  1904. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
  1905. attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
  1906. /* skip mode change if it's just for clearing setuid/setgid */
  1907. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1908. attrs->ia_valid &= ~ATTR_MODE;
  1909. if (attrs->ia_valid & ATTR_MODE) {
  1910. mode = attrs->ia_mode;
  1911. rc = 0;
  1912. #ifdef CONFIG_CIFS_ACL
  1913. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  1914. rc = id_mode_to_cifs_acl(inode, full_path, mode,
  1915. INVALID_UID, INVALID_GID);
  1916. if (rc) {
  1917. cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
  1918. __func__, rc);
  1919. goto cifs_setattr_exit;
  1920. }
  1921. } else
  1922. #endif /* CONFIG_CIFS_ACL */
  1923. if (((mode & S_IWUGO) == 0) &&
  1924. (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  1925. dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
  1926. /* fix up mode if we're not using dynperm */
  1927. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  1928. attrs->ia_mode = inode->i_mode & ~S_IWUGO;
  1929. } else if ((mode & S_IWUGO) &&
  1930. (cifsInode->cifsAttrs & ATTR_READONLY)) {
  1931. dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
  1932. /* Attributes of 0 are ignored */
  1933. if (dosattr == 0)
  1934. dosattr |= ATTR_NORMAL;
  1935. /* reset local inode permissions to normal */
  1936. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  1937. attrs->ia_mode &= ~(S_IALLUGO);
  1938. if (S_ISDIR(inode->i_mode))
  1939. attrs->ia_mode |=
  1940. cifs_sb->mnt_dir_mode;
  1941. else
  1942. attrs->ia_mode |=
  1943. cifs_sb->mnt_file_mode;
  1944. }
  1945. } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  1946. /* ignore mode change - ATTR_READONLY hasn't changed */
  1947. attrs->ia_valid &= ~ATTR_MODE;
  1948. }
  1949. }
  1950. if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
  1951. ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
  1952. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1953. /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
  1954. /* Even if error on time set, no sense failing the call if
  1955. the server would set the time to a reasonable value anyway,
  1956. and this check ensures that we are not being called from
  1957. sys_utimes in which case we ought to fail the call back to
  1958. the user when the server rejects the call */
  1959. if ((rc) && (attrs->ia_valid &
  1960. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  1961. rc = 0;
  1962. }
  1963. /* do not need local check to inode_check_ok since the server does
  1964. that */
  1965. if (rc)
  1966. goto cifs_setattr_exit;
  1967. if ((attrs->ia_valid & ATTR_SIZE) &&
  1968. attrs->ia_size != i_size_read(inode))
  1969. truncate_setsize(inode, attrs->ia_size);
  1970. setattr_copy(inode, attrs);
  1971. mark_inode_dirty(inode);
  1972. cifs_setattr_exit:
  1973. kfree(full_path);
  1974. free_xid(xid);
  1975. return rc;
  1976. }
  1977. int
  1978. cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  1979. {
  1980. struct inode *inode = direntry->d_inode;
  1981. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1982. struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
  1983. if (pTcon->unix_ext)
  1984. return cifs_setattr_unix(direntry, attrs);
  1985. return cifs_setattr_nounix(direntry, attrs);
  1986. /* BB: add cifs_setattr_legacy for really old servers */
  1987. }
  1988. #if 0
  1989. void cifs_delete_inode(struct inode *inode)
  1990. {
  1991. cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
  1992. /* may have to add back in if and when safe distributed caching of
  1993. directories added e.g. via FindNotify */
  1994. }
  1995. #endif