inode.c 62 KB

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