inode.c 60 KB

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